Reputation: 2809
How can I deinitialize x inside the free_x function? I have to do it in order to fit to an API method. I can very easily deinitialize x just by assigning null to it but I have to do it inside the free_x function.
typedef struct
{
int field1;
void *field2;
}my_struct;
static my_struct var;
int main(void)
{
void *x;
alloc_x(&x);
free_x(x); // x = NULL works but not allowed
return 0;
}
void alloc_x(void **param)
{
*param = (my_struct *)&var;
}
void free_x(void *param)
{
// how can I free param here?
}
Upvotes: 1
Views: 1670
Reputation: 2320
Simple answer: your code is already complete, so do no more.
Explanation: You do not allocate memory (on a heap or stack or elsewhere) so there is nothing to free. You do not take ownership of any resources that have to be returned, of set any flags that need to be cleared, or increment any semaphores that need to be decremented, etc.
You are implementing an API, but just because there is a function prototype that does not mean your implementation has to do anything, if it doesn't need to. Just change the comment to explain that there is nothing to do and leave the function empty.
void alloc_x(void **param)
{
*param = &var; // No need to cast here, just assign.
}
void free_x(void *param)
{
// Nothing allocated, taken, incremented, etc. in alloc_x, so ...
// nothing to free, release, decrement, etc. here in free_x.
}
The code that is using the API is expecting the memory behind the param
or x
pointer to have been freed after the call, so it shouldn't be doing anything with its variable afterwards anyway. It's not your problem if they do, but it will be your problem if you go diddling round with the caller's x
variable!
Upvotes: 2
Reputation: 16213
A little bit triky, but you can do
#include <stdio.h>
#include <string.h>
typedef struct
{
int field1;
void *field2;
}my_struct;
static my_struct var;
void alloc_x(void **param)
{
*param = (my_struct *)&var;
}
void free_x(void *param)
{
memset(param, 0x00, sizeof(void *));
}
int main(void)
{
void *x;
alloc_x(&x);
printf("Before: x=%p\n", x);
free_x(&x);
printf("After: x=%p\n", x);
return 0;
}
or
void free_x(void *param)
{
my_struct **p = param;
*p = NULL;
}
Obviously it is valid only in c using void *
Upvotes: 0
Reputation: 551
I don't think its possible without changing the alloc_x function. One possible implementation is given below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int field1;
void *field2;
}my_struct;
static my_struct var;
void alloc_x(void **param)
{
*param = malloc(sizeof(my_struct *));
memcpy(*param,(my_struct *)&var,sizeof(my_struct *));
}
void free_x(void *param)
{
free(param);
// how can I free param here?
}
int main(void)
{
void *x;
alloc_x(&x);
free_x(x); // x = NULL works but not allowed
return 0;
}
Upvotes: 0
Reputation: 1374
just write *param = NULL;
malloc returns void * and free takes void *, so some of your casts are meaningless, and you're always freeing a void * even if you're starting with some other sort of pointer.
Upvotes: 0