473,756 Members | 7,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to check the function free()

I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing. Why I cannot
free them?

Sorry, I cannot post my huge program here.

Thanks for any comments

Mar 30 '06 #1
7 2050

questions? wrote:
I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing. Why I cannot
free them?

Sorry, I cannot post my huge program here.

Thanks for any comments


Sir,

This is comp.lang.c, not comp.lang.c.Iha veESPandCanRead YourCodeByMagic
forum. Please post the entire code.

Thank You

Mar 30 '06 #2
un************@ hotmail.com wrote...
I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing.
Why on earth did you do that?
Why I cannot free them?
free() means the allocation is no longer a valid reference. Not that
the values it contained have been re-initialized in some way.
Sorry, I cannot post my huge program here.

Thanks for any comments

Mar 30 '06 #3
"questions? " <un************ @hotmail.com> writes:
I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing. Why I cannot
free them?


What makes you think you can't free them? What did you expect to
happen when you tried to access free()d memory?

The free() function, quoting the standard, "causes the space pointed
to by ptr to be deallocated, that is, made available for further
allocation." Once the space is deallocated, any attempt to access it
causes undefined behavior. This means that, as far as the standard is
concerned, literally anything can happen -- including the exact
behavior you happened to see.

In your case, what *probably* happened is that the system marked the
previously allocated memory as available for further allocation, but
didn't clobber the memory itself (there's no real reason why it
should). Physically, the memory is still there, and still contains
whatever information you stored in it. But there's no guarantee that
it will stay that way. Somethinge else *could* come along and store
values in that memory before you print it -- or your program could
return it to the operating system, so that any attempt to access it
again could cause a trap -- or demons could fly out of your nose.

When you call free(), you're telling the system that you're finished
with the memory. If you're trying to access it again, you obviously
were't finished with it. If you lie to the implementation, it will
get its revenge (possibly by letting you get away with something like
this).

Just don't do that.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 30 '06 #4
"Chad" <cd*****@gmail. com> writes:
questions? wrote:
I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing. Why I cannot
free them?

Sorry, I cannot post my huge program here.

Thanks for any comments


Sir,

This is comp.lang.c, not comp.lang.c.Iha veESPandCanRead YourCodeByMagic
forum. Please post the entire code.

Thank You


Actually, the question can be answered without seeing the code. See
my other response in this thread.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 30 '06 #5

Keith Thompson wrote:
"Chad" <cd*****@gmail. com> writes:
questions? wrote:
I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing. Why I cannot
free them?

Sorry, I cannot post my huge program here.

Thanks for any comments


Sir,

This is comp.lang.c, not comp.lang.c.Iha veESPandCanRead YourCodeByMagic
forum. Please post the entire code.

Thank You


Actually, the question can be answered without seeing the code. See
my other response in this thread.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Interesting. I think this has to be the first thread I got more insight
into using free() without actually seeing any kind of working code.

Chad

Mar 30 '06 #6

Keith Thompson wrote:
"questions? " <un************ @hotmail.com> writes:
I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing. Why I cannot
free them?


What makes you think you can't free them? What did you expect to
happen when you tried to access free()d memory?

The free() function, quoting the standard, "causes the space pointed
to by ptr to be deallocated, that is, made available for further
allocation." Once the space is deallocated, any attempt to access it
causes undefined behavior. This means that, as far as the standard is
concerned, literally anything can happen -- including the exact
behavior you happened to see.

In your case, what *probably* happened is that the system marked the
previously allocated memory as available for further allocation, but
didn't clobber the memory itself (there's no real reason why it
should). Physically, the memory is still there, and still contains
whatever information you stored in it. But there's no guarantee that
it will stay that way. Somethinge else *could* come along and store
values in that memory before you print it -- or your program could
return it to the operating system, so that any attempt to access it
again could cause a trap -- or demons could fly out of your nose.

When you call free(), you're telling the system that you're finished
with the memory. If you're trying to access it again, you obviously
were't finished with it. If you lie to the implementation, it will
get its revenge (possibly by letting you get away with something like
this).

Just don't do that.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Thanks for the nice comments.
Thank you all. I posted my code on another thread.

Mar 30 '06 #7
On 2006-03-30, Chad <cd*****@gmail. com> wrote:

Interesting. I think this has to be the first thread I got more insight
into using free() without actually seeing any kind of working code.

Chad


Since this thread was about something that happens under the covers,
what code could show anything in this threads context? It was as
simple as "dont access memory you have not legally got a handle to"
Mar 30 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4985
by: Thomas Schulz | last post by:
Any ideas how to check for free disc space from Python. Platform independent? Thanks for any suggestion. Thomas
4
2419
by: Lodewijk Smit | last post by:
Hi, In the C standard of 1999 additional mathematical functions are added. For example, beside the traditional double sin(double x); there are also the functions: float sinf(float x); long double sinl(long double x);
10
14052
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile file to use purify. However, my code is a rather simple single-source C program, and I didn't write a Makefile for it. I'm wondering if anybody can tell me which commands are to be entered at the Unix prompt to use purify. And, I don't know if...
9
5166
by: Alex Shirley | last post by:
Hi there I’m simply trying to check for a blank or empty value in a textbox on my webform. In this instance I do not want to use a requiredfieldvalidator, I want to use a customvalidator (as I have other code within the customvalidator which works). This won’t work (within servervalidate): If txtBox.Text.ToString = "" Then raise exception…..
21
2279
by: sanjaymeher | last post by:
Hi, Right now addDynamicMemory(char **ptr, int size) method can able to handle if input ptr is intitialized to NULL or something. But how to improve this method to handle uninitialized pointed even. Any Answer ?? Thanks, Sanjay
25
29760
by: pamelafluente | last post by:
Hi Guys, I have the following HTML code which is doing a GET to a page, say MyUrl.aspx : <body> <form name="form1" method="get" action="MyUrl.aspx" id="form1"> <input type="hidden" name="ClickedElement" id="Messenger" /> </form> </body>
10
22647
by: kevinliu23 | last post by:
HI, I am new to Python and wanted to know how to check for the remaining disk space on my Windows machine using Python? I was thinking of using the command line "dir" and trying to extract the output from there. But I'm not sure how to extract command line strings using Python either. Anyway help would be appreciated. :)
173
8152
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
55
3319
by: lovecreatesbea... | last post by:
Do you check all error conditions for all library calls in you code? Is it necessary to check all errors? Is it convenient to check all errors (or how to make code clean and readable with mass of non business logic related code block)? The following code from net-snmp library calls atoi five times but checks none. Do we code a wrapper my_atoi_with_error_check() around atoi and call this new one everywhere else? Is it a good practice...
0
9384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9212
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9973
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9779
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9645
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5247
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.