1

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?
}
9
  • 5
    *param = NULL;.......?; Commented Dec 15, 2016 at 9:50
  • 2
    *param = NULL should work Commented Dec 15, 2016 at 9:51
  • The address of the pointer? Or the address that the pointer points to? Commented Dec 15, 2016 at 9:51
  • OT: The cast here *param = (my_struct *)&var; is useless. Just do *param = &var; Commented Dec 15, 2016 at 9:52
  • 1
    @sanchop22 Then you can't. If you want to alter what "the thing you pass into free_x points at" (i.e. set it to NULL) then you must pass in a pointer-to-a-pointer, like you do for alloc_x. Commented Dec 15, 2016 at 10:10

4 Answers 4

2

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!

Sign up to request clarification or add additional context in comments.

2 Comments

You are answering a question that is not the asked one. Moreover the question struct my_struct has an internal pointer so, can be easily guessed that other mallocated memories must be freed in free_x function.
+1 Bang on target. I agree, we don't need to do anything in this case. If the caller uses x after free_x(..) its not your problem
0

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.

1 Comment

When I write *param = NULL, I receive ''expression must be a modifiable lvalue" error!
0

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;
}

4 Comments

Exactly but I must not use malloc and free because we are working on a microcontroller and it is not allowed to use dynamic memory allocations even though it is possible.
In that case may I ask why you need alloc_x and free_x at all??. Instead of function why not use a MACRO. like #define free_x(param) param = NULL
Because I have to implement free_x function in order to fit to an API function. I must have free_x(void *param) function and it is supposed to deinitialize x inside the function.
I don't think you can deinitialize x inside free_x by passing it as *, better use MACRO's if thats the only requirement you have
0

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 using void *

4 Comments

This would destroy contents of var variable as well. Not sure if this is intended.
This is not good. Code that tries to change private values in another piece of code is either a bug by design or one waiting to happen.
@A.N I'm not getting you
@LPs Yes, when you are passed a pointer to a pointer, as is the case in the alloc_x function. The fact that only the pointer is passed to the free_x function indicates that the caller's copy (i.e. the original x variable) is not intended to be modified. If x were meant to be modified it would indeed have been passed as a pointer to a pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.