Dynamic memory allocation problem

Hello.
I was reading in the last time about dynamic memory allocation in c++. For tests i write this small piece of code bellow. As you see i simply allocate one char and one int array. After giving them value i delete them (by using delete []) and then try to write on screen. In result i got the same as without "delete", data objects are still allocated in memory! I am using the newest version of Code::Blocks (that usually handle c++ 11 standards).

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
  //char string memory allocation
    char * pc = new char [arr_size];
    pc = {"Jedi's strength come from the force!"};
    delete [] pc;
    cout << "Char pointer \"pc\": \"" << pc << "\"" << endl <<
    "is located at: " << &pc << " and is length at " << strlen(pc) - 1 << " fields." << endl << endl;

    //int array memory allocation
    int *pi = new int [arr_size];
    for (int i = 0; i < arr_size; i ++)
        pi[i] = i;
    delete [] pi;
    cout << "\"pi[10]\" = " << pi[10] << endl;

Thank you for help!
Daniel
closed account (E0p9LyTq)
Using delete/delete[] just deallocates the memory so your program can't use it without causing logic problems, it doesn't change the last value stored at that memory location.

C++ will allow you to change the value of deallocated memory, but that is definitely not a good thing to do.
Last edited on
Thank you for answer. That exactly what i was looking for, short and precise. So simply, when i use delete for data object its address is not changing and value under this address either but this address is not longer owned by the object? So use of such could give undefined result because this address can be already use by some other program. Am I right? :)
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

int main()
{
   unsigned short* pShort = new unsigned short;
   *pShort = 10;

   std::cout << "*pShort:  " << *pShort << "\t";
   std::cout << "pShort:   " << pShort << "\n";
   delete pShort;

   long* pLong = new long;
   *pLong = 90000;

   std::cout << "*pLong:   " << *pLong << "\t";
   std::cout << "pLong:    " << pLong << "\n\n";

   // uh oh, this was deleted!
   *pShort = 20;

   std::cout << "*pShort:  " << *pShort << "\t";
   std::cout << "pShort:   " << pShort << "\n";
   std::cout << "*pLong:   " << *pLong << "\t";
   std::cout << "pLong:    " << pLong << "\n\n";

   // setting a deleted pointer to nullptr will prevent memory problems
   delete pShort;
   pShort = nullptr;

   delete pLong;
   pLong = nullptr;

   return 0;
}


I get an output as follows:

*pShort:  10    pShort:   0x8d6b10
*pLong:   90000 pLong:    0x8d6b10

*pShort:  20    pShort:   0x8d6b10
*pLong:   65556 pLong:    0x8d6b10


Notice, both pointers point to the same memory location! Ouch!

Using a deleted pointer that wasn't set to nullptr can cause some real bad things! A rule of thumb I have is when I delete, I set the pointer to nullptr.

If you try to access a pointer set to nullptr your program will crash. Better to crash than quietly alter heap memory. That can be real tough to debug.
That's good advice, thank you. But something heart me inside when i see you release the same memory cell two times. In book I'm reading now ("C++ Primer Plus (Developer's Library)") i learn it can cause serious problems and such operation is not defined, so actually this can result with anything. :)
closed account (E0p9LyTq)
Releasing memory twice can indeed cause problems, which is why I delete and then assign nullptr. So if later I should accidentally reuse a nulled pointer the program crashes.

C++ doesn't protect against a programmer making dumb mistakes as many other programming languages do.

Oh.. i rewrite my code. I was thinking i will get the same result as you, I mean new pointer will be allocated in place of the deleted one. But... no. That's the piece of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
char * pc = new char [arr_size];
    pc = {"Jedi's strength come from the force!"};
    cout << "Char pointer \"pc\": \"" << pc << "\"" << endl <<
    "is located at: " << &pc << " and is length at " << strlen(pc) - 1 << " fields." << endl << endl;
    delete [] pc;
    pc = nullptr;

    char * pc2 = new char [arr_size];
    pc2 = {"Pika.. Pikachu!"};
    cout << "Char pointer \"pc2\": \"" << pc2 << "\"" << endl <<
    "is located at: " << &pc2 << " and is length at " << strlen(pc2) - 1 << " fields." << endl << endl;
    delete [] pc2;
    pc2 = nullptr;


and result is:

Char pointer "pc": "Jedi's strength come from the force!"
is located at: 0x28fef4 and is length at 35 fields.

Char pointer "pc2": "Pika.. Pikachu!"
is located at: 0x28fef0 and is length at 14 fields.


Now whats strange, when i show address of 'pc' with 'cout' after deleting it and don't show address of 'pc2', pc change its position to 0x28fef0, so there where i been expecting. Here is exemple:

1
2
3
4
5
6
7
8
9
10
11
12
13
char * pc = new char [arr_size];
    pc = {"Jedi's strength come from the force!"};
    cout << "Char pointer \"pc\": \"" << pc << "\"" << endl <<
    "is located at: " << &pc << " and is length at " << strlen(pc) - 1 << " fields." << endl << endl;
    delete [] pc;
    pc = nullptr;

    char * pc2 = new char [arr_size];
    pc2 = {"Pika.. Pikachu!"};
    cout << "Char pointer \"pc2\": \"" << pc2 << "\"" << endl;
    cout << "\"pc\" is located at: " << &pc << endl;
    delete [] pc2;
    pc2 = nullptr;


and result:

Char pointer "pc": "Jedi's strength come from the force!"
is located at: 0x28fef0 and is length at 35 fields.

Char pointer "pc2": "Pika.. Pikachu!"
"pc" is located at: 0x28fef0


Just in the moment i try to write on screen 'pc2', 'pc' address change again to 0x28fef4. That make my really uncertain about how memory is really allocating. Any idea why is that happening?
Last edited on
closed account (E0p9LyTq)
I'd personally try to avoid using C-style strings if possible, instead use the C++ string library. Also use smart pointers instead of raw pointers. That way all the nitty-gritty details of memory management are done automatically.
Topic archived. No new replies allowed.