"Will the following code throw any error? int *ptr1 = new int; int *ptr2 = ptr1; … … delete ptr1; *ptr2 = 10; cout << *ptr2; The above code works in VC++. I am not able to understand how this works. Kindly advise. Thanks in advance. Regards, Dhamayanthi
The code may throw an error or may not in C++,VC++ (In C for free())
Say ptr1 is pointing to the address 0x554568.
After the delete statement still ptr1 contains the same memory address
though the memory it was pointing to is deallocated.
ptr1 has become dangling poiter and also ptr2.
your code still works because 0x554568 is a valid address.
The danger is here.
Say again somewhere in the code (Especially in multithreaded apllication
where there are frequent memory allocation and deallocation)
you are allocating or memory by some other module has been allocated to some
other pointer(q). The above address(0x554568) may be allocated
to q. Now ptr1, ptr2 and q are pointing to the same address 0x554568. Later
if the memory allocated to q is deleted and poiter is set to NULL
(delete q; q=NULL)
then during the print (cout << *ptr2; ) application crashes.
The possibility of crash is more frequent in larger applications
So the good programming practice is that pointer should always contain a
valid address or NULL
1.
int *p; //danger
int *p=NULL; //good
2.
danger
int p=(int)malloc(sizeof(int));
*p=10;
free(p);
good
int p=(int)malloc(sizeof(int));
*p=10;
free(p);
p=NULL;
regards
Sangappa
Hello,
when we delete the memory(delete ptr1;), the memory is not immediately
released by the MMS( Memory Management System).
the memory area is still accessible by pointer ptr1.but this memory is
placed in POOL state.
So MMS have a chance to allocate this area to some other process.
If MMS has allocated this area to some other process,
Now if you call *ptr2 = 10; MMS can flashes a Segmentation Fault (Crash).
Deleting the pointer informs the run time that you are not longer using the memory.
The pointer is, in a sense, just a number. You set that number to a value, then you deleted memory that that value points at. The value does not change.
That is why I always set a point to null ( or to point at something else ) after calling delete on that pointer.
The other question you have not asked, is why can you still reference that bit of memory. That memory still exists prior to your allocation ( although it may not be part of your process space ), during your allocation ( when it certainly is part of your process space ), and after ( when it is undefined as to exactly when the heap managers does it’s thing, and how your process space is managed ). You were able to use the memory location after the delete without an exception. That is not good form, and you should definitely not rely on this working, especially the further out in time you get away from the call to delete, or if you port to a difference platform, upgrade the existing OS, tool chain or ???.
If you call delete, null the pointer, don’t attempt to use it again.
Another form issue for you, if you like, having two pointers point at the same item is another thing to avoid, if you can help it. That is where you get the true dangling pointer issue, in any non-trivial app. The second pointer will likely not be “right next to” the first, and as you can see, there is no notification to either pointer that the number they contain is not a good pointer any more.
David
tamizzh via cpp-l
When delete is called, the memory is not immediatly reclaimed by the memory
management system. The memory area is still accessible. But if you do more
allocations after deletion the memory area might be reused and with *ptr2 10, we might end up corrupting data in some other variable.
thanks and regards,
– Ravi
On Thu, Jun 26, 2008 at 1:57 AM, Veer via cpp-l
I’m using GCC in linux platform for the below code
int *ptr1 ;
ptr1 = new int;
*ptr1 = 10;
int * ptr2 = ptr1;
std::cout<<"ptr1 address: "<
Yes it may work …
Actaully when you delete some memory you can say it is more like a node has
been moved from one busy list to free list.
Though it still points to same memory location…
But this code is very very risky and must be avoided…
reason is : since this memory block has been moved to free list it may be
assigned to another new or malloc request and you may find you value over
written.
leading to unexpectated results…
thanks
Chakresh
The delete operator returns allocated memory to the freestore.
int *ptr1 = new int;
int *ptr2 = ptr1;
In these statements, ptr1 is just declared and never initialized.
‘delete ptr1;’ was intended to deallocate the memory with which ptr1 was
initialized.
some thing like this:-
int *ptr1 = new int;
*ptr1 = 12;
Now if you do ‘delete ptr1;’ then ‘*ptr1’ will give you garbage value not
12.
In your case, you delete the ptr1 before initializing it.
Then you initialize '*ptr2 = 10; ’ This will make ptr1 and ptr2 to point
to value 10. The first call to 'cout << *ptr2; ’ would print the value BUT
at the same point it will show a exception triggered at
‘_free_crt(refcount)’ in ‘setlocal.c’ [ONLY WHILE DEBUGGING], since you
deleted it. And hence forward *ptr1 and *ptr2 will have garbage value.
Also the best idea is to do ptr1 = 0 whenever you delete it, it makes sure
that pointers don’t become dangling or contain garbage value.
Regards,
Mitendra
“Veer via cpp-l”
Sorry for this belated answer.
All those who have already contributed to this subject may to suggest that “a dangling pointer is one that is pointed to a location that is no longer valid but was valid a while ago… it formerly pointed to something that has moved or disappeared, e.g. a heap-allocated block which has been freed and reused”.
One person said - rightly - that this is an axample but I wish he/she had given us another example.
Well, a dangling pointer is simply a pointer that doesn’t actually point at anything valid - uninitialized pointer - whatever the course of it NOT pointing to something valid. So another example? A newly declared pointer!
float *x; //x is a dangling pointer. Dare use it as it is now/here!!
Austin Owino
Nairobi, Kenya, East Africa
From Amit Sarode via cpp-l
A dangling pointer is a pointer to storage that is no longer allocated.For
eample in the below code “pointer j” become the dangling pointer once u
delete “i”
#include
amit
dangling pointer is nothing but pointer pointing to the object which is no
where exist?
let say
int *ptr1= new int(10);
int *ptr2 = ptr1;
delete pt1;
now ptr2 becomes dangling pointer
Hi!
What do you mean by dangling pointer?
Can anyone illustrate ??
-amit
Dhamayanthi, u might be doing wrong
if u do this
int *ptr=0;
ptr= new int;
delete ptr;
ptr=0;
*ptr=100
it will give u 100% exception.
Thanks
Krushna
From: “Dhamayanthi N - CTD, Chennai. via cpp-l”
Hi D,
ptr1 = 0; will point the ptr1 to address 00000000 (this mean NULL in
C/C++). This command do not impact ptr2.
When you declare:
int *ptr1 = new int;
int *ptr2 = ptr1;
you declare that ptr2 is point to same address as ptr1. So, when you
put the command:
ptr1 = 0;
that’s just changing the value of ptr1 and not changing ptr2 which
still have same value (address).
Best regards,
Bunamin Uning
========================================================================
Wednesday, August 21, 2002, 3:21:23 PM, you wrote:
DNCCvcl> C++ news updated daily - http://www.ittoolbox.com/i/cpp2.asp
DNCCvcl> I thought ptr2 is pointing to an illegal memory address since the memory is
DNCCvcl> released by ptr1.
DNCCvcl> If I include ptr1=0 in the code, it is not throwing any exception. It still
DNCCvcl> works.
DNCCvcl> Thanks & Regards,
DNCCvcl> Dhamayanthi
DNCCvcl>
Hi D,
I try the small program like this:
//======================================
#include
I thought ptr2 is pointing to an illegal memory address since the memory is
released by ptr1.
If I include ptr1=0 in the code, it is not throwing any exception. It still
works.
Thanks & Regards,
Dhamayanthi
This program may not throw up an error.
But the result is unpredictable.
ptr1 and ptr2 are pointing to the same address that you have allocated.
after deleting ptr1 this memory is no longer valid, or else this is also
called a dangling pointer.
After which you are assigning a value to this invalid memory location.
This will corrupt the memory and might change the values of other variables.
regards
Ashok.
Dhamayanthi this will execute because u r deleteing the object which is
created by new but ptr2 still pointing to valid address. delete only deletes
object and free memory allocated by new but it doesn’t point to null. Even u
ptr1 points to null it doesn’t means that ptr2 to also point to null.
u try this will give exception when program is exiting.
int *ptr1 = new int;
int *ptr2 = ptr1;
…
…
delete ptr1;
ptr1=0
*ptr2 = 10;
cout << *ptr2;
you are re-writing the memory that doesn’t belong to you in assignment *ptr2=10
maybe it works in a simple example, but if you do some more allocation/deallocation it will cause problems… (SIGSEGV on Unix)
m.