Home > Back-end >  Free pointer times wrong
Free pointer times wrong

Time:12-21

Int main (int arg c, char * * argv)
{

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 free

CodePudding user response:

F_value into a dynamic array, can also use malloc application memory
Memory 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 free

CodePudding user response:

refer to 6th floor CHXCHXKKK response:
please keep in mind that the malloc and free must be come in pairs, use malloc, don't use free

Thank you, I know, I mean using the malloc, also complains, here,

CodePudding user response:

refer to fifth floor in smoke past 0819 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;

}


Thank you,,, can only use memcpy here?? I tried, it's slow, practice, I'm a bit big, the amount of data, there is no direct fp=f_value; The way?

CodePudding user response:

Fp=f_value can not, then you don't need to free,
Reason've said above, f_value is stack memory, stack exit automatically recycling, not free,
That is your dll_start function internal don't need to manage free, because dll_start don't know parameter memory pointer is pointing to a stack or heap memory, so no need to force free, free should be determined by the applicant for the memory to control yourself, dll_start don't need,
  • Related