Pointers

Started by
6 comments, last by virtuacasper 19 years, 3 months ago
Is it absolutely necessary to release ALL pointers? And if so, how? My book shows how to create them but im not sure if I should be releasing them. Thanks for any comments!
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Advertisement
Pointers *point* to memory blocks. You don't release pointers, you release the memory they point to, if that memory was dynamically allocated. Basically, each time you "new" something somewhere, you have to "delete" it before the program ends.

Looking for a serious game project?
www.xgameproject.com
ok so if I were to create a pointer:

int *pi;

and if I were to point it to the a variable location:

int a = 7;
pi = &a

then when I was done with it, I would have to go

pi->Release();

Is this correct?
Thanks
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Which book is this? What language?
no, for one thing, int is a primitive, your treating it as an object. Your probably thinking of delete, but you only have to delete something if you new it.
I was just making guesses... But the book im using is by skanksholm. Thanks for your replies. So from here on I have to assume I need to release whatever is created with the new operator...
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein (1879-1955) That is so very true...
Seriously consider looking at another book.
[edit]Didn't read your last post. You are perfectly right about new/delete.[/edit]
To answer your question, in C/C++ you only need to release pointer that you allocated memory for, e.g.
malloc() → free()
new, new[] → delete, delete[]

Since you didn't ask about the functions and operators I assume you didn't allocate any memory. In this case there is no need to release it as the pointer is only, well, pointing [smile] to an existing memory location and thus is not different from any other variable on the stack. It will be invalid once it goes out of scope.


HTH,
Pat.
i think your thinking of Release() method from COM/DirectX

This topic is closed to new replies.

Advertisement