-2

In c++ ,if we want to destroy the built-in type(as to int) to save the memory when the program is running how we should do?if We do like this:

int h;
int *p=&h;
delete p;

To destroy h,I need to create a pointer which points to h then execute the delete operation,but pointer p still consumes the equal memory,it seems like that I didn't do anything to save the running memory,what should I do?

Kaiser
  • 57
  • 2
  • 4
    You `delete` only what you allocate using `new` – Gaurav Sehgal Jul 24 '17 at 11:35
  • 1
    You can't. And normally you would not need to either. The space taken up by variables (global, local or static anywhere) is usually *very small* compared to everything else. – Some programmer dude Jul 24 '17 at 11:36
  • 1
    If you are using temporary variables you can create them in sub scopes so that if code is out of scope variable is deleted – Muhammet Ali Asan Jul 24 '17 at 11:36
  • 1
    *"if We do like this:"* - Our program has undefined behavior. If you want to write good and efficient C++, [pick up a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – StoryTeller - Unslander Monica Jul 24 '17 at 11:37
  • 1
    Read much more about C++ programming and about [**undefined behavior**](https://en.wikipedia.org/wiki/Undefined_behavior) – Basile Starynkevitch Jul 24 '17 at 11:37
  • `h` is on the stack, you can only delete memory with `delete` which is on the heap, e.g. created with `new`. – amuttsch Jul 24 '17 at 11:37
  • 1
    To destroy an automatically allocated type, let it go out of scope. – NathanOliver Jul 24 '17 at 11:38
  • Actually ,I want to know whether the pointer consumes memory when it points null. – Kaiser Jul 24 '17 at 11:38
  • 1
    @Kaiser Generally all variables occupy memory. Their contents or value doesn't matter. – Some programmer dude Jul 24 '17 at 11:39
  • I think you have some misconceptions about the basic memory model for C / C++. You'd be better off looking at stack vs heap allocation and object scope (lifetimes). – Brett Hale Jul 24 '17 at 11:41
  • as you are writing c++ you should worry much more about correctness (something you dont get for free) rather than efficient memory usage (something you get for free to a certain extend) – 463035818_is_not_an_ai Jul 24 '17 at 11:43
  • The compiler will do this for you. If you have variables used in non-overlapping sections of code the compiler could use the same memory area for all of them. Or keep the values in CPU registers and never store them in memory at all. – Bo Persson Jul 24 '17 at 12:42

3 Answers3

4

You're not really asking about how to destroy a "built-in type" but rather how to destroy an object with automatic or static storage duration. And that you can't do: the behaviour of your code is undefined.

You can only call delete on a pointer that's given back to you as a result of a call to new.

Your approach is therefore an ineffective memory saving technique.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    It is however a very effective *feeding-cats-to-compilers* technique. – StoryTeller - Unslander Monica Jul 24 '17 at 11:38
  • The delete operation is just to destroy h,h consumes some memory,when I don't want to use it ,can't I destroy it to save some memory?what I want to know is that the memory the pointer occupied whether influence the effectiveness of the program. – Kaiser Jul 24 '17 at 11:46
  • @Kaiser you cannot delete h. C++ is to a large part about not managing memory, so what you want to do is a bit backwards. Anyhow you cannot delete anything that wasnt created via `new`. And no, it isnt clever to use `new` all over the place just so you can `delete` stuff afterwards. Use scopes: `{ int i; }` after this code has been executed `i` is cleaned up automatically – 463035818_is_not_an_ai Jul 24 '17 at 11:54
  • 1
    @StoryTeller: Indeed it is ;-) – Bathsheba Jul 24 '17 at 11:59
2

As said in the other answer, you can't manually destroy a built-in type(*).

However, you can reduce the scope where it's defined:

int foo = 3;
...
{
    int bar = foo + 2;
    ...
}
// bar is not defined since it's been "destroyed" 
// when going out of scope

Here you create a anonymous scope. From this scope, you can access variable defined in the parent scope but once you go out of anonymous scope, each variables defined in it is destroyed.

EDIT:

(*) You can delete a dynamically allocated built-in type (int * p = new int(); delete p;) but you can't destroy them since they don't have a destructor.

nefas
  • 1,120
  • 7
  • 16
1

The simple built-in types do not need any destruction - you can just overwrite their memory with whatever you like. This will not have any negative effects when the variable's lifespan ends. However note that such variables - like h in your example - typically live on the stack, so that place in memory will get used by some other function's variables and you can't reserve it for your own use.

I think you might be confusing freeing allocated memory and destroying an object.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • so only the dynamic memory will effect the effectiveness of the program? – Kaiser Jul 24 '17 at 12:01
  • I'm not sure what you mean by the program's "effectiveness". If you mean "memory footprint", then - you could go out of stack space, e.g. with recursive calls which allocate long arrays, and eventually get a Stack Overflow (like the name of this site). – einpoklum Jul 24 '17 at 13:24