pointer deletion querry
-
Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?
-
Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?
Deepu Antony wrote:
delete * p;
It's like:
delete p;
Deepu Antony wrote:
Whether pointer q is also created on the heap.
Nope, heap allocation occurs only when you use any of the heap allocation functions (
new
,calloc
,malloc
, etc.,)Deepu Antony wrote:
Is q = NULL is correct?
No.
q
has the address of the variablea
. And can be pointing to some random value if you hadn't assigned it like that.It is a crappy thing, but it's life -^ Carlo Pallini
-
Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?
Deepu Antony wrote:
Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?
No, q is not allocated on the heap. It just contains the address of a, you didn't allocate anything for it. Keep in mind a very simple rule: for each call to new, you should have a matching delete. No more, no less. In this case, you have one single new (for the p pointer), so you have to call delete on this pointer, and that's it.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?
Thank you all very much.
-
Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?
Deepu Antony wrote:
Whether pointer q is also created on the heap. If so how to delete it?
If the momory pointed by q (pointers are ususally created on the stack) is created on the heap then you must use delete.
Deepu Antony wrote:
Is q = NULL is correct?
Again, when
q
points heap-allocated memory you've to delete it. Invalidating the pointer afterward is a good pactice (for instance):int * q;
q = new int[10];
//...
//...
if (q) delete [] q;
q = NULL;If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?
If you delete memory, which you didn't allocate dynamically, you surly putting yourself in trouble. Remember simple rule, you only need to delete the memory, which is you allocate to the program! <blockquote class="FQ"><div class="FQA">Deepu Antony wrote:</div>Is q = NULL is correct?</blockquote> yes!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You