C 将null赋值给指针的地址

C 将null赋值给指针的地址,c,pointers,struct,free,pointer-to-pointer,C,Pointers,Struct,Free,Pointer To Pointer,如何在free_x函数中取消初始化x?为了适应API方法,我必须这样做。我可以很容易地通过给x赋值null来取消初始化x,但是我必须在free_x函数中进行 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

如何在free_x函数中取消初始化x?为了适应API方法,我必须这样做。我可以很容易地通过给x赋值null来取消初始化x,但是我必须在free_x函数中进行

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

只需写入
*param=NULL

malloc返回void*,free接受void*,因此您的一些强制转换是 毫无意义,你总是在释放空虚,即使你 从其他类型的指针开始


我认为不改变alloc_x函数是不可能的。下面给出了一种可能的实现方式:

#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;
}
#包括
#包括
#包括
类型定义结构
{
int field1;
无效*字段2;
}我的结构;
静态myu结构变量;
无效分配x(无效**参数)
{
*param=malloc(sizeof(my_struct*));
memcpy(*param,(my_struct*)和var,sizeof(my_struct*);
}
无效自由_x(无效*参数)
{
自由(param);
//我怎样才能在这里释放param?
}
内部主(空)
{
void*x;
alloc_x(&x);
free_x(x);//x=NULL有效,但不允许
返回0;
}

有点麻烦,但你可以做到

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

显然,它只有在使用
void*
时才有效。简单的回答是:您的代码已经完成了,所以不要再做了

说明:您不分配内存(在堆、堆栈或其他地方),因此没有任何可用内存。您不拥有任何必须返回的资源、设置任何需要清除的标志或增加任何需要减少的信号量等

您正在实现一个API,但仅仅因为有一个函数原型,并不意味着您的实现必须做任何事情(如果不需要的话)。只需更改注释以说明无需执行任何操作,并将函数留空即可

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

使用API的代码希望
param
x
指针后面的内存在调用后已被释放,因此它不应该在调用后对其变量执行任何操作。如果他们这样做了,这不是你的问题,但是如果你去摆弄调用方的
x
变量,那将是你的问题

*param=NULL*param=NULL是否应该指定指针的地址?或者指针指向的地址?OT:the cast here
*param=(my_struct*)&var是无用的。只需执行
*param=&var@sanchop22那么你不能。如果您想更改“传递到
free_x
的对象所指向的内容”(即,将其设置为
NULL
),则必须传入指针指向指针,就像对
alloc_x
所做的那样。当我写入*param=NULL时,我接收到的“表达式必须是可修改的左值”“错!确实如此,但我不能使用malloc和free,因为我们正在使用微控制器,即使可能也不允许使用动态内存分配。在这种情况下,我可以问一下,为什么您需要alloc_x和free_x??。为什么不使用宏来代替函数呢。比如#define free_x(param)param=null,因为我必须实现free_x函数才能适应API函数。我必须有free_x(void*param)函数,它应该在函数中反初始化x。我不认为你可以通过将它作为*传递来反初始化free_x中的x,最好使用宏,如果这是你唯一的要求,这也会破坏var变量的内容。不确定这是否是有意的。这不好。试图更改另一段代码中的私有值的代码可能是设计上的错误,也可能是等待发生的错误。@a.N我不明白you@LPs是,当您被传递一个指向指针的指针时,就像
alloc_x
函数中的情况一样。只有指针被传递到
free_x
函数这一事实表明调用方的副本(即原始
x
变量)不需要修改。如果要修改
x
,它确实会作为指向指针的指针传递。您回答的问题不是所问的问题。此外,问题
struct my_struct
有一个内部指针,因此很容易猜测其他
malloc
ated内存必须是
free_x
函数中的
free
d。+1砰的一声击中目标。我同意,在这种情况下我们不需要做任何事情。如果调用方在free_x(..)之后使用x,这不是您的问题
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.
}