file ----%26gt;input.h (contents of input.h)
float * datain();
file ----%26gt;input.c(contents of input.c)
#include %26lt;malloc.h%26gt;
//.....other includes
float *datain()
{
float *data;
//routine to get data........
return(data);
free (data) ///????????
}
assuming I have a main.c function in the same project which calls datain()...is it legal operation to free the pointer after returning the pointer to the main function? even though I need to do some thing to the returned data?
file----------%26gt;main.c
#include %26lt;malloc.h%26gt; ....//other includes
#include "input.h"
void main ()
{
float *getdata;
getdata = datain();
// .....do something to get data;
}
Thanks in advance
C code question regarding malloc() and free().....?
statements after return statement are not executed.
so basically, your function does not execute the line
%26gt; free(data)
instead of that, you could define a structure and two functions,
typedef struct {
float * data;
int valid;
} datastruct;
datastruct get_data(){
float *tmp;
// malloc(tmp)
// copy data to memory pointed by tmp
datastruct result = {tmp, 1};
}
float free_data(datastruct *pdata){
if (pdata -%26gt; valid){
pdata -%26gt; valid = 0;
free (pdata -%26gt; data);
}
}
==================
and inside your main function:
void main(){
datastruct data;
data = get_data();
// do smth to data
free_data(%26amp;data);
}
Reply:you're right, the struct is not really needed. Report It
Reply:Actually in C different memory space will be alloted for the functions so whenever u pass a data the value will be passed but when it is a pointer it only passes the address of the pointer so if u pass a pointer from one function to another the address of the pointer will be passed.
If u want to free a pointer the u need to pass the data value instead of using address of the pointer
ie instead of return(data); use return(*data); It passes the data value so when u free the location u dont have any problem
If u free the pointer only the pointer will be freed ie if u free data then it will be deleted (the location having the address of original data )as u have already passed the address if u free there is no problem .if u free first the original address of data will be lost thus the data becomes inaccessible .
I think i have atleast solved some of your problems
Reply:I really didn't use C before ... but i know the pointers concept.
I think there's no problem if u make the pointer "data" free after returning the data
even if u need to do things to the returned data in the main.
----
*u've pointer "data" in dataIn() and pointer "getData" in the main,
*what is really done here is that u recieved some DATA
*the DATA now is in the memory with address "150" for example
*u put the address "150" in the pointer "data"
*u returned the pointer value to the pointer "getData"
*then "getData" now contains the address "150"
i don't know the function "free" but,
when u free(data):
-if that means u make the pointer itself free and not points any DATA, so u still have the pointer "getData" points to the same data.
-if it means u'll delete the actual DATA pointed by the pointer "data", then that's wrong
but it seems like the 1st meaning is right
----------------
another thing :
as i said b4, i didn't use C before ... but in Java, the statements after "return" aren't reachable as the function ends at the word "return", i don't know if it's the same thing in C or not
Reply:[747] cmalloc: cat main1.c
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
float *dataIn( void )
{
float *dataPtr = malloc( sizeof(float) );
*dataPtr = 1.2345;
return dataPtr;
}
int main( int argc, char* argv[] )
{
float *myDataPtr = dataIn();
printf( "myDataPtr: %p\n", myDataPtr );
printf( "*myDataPtr: %f\n", *myDataPtr );
free( myDataPtr );
return 0;
}
[748] cmalloc: gcc -Wall main1.c
[749] cmalloc:
[749] cmalloc: ./a.out
myDataPtr: 0x3000f0
*myDataPtr: 1.234500
The dataIn() function allocates the memory, fills it in, and returns a pointer to the data. The main() function gets a pointer to the data from the dataIn() function. The ownership of the allocated memory passes from the dataIn() function to main(). The main() does whatever it wants with the data and then frees the memory.
You must NOT free the memory until all the program is completely done using the data stored within the memory.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment