473,499 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Doubt related to free()

34 New Member
Hi All
I have a question related to free().

For example
A(char *buffer) call B(char *buffer)
B(char *buffer) in turn calls C(char *buffer)
C(char *buffer) in turn calls D(char *buffer)
D(char *buffer) in turn calls E(char *buffer)
and so on.........
I pass a pointer as a parameter from A()
A(char *buffer);
1) This buffer was freed somewhere in some function. (Deallocation could have done by B() or C() or D() or E() etc)
2) I did not set BUFFER=NULL after freeing up the buffer in any of the function.

Is there any way to know whether buffer was FREED or NOT when I return to A().
I believe BUFFER will be pointing to the location even if we free() the data.

Please let me know your response.

Thanks & Regards
Sathish Kumar
Jan 9 '09 #1
3 1185
JosAH
11,448 Recognized Expert MVP
Yep, the value of the buffer pointer isn't altered but it doesn't point to a valid memory region anymore; you have freed it so you can't use it anymore.

kind regards,

Jos
Jan 9 '09 #2
gpraghuram
1,275 Recognized Expert Top Contributor
Hi,
As Jos said it will be an invalid memory.
But if u want to find if there is a leak in code then go for Purify or boundschecker

raghu
Jan 9 '09 #3
Tassos Souris
152 New Member
You probably must get used to using pointer to pointers rather than pointers. For example when you have the head of a single linked list it is better to use that:
Expand|Select|Wrap|Line Numbers
  1. void freeList( struct List **head ){
  2.     // Do the free of all the nodes here
  3.  
  4.     // Set it safely to NULL
  5.     *head = NULL;
  6. }
  7.  
You must make it a rule that every free() is followed by a assignment to NULL:
Expand|Select|Wrap|Line Numbers
  1. free( ( void * )ptr );
  2. ptr = NULL;
  3.  
Jan 10 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
11301
by: RK | last post by:
Hi, In my application, I need to copy data from an Excel file into a SQL table. The article related to this can be found at http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B306572 ...
2
1752
by: Lee Garrington | last post by:
Hey there, Is there a function that returns the amount of RAM free for memory allocation? Is there a function that will return the amount of memory your program currently uses through memory...
5
1472
by: CViniciusM | last post by:
Hello, The output of the code below is: number = 10 number = 0 (or other number except 15) Why the output of the second 'number' is not 15? Why the 'number' address is not changed? Thanks...
8
3359
by: Chul Min Kim | last post by:
Hi, I got a BUS ERROR from one of my company's program. Let me briefly tell our environment. Machine : Sun E3500 (Ultra Sparc II 400Mhz CPU 4EA) OS : Solaris7 Compiler : Sun Workshop...
3
1249
by: orium69 | last post by:
Hi everyone! I have a doubt, when I free an alloced memory, how does the compiler know how many bytes I have alloced forward the pointer. eg: int *coiso; coiso=(int...
122
4150
by: ivan | last post by:
hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? thanks
13
1568
by: Kamui Shirow | last post by:
Hello! I will write a snippet to illustrate my doubt: snippet 1: void foo( int l ) { char* tmp; tmp = (char*) malloc( l * sizeof( char ) );
8
2016
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
11
2895
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
7128
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,...
0
7006
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...
0
7215
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...
0
7385
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...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
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 ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.