用于解除分配和将指针设置为NULL的通用函数 [英] Generalized function for deallocating and setting pointer to NULL

查看:77
本文介绍了用于解除分配和将指针设置为NULL的通用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写一个能够检查指针

值是否设置为NULL的函数,如果它不是,则取消分配并设置

无论指针的类型如何,它的值都为NULL。我想无效

**应该做的伎俩,但我一直收到以下警告
来自gcc的
消息:


警告:从不兼容的指针传递''free_var''的参数1

类型


显然可以通过在参数列表中对(void **)

进行显式转换来解决调用功能。然而,这会给程序员带来额外的负担,所以我发明了以下

解决方案,显然效果很好。请在这个问题上给我你的

意见。

void free_var(void * vptr)

{

void ** ptr =(void **)vptr;


if(ptr!= NULL){

if(* ptr!= NULL) ){

免费(* ptr);

* ptr = NULL;

}

}

}


谢谢。

I''ve been trying to write a function capable of checking if a pointer
value is set to NULL, and if it isn''t, of deallocating and setting
it''s value to NULL regardless of the pointer''s type. I thought "void
** " should do the trick, but I kept getting the following warning
message from gcc:

warning: passing argument 1 of ''free_var'' from incompatible pointer
type

which obviously could be solved by doing an explicit cast to (void **)
in the argument list when calling the fuction. That however would put
an extra burden on the programmer, so I invented the following
solution which works quite well apparently. Please give me your
opinions on this issue.

void free_var(void *vptr)
{
void **ptr = (void **) vptr;

if (ptr != NULL) {
if (*ptr != NULL) {
free(*ptr);
*ptr = NULL;
}
}
}

Thank you.

推荐答案

ro ********* @ gmail.com 写道:
ro*********@gmail.com wrote:

我一直在尝试编写一个函数,能够检查指针是否设置为NULL,如果不是,则取消分配并设置

无论指针的类型如何,它的值都为NULL。我想无效

**应该做的伎俩,但我一直收到以下警告
来自gcc的
消息:


警告:从不兼容的指针传递''free_var''的参数1

类型
I''ve been trying to write a function capable of checking if a pointer
value is set to NULL, and if it isn''t, of deallocating and setting
it''s value to NULL regardless of the pointer''s type. I thought "void
** " should do the trick, but I kept getting the following warning
message from gcc:

warning: passing argument 1 of ''free_var'' from incompatible pointer
type



这是因为在C中唯一能指向任何

类型的指针类型数据是无效*,而不是空白**。 void **只能指向一个

void *数据类型。

That''s because in C the only pointer type capable of pointing to any
type of data is the void *, not a void **. void ** can only point to a
void * data type.


显然可以通过显式转换来解决(void **)
which obviously could be solved by doing an explicit cast to (void **)



未解决,仅仅被抑制。

Not solved, merely suppressed.


在参数列表中调用函数时。然而,这会给程序员带来额外的负担,所以我发明了以下

解决方案,显然效果很好。请在这个问题上给我你的

意见。

void free_var(void * vptr)

{

void ** ptr =(void **)vptr;


if(ptr!= NULL){

if(* ptr!= NULL) ){

免费(* ptr);

* ptr = NULL;

}

}

}
in the argument list when calling the fuction. That however would put
an extra burden on the programmer, so I invented the following
solution which works quite well apparently. Please give me your
opinions on this issue.

void free_var(void *vptr)
{
void **ptr = (void **) vptr;

if (ptr != NULL) {
if (*ptr != NULL) {
free(*ptr);
*ptr = NULL;
}
}
}



C'的值传递约定意味着此函数实际上并不是
对调用者进行操作' 's''vptr'',只是这个函数'的本地化副本。

所以调用者'''vptr''在调用此函数后保持不确定

功能。

C''s pass by value convention means that this function does not actually
operate on the caller''s ''vptr'', merely this function''s localised copy.
So the caller''s ''vptr'' is left indeterminate after a call to this
function.


rocco.rossi:
rocco.rossi:

我一直在尝试编写一个能够检查指针

值是否设置为NULL的函数,如果它不是,则取消分配并将其设置为'
值为NULL无论指针的类型如何。我认为无效**

应该可以解决这个问题,但我一直收到以下警告信息
来自gcc的

I''ve been trying to write a function capable of checking if a pointer
value is set to NULL, and if it isn''t, of deallocating and setting it''s
value to NULL regardless of the pointer''s type. I thought "void ** "
should do the trick, but I kept getting the following warning message
from gcc:



void DeallocateAndNullify(void ** const pp)

{

free(* pp);


* pp = 0;

}


将空指针传递给free无效,因此无需检查

它为null。


您可以使用宏来假装C有通过引用传递,但是我

不会建议它 - 因为一个C程序员假设他们的对象不会被改变,除非他们通过它的地址。


-

汤姆?s ?? h ?? ilidhe


void DeallocateAndNullify(void **const pp)
{
free (*pp);

*pp = 0;
}

Passing a null pointer to free has no effect, so there''s no need to check
it for null.

You could use macros to pretend that C has "pass by reference", but I
wouldn''t suggest it -- because a C programmer assumes their object won''t
get altered unless they pass its address.

--
Tom??s ?? h??ilidhe


ro ******* **@gmail.com 写道:

我一直在尝试编写一个能够检查是否有指针的函数

值设置为NULL,如果不是,则取消分配并设置

,无论指针的类型如何,它的值都为NULL。我想无效

**应该做的伎俩,但我一直收到以下警告
来自gcc的
消息:


警告:从不兼容的指针传递''free_var''的参数1

类型


显然可以通过在参数列表中对(void **)

进行显式转换来解决调用功能。然而,这会给程序员带来额外的负担,所以我发明了以下

解决方案,显然效果很好。请在这个问题上给我你的

意见。

void free_var(void * vptr)

{

void ** ptr =(void **)vptr;


if(ptr!= NULL){

if(* ptr!= NULL) ){

免费(* ptr);

* ptr = NULL;

}

}

}
I''ve been trying to write a function capable of checking if a pointer
value is set to NULL, and if it isn''t, of deallocating and setting
it''s value to NULL regardless of the pointer''s type. I thought "void
** " should do the trick, but I kept getting the following warning
message from gcc:

warning: passing argument 1 of ''free_var'' from incompatible pointer
type

which obviously could be solved by doing an explicit cast to (void **)
in the argument list when calling the fuction. That however would put
an extra burden on the programmer, so I invented the following
solution which works quite well apparently. Please give me your
opinions on this issue.

void free_var(void *vptr)
{
void **ptr = (void **) vptr;

if (ptr != NULL) {
if (*ptr != NULL) {
free(*ptr);
*ptr = NULL;
}
}
}



如果按如下方式使用,该方法将起作用:


void * vp = malloc (42);

free_var(& vp);


但是,不能保证malloc的更典型用法有效:


int * ip = malloc(42 * sizeof(int));

free(& ip); //错误


这是因为只有当指向指针实际上具有类型void *时,free_var中的* ptr表达式才定义了行为

。在许多

实现中,所有指针都具有相同的表示形式,因此这个转换

恰好起作用。但是,该标准允许每个指针类型具有

它自己的表示(这里有一些不相关的例外)。


void * type在C中允许一定量的通用性,但不足以将这个想法作为C函数实现。对于这种

通用性,你需要一个宏:


#define FREE_VAR(p)(free(p),(p)= NULL)

That method will work if you use it as follows:

void *vp = malloc(42);
free_var(&vp);

However, the more typical use of malloc is not guaranteed to work:

int *ip = malloc(42*sizeof(int));
free(&ip); // WRONG

This is because the *ptr expression in free_var has defined behavior
only if the pointed-at pointer actually has the type void*. On many
implementations, all pointers have the same representation, so this cast
happens to work. However, the standard allows each pointer type to have
it''s own representation (with certain exceptions that aren''t relevant here).

The void* type allows a certain amount of genericity in C, but not
enough to implement this idea as a C function. For this kind of
genericity, you need a macro:

#define FREE_VAR(p) (free(p), (p)=NULL)


这篇关于用于解除分配和将指针设置为NULL的通用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆