Pointers
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!
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.
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
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
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...
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.
[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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement