{
Float f_value []={1, 2, 3, 4, 5};
Float * fp=NULL;
//fp=(float *) malloc (30 * sizeof (float).
Fp=f_value;
Free (fp);
return 0;
}
This code in any add do not add the malloc, free will be an error, when I know fp in fp=f_value; When the memory is not the original memory, but my goal was to free (fp), have what good way? (here is just a demo, f_value is a function in actual applications and passed to the fp)
CodePudding user response:
Free can only release the memory malloc opening, f_value is stack variables, there is no freeCodePudding user response:
F_value into a dynamic array, can also use malloc application memoryMemory array is fixed, in the stack, the function after the end of stack will automatically be recycled, so you can't free memory on the stack,
Is dynamically allocated memory on the heap and stack it doesn't matter, stack exit will not automatically recycling, so we need to free,
CodePudding user response:
That can't free first, then the assignment?CodePudding user response:
Thank you! ,,, f_value if is pass?Int Dll_Start (float * f_value) {
Float * fp=NULL;
//fp=(float *) malloc (30 * sizeof (float).
Fp=f_value;
Free (fp);
return 0;
}
CodePudding user response:
Do you want the f_value assigned to the content of the fp (malloc allocation space)
Int main (int arg c, char * * argv)
{
Float f_value []={1, 2, 3, 4, 5};
Float * fp=NULL;
Fp=(float *) malloc (30 * sizeof (float).
If (fp!=NULL)
{
Memcpy (fp, & amp; F_value [0], sizeof (f_value));
}
The else
{
/* the Error process... */
}
Free (fp);
return 0;
}
CodePudding user response:
Please keep in mind that the malloc and free must be come in pairs, use malloc, don't use freeCodePudding user response: