PDA

View Full Version : memory allocation question



StOoZ
April 12th, 2008, 01:13 PM
I would like to know, what is the difference between these two:

char* ptr = new char[4]
ptr = "ggg";
delete[] ptr;
ptr = new char[5];

and:

char* ptr = new char[4]
ptr = "ggg";
ptr = new char[5];


as you can see, one uses a delete , and the other , just plain renewing, I cant figure out, what is the difference of the final outcome.

Wybiral
April 12th, 2008, 01:30 PM
Run the second one in a loop and see what happens :)

Don't! It's not "renewing" it's just grabbing more and more memory and losing it to the abyss. You MUST explicitly free all memory that you allocate with "delete".

aks44
April 12th, 2008, 01:32 PM
Or, just run the first one and see what happens... I wouldn't be surprised if you get a core dump...

Wybiral
April 12th, 2008, 01:36 PM
Or, just run the first one and see what happens... I wouldn't be surprised if you get a core dump...

lol, I missed that one.

When you say


ptr = "string";


Keep in mind that "string" is a pointer to a string literal... You're not copying that string into the memory you allocated, you're actually just losing grid of that allocated memory (lost to the abyss). You have to copy it.

But, you should be using the string class for this anyway...

StOoZ
April 12th, 2008, 01:44 PM
ok , so lets assume I used strcpy for the first one and not just "string" which is constant.

when I use the new, without delete, if I have this :
char* ptr = new char[5];
strcpy(ptr,"fine");

and I do this:
ptr = new char[10];

I assumed that I I'll try to check the output of:
ptr[0];
I'll see the letter 'f'., but its not the case,even thought the address is the same, how come?

aks44
April 12th, 2008, 02:05 PM
ok , so lets assume I used strcpy for the first one and not just "string" which is constant.

when I use the new, without delete, if I have this :
char* ptr = new char[5];
strcpy(ptr,"fine");

and I do this:
ptr = new char[10];

That's called a memory leak. Again, you have to delete any new'ed memory, and delete[] any new[]'ed memory.



but its not the case,even thought the address is the same, how come?

The pointer's address obviously stays the same, but the pointed-to address (IOW the contents of the pointer) changed.


Keep in mind that a pointer is a memory area (aka. variable) that contains the address of another memory area.

lnostdal
April 12th, 2008, 02:11 PM
offtopic: beware; you have just found an orange with two layers of peeling on it .. O_O .. edit: and, lol, not even tinfoil can save you now .. (to quote your sig., aks44)

ontopic: ok, seriously .. pointers can be confusing .. draw some boxes and arrows .. it helps sometimes =)