why is it happening so???
.....why does it print "hello" the second time also???Code:void main() { char *x="hello"; printf("%s \n",x); delete x; printf("%s \n",x); getch(); }
why is it happening so???
.....why does it print "hello" the second time also???Code:void main() { char *x="hello"; printf("%s \n",x); delete x; printf("%s \n",x); getch(); }
It could do anything, because the code is broken in so many ways.
1. main returns int, not void
SourceForge.net: Void main - cpwiki
2. delete is a C++ thing, but the rest of the code is apparently C
3. You can only delete/free something which was originally allocated using new/malloc
4. Even when you've fixed all those problems, deallocating memory does NOT automatically make your pointer NULL, nor does it necessarily mean that the memory it points to is 'reset' in some way. If the code dereferences a pointer after calling free, then it is broken - no matter how much it seems to you like nothing has happened.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Then what is the use of delete or free when we can retrieve the value after using them???
To destroy the object or to free memory. Note that attempting to "retrieve the value after using them" results in undefined behaviour.Originally Posted by mohnish_khiani
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
To expand a little on what laserlight (Hi Lase!) said...
When you use malloc() to reserve memory for an object, you have to free() the object when done with it...
However, free() does not *erase* the data. It only releases the memory used back to the shared memory pool. The values you stored in that memory are not erased... but they might be overwritten at any instant by a malloc() call from elsewhere. Thus you cannot trust memory that has been freed, resulting in unpredictable (undefined) behavior.
The reason that memory is not erased is a simple matter of speed. Going through a large chunk of memory and resetting it all to 0 (or some other value) takes time. If it's done often enough it will impact on your software's performance and the performance of the machine itself. So they just leave it sitting there until the space is needed for something else.
> Then what is the use of delete or free when we can retrieve the value after using them???
Try this
Now on machines which allocate the same block of memory that was just freed (if it is large enough), foo and bar will end up pointing to the same place.Code:int main ( ) { char *bar; char *foo = malloc(10); strcpy( foo, "hello" ); printf("foo=%s\n", foo ); free( foo ); bar = malloc(10); strcpy( bar, "world" ); printf("foo=%s\n", foo ); // What does this print, and WHY printf("bar=%s\n", bar ); free( bar ); return 0; }
Now imagine if you had instead of just printing foo after deleting it, you had done something dangerous like
strcpy( foo, "bombed" );
How disappointed would you be to see on trying to print bar that you didn't get the world you were expecting?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
yaa it prints world the second time....thus resulting in unpredictable behaviour...