-2

One of the requirements for my first C++ assignments was that we must make sure that any allocated space was deallocated. However I ran into a problem when trying to determine if and how one would deallocate a non pointer variable. I have seen that outside of scope these variables are deallocated but could one also have:

int var_name=new int(6);
...
delete &var_name;

both seem to run but I don't understand why or how deallocating the address could cause problems. Thanks.

  • 1
    You didn't allocate it so why you want to deallocate it ? deallocate the allocated elements (in heap). not in stack. –  Aug 31 '14 at 05:48
  • Seems to be a duplicate or at least very similar to: http://stackoverflow.com/questions/9026640 – Rufflewind Aug 31 '14 at 05:48
  • Thanks, like I said first assignment still trying to get my bearings on the language I am having a hard time figuring out how to ask my questions. Would this cause a problem or is it being redundant? – Kathryn Levin Aug 31 '14 at 06:41

2 Answers2

0

You only have to deallocate the variables you have initialized with new.

robbannn
  • 5,001
  • 1
  • 32
  • 47
0

There are couple of points you can keep in mind:

  1. Memory has to be de-allocated only when it has been explicitly allocated using the new operator. If new operator is not used then memory is allocated on stack and it is de-allocated when you go out of scope. With new operator, on the other hand, memory is allocated on heap and has to be explicitly deleted. Consider this:

    void fun () {

    // 1. memory allocation on stack
    int var_on_stack = 10;
    
    // 2. memory allocation on heap
    int* var_on_heap_ptr = new int ( 10 );
    
    // 3. delete memory allocated on heap
    delete var_on_heap_ptr;
    

    }

In statement 1, 4 bytes ( considering an integer occupies 4 bytes ) are allocated for var_on_stack. This memory is on stack and need not be explicitly deleted. In statement 2 again 4 bytes are allocated on heap and the address of the first location is assigned to var_on_heap_ptr. When memory is allocated on heap using new, compiler will allocate required amount of memory and return you the address of the first memory location. That is why var_on_heap_ptr is declared as an integer pointer. This memory is allocated on heap and has to be explicitly deleted by user using the delete operator. In your question you have written your first statement works. It should not. You have to declare var_name as int* and not int for it to work otherwise you will get compile error.

  1. When de-allocating memory allocated on heap use the delete operator. delete operator takes a pointer as argument and assumes that the memory location the pointer is pointing to was allocated using new operator. Remember it will assume and will not verify. If you pass the address of a stack variable to delete then this memory will be de-allocated twice. Once by delete operator and again when the variable goes out of scope. And as any c++ programmer will tell you, this is a surefire recipe for disaster.

  2. Consider the following statement again:

    int* var_on_heap_ptr = new int ( 10 );

    Here the pointer variable var_on_heap_ptr will contain address of memory allocated on heap but the variable itself will reside on stack. That means the compiler will first allocate four bytes for the pointer variable var_on_heap_ptr (Usually a pointer variable will occupy four bytes but it can be machine dependent) . This memory is on stack. Next the compiler will allocate four bytes for an integer on heap and store the first address of these four bytes in var_on_heap_ptr variable. That means the variable itself is on stack but its value is ( i.e. it is pointing to ) the address of a memory location allocated on heap. When you say delete var_on_heap_ptr; the compiler will de-allocate the "memory pointed to by var_on_heap_ptr" i.e. the memory allocated on heap. Then when you go out of scope, the memory assigned to the var_on_heap_ptr variable will be de-allocated by compiler since the variable is on stack. Therefore all the memory assigned, both on heap and stack, gets de-allocated.

  3. One final point. Note that the the delete operator will delete the memory pointed to by the var_on_heap_ptr variable but will leave the contents of the variable unchanged. That means if the var_on_heap_ptr contains 0xaabbccdd as the memory address before delete operation, it will continue to hold 0xaabbccdd even after delete operation is complete. The difference is that before delete the pointer variable was pointing to memory allocated on heap but after delete the memory has been freed and the variable is now pointing to de-allocated memory ( which could now have been allocated to some other process ) and hence cannot be relied upon. That means if you now try to use the pointer variable you program can crash. Also if you try to delete the variable twice, the compiler will again try to de-allocate the memory pointed to by the variable and the results can be disastrous. To avoid this after calling delete operation on a pointer variable, its good to set the pointer variable to NULL so that even if someone calls delete again on this variable, the program will not crash as delete operation on NULL is safe.

    Hope this helps.

Vimal
  • 436
  • 1
  • 4
  • 14