Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is deallocates means delete ?

Status
Not open for further replies.

anxious7

Technical User
Nov 23, 2007
7
0
0
struct mystruct
{int *x1;
int x2;};


struct mystruct obj1;
obj1.x1 = malloc(5*sizeof(int));
*obj1.x1=40;
free(obj1.x1);
printf("%d %d \n", *obj1.x1, obj1.x2);

at printf *obj1.x1 is 0.
 
Well if you try to dereference a pointer after you have freed it, then anything can happen.

As it stands, the code is broken.

Please use [code][/code] tags when posting code.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
sorry
i'm confused because with

Code:
*obj1.x1=40;
*(obj1.x1+1)=50;
free(obj1.x1);
printf("%d \n", *(obj1.x1+1));

the result is 50

 
Doesn't matter whether it prints what you "expect" or what the last value you stored before it was deleted, it's still wrong.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
I don't care if it is wrong. i need to know for some reason what's the exact behavior of free
 
Who knows what free() does apart from what the manual says. The pointer you pass to it is no longer valid following the call to free, so don't use it.

Debug versions of free() in particular might trash all the data to make it plainly obvious that you're accessing memory after you have freed it.

Most likely, the memory will become available again for some other purpose with a following malloc call. If you're still hanging on to the old pointer, then chaos will ensue.

"Elvis has left the building, there is no point screaming for an encore."


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
As Salem says...undefined behavior and platform specific. The memory has been released and whether or not the old location is cleared or not is immaterial. The address at which the pointer is directed is no longer valid for storage.

Usually what one does is set the pointer to NULL after free in order to be (somewhat) safe.
 
It is like throwing something into a bin. You cannot guarantee that it will be in the bin the next time you look. It may still be in the bin or the bin may be empty or full of other rubbish.
 
Think of malloc() as putting your cookies in a bag in the fridge at work with your name on it. People know they're your cookies, so they won't touch them (usually).
Then think of free() as putting the cookies on a table in the lunch room with a sign that says "free cookies". You might have 5 cookies to start with, but if you come back in a few minutes you could still have 5 cookies, or you could have none.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top