将空值分配给指针的地址
Assign null to the address of a pointer
如何在 free_x 函数中取消初始化 x?我必须这样做才能适应 API 方法。我可以很容易地取消初始化 x 只需将 null 分配给它,但我必须在 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 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.
我认为不更改 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;
}
有点棘手,但你可以做到
#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 free_x(void *param)
{
my_struct **p = param;
*p = NULL;
}
显然它仅在 c using 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
变量,那将是你的问题!
如何在 free_x 函数中取消初始化 x?我必须这样做才能适应 API 方法。我可以很容易地取消初始化 x 只需将 null 分配给它,但我必须在 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 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.
我认为不更改 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;
}
有点棘手,但你可以做到
#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 free_x(void *param)
{
my_struct **p = param;
*p = NULL;
}
显然它仅在 c using 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
变量,那将是你的问题!