Skip to main content
gamedev.net
🔒 Locked

Learning C++ pointers and memory

Started by tufflax Dec 12, 2005 at 8:44 PM 3 replies 350+ views
Original Post
tufflax
tufflax
Hey! I've been reading this book, by Stephen Prata and everything is fine, but there is some things I wonder about... If I for example do: char *ptr = new char; I need to delete it when im done, that will give back the memory to the pool, but... I've seen this in an example in the book: char *ptr = "hello"; The book explains why it works but my question is; why dont I have to use delete on that pointer? At least the book doesnt do it. What happends with the memory it uses when the function it is in is done? And what happends with: char *ptr2 = "hello"; ptr2 = "hey"; What happends with the string "hello"? is it automatically deleted, and the memory given back? or is that wasted memory? Also, what about... int *ptr; ... { // THE block int a = 2; ptr = &a } From outside the block, can I use a? can I use *ptr? Can I even do that? Thanks in advance!
Ra
Ra
You only delete what you new.

In the case of char * ptr = "string literal";, "string literal" is stored somewhere in the executable and the pointer points to it. This pointer was allocated when the program was loaded (as part of the program image, not with the new allocator), so it doesn't have to be freed and is unloaded when the program is unloaded. It is effectively part of the program.

newd pointers are on the heap, only in memory and not part of the program image, so they have to be deleted to tell the OS to free that memory. Any sane OS will keep track of those allocations and free them when the program terminates, but it's best practice to delete that memory yourself.

Quote:
Original post by tufflax
Also, what about...
int *ptr;
...
{ // THE block
int a = 2;
ptr = &a
}

From outside the block, can I use a? can I use *ptr? Can I even do that?

No. Since a was defined inside the block, it goes out of scope when you exit the block and is not safe to access outside the block. This is because it's likely allocated on the stack, and will be popped off the stack (and the pointer will no longer point to that variable) at the end of the block.
Ra
Sharlin
Sharlin
Quote:
Original post by tufflax
I've seen this in an example in the book:

char *ptr = "hello";

The book explains why it works but my question is; why dont I have to use delete on that pointer? At least the book doesnt do it. What happends with the memory it uses when the function it is in is done?


The string "hello" is statically stored inside the executable itself (only once; if you have const char *a = "foo", *b = "foo"; a and be point to the same address). The string always exists, so you don't need to release it.

BUT: if the book really had char* without const, it was wrong. The type of that kind of character literals *must* be const char*, because the string itself may well be stored in a read-only part of the program's memory space. And because the literals are shared, not-nice things might happen if they were writable.

Quote:

And what happends with:

char *ptr2 = "hello";
ptr2 = "hey";

What happends with the string "hello"? is it automatically deleted, and the memory given back? or is that wasted memory?


The same applies: there is place reserved for both "hello" and "hey", and you can freely change where ptr2 points to. Nothing is wasted. Again, though, that should be const char *ptr2.

Quote:

Also, what about...
int *ptr;
...
{ // THE block
int a = 2;
ptr = &a
}

From outside the block, can I use a? can I use *ptr? Can I even do that?


No, you can't. In an abstract sense, a ceases to exist when its scope ends, and ptr ends up pointing to garbage. Most compilers should notice and warn about it when you try to do something like that.

tufflax
tufflax
Thanks! That helped out. But... I didn't quite understand the thing about const.

So string literals are read only? And I cant write to them. So what good does the const key word do? I mean, except for making the code more understandable and maybe gives the compiler a hint or something? Or are they (the string literals), but should not be, writeable as long as I do NOT use the const key word?

Also, how do I format the text so it look more like code? The way you two did.
Sharlin
Sharlin
Well, given
const char *foo = "hello";

the compiler stops you from modifying the string through the pointer - for example, this:
foo[1] = 'a';

will result in a compile-time error. If it weren't const, that would have compiled, but highly probably crashed your program at run-time (which is a Bad Thing(tm)).

Quote:
Also, how do I format the text so it look more like code? The way you two did.

Info about formatting messages can be found in the forum FAQ.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.