[英]Do I still need to set ptr to NULL if I use memset earlier?
For example:例如:
struct sth {
int t;
char *p;
struct sth *next;
}
And the init code:和初始化代码:
struct sth *data = malloc(sizeof(*data));
memset(data, 0, sizeof(*data));
data->t = 0; // A
data->p = NULL; // B
data->next = NULL; // C
Since I have used memset
, do I still need to write A, B, C
?既然用过
memset
,还需要写A, B, C
吗?
This is just a sample, I have a struct with lots of pointers...这只是一个示例,我有一个包含很多指针的结构......
" Do I still need to set
ptr
toNULL
if I usememset()
earlier? "“如果我之前使用
memset()
,是否还需要将ptr
设置为NULL
? ”
If you want your program to be fully standard-compliant, then yes, as memset()
set each byte to 0
, which is different to setting NULL
to a pointer.如果您希望您的程序完全符合标准,那么可以,因为
memset()
将每个字节设置为0
,这与将NULL
设置为指针不同。 (Assuming ptr
is equivalent to p
inside of the structure sth
). (假设
ptr
相当于结构sth
内部的p
)。
Quote from the current C standard ISO:IEC 9899:2018 (C18), Section 7.22.3.5/2 - The calloc function:引用当前 C 标准 ISO:IEC 9899:2018 (C18), Section 7.22.3.5/2 - the calloc function:
" The space is initialized to all bits zero. 302) "
"空间被初始化为所有位为零。302) "
" 302 - Note that this need not be the same as the representation of floating-point zero or a null pointer constant . "
“ 302 - 请注意,这不必与浮点零或 null 指针常量的表示相同。 ”
" Since I have used
memset()
, do I still need to write A, B, C" ?“既然我已经使用了
memset()
,我还需要写 A、B、C 吗?”
A.
is redundant, as soon as t
is not an object of floating-point type as those can have a floating-point zero value, which may not have all bits set to 0
. A.
是多余的,只要t
不是浮点类型的 object 因为它们可以具有浮点零值,它可能不会将所有位设置为0
。 If t
were fe of type float
or double
A.
would be useful and appropriate to be standard conform.如果
t
是float
型或double
精度型的 fe, A.
将是有用且适合符合标准的。
B.
and C.
B.
和C.
are appropriate, since according to the standard setting each byte to 0
does not necessarily also set the pointers to NULL
, if you explicitly want to have them assigned to NULL
(although it should on most systems).是合适的,因为根据标准将每个字节设置为
0
不一定也将指针设置为NULL
,如果您明确希望将它们分配给NULL
(尽管它应该在大多数系统上)。
Note that calloc()
might be more convenient and also faster in performance as it allocates memory and immediately initialize each bit of it to 0
:请注意,
calloc()
可能更方便,性能也更快,因为它分配 memory 并立即将它的每一位初始化为0
:
struct sth *data = calloc(sizeof(*data));
But again here, p
and next
do not need to be NULL
.但是在这里,
p
和next
不需要是NULL
。
A simpler version is:一个更简单的版本是:
struct sth *data = malloc(sizeof(*data));
*data = (struct sth){0};
Your compiler can optimize this to a calloc call if the platform has all-bits-zero for the struct members.如果平台的结构成员全为零,则编译器可以将其优化为 calloc 调用。
Setting the structure to all bits zero with memset()
effectively sets all integer members to 0
.使用
memset()
将结构设置为所有位为零有效地将所有 integer 成员设置为0
。 The C Standard specifies that other members will have a value which may or may not correspond to the effect of setting them to 0
manually. C 标准指定其他成员的值可能与手动设置为
0
的效果相对应,也可能不对应。 But on all current architectures it is the case, and it seems extremely unlikely that any new processor will depart from this convention in the future.但在所有当前架构上都是如此,而且未来任何新处理器似乎都不太可能背离这一惯例。 So in practice you are safe.
所以在实践中你是安全的。
Note also that you should use calloc()
to allocate a object initialized to all bits zero:另请注意,您应该使用
calloc()
分配一个初始化为所有位为零的 object:
struct sth *data = calloc(1, sizeof(*data));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.