C语言 如何检查初始化的结构是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30422539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an initialised struct is empty or not?
提问by user3484582
So I want to check if my struct is empty or not. I declare my struct as a variable but DO not allocate memory.
所以我想检查我的结构是否为空。我将结构声明为变量,但不分配内存。
text* t;
text* t;
Later on I want to check with an if statement whether my struct is empty or not.
if (t!=NULL)does not seem to work as thas an address. Also doing something like if (t->head != NULL)gives me a segfault as I haven't allocated any memory for the struct.
稍后我想用 if 语句检查我的结构是否为空。
if (t!=NULL)似乎不像t有地址那样工作。也做类似的事情if (t->head != NULL)给了我一个段错误,因为我没有为结构分配任何内存。
I don't want to mallocas soon as I declare t. Is there a way to check if my struct is empty?
我不想在malloc我声明的时候就这样做t。有没有办法检查我的结构是否为空?
Thanks in advance!
提前致谢!
回答by Paul R
Just make sure you initialise the pointer to NULL:
只要确保将指针初始化为NULL:
text* t = NULL;
then later you can mallocon demand, e.g.:
然后你可以malloc按需,例如:
if (t == NULL)
{
t = malloc(sizeof *t);
// NB: check for error here - malloc can fail!
}
t->head = foo;
回答by Vlad from Moscow
Simply initialize the pointer whan it is defined
只需在定义时初始化指针
text* t = NULL;
In this case you can check whether an object that should be pointed to by the pointer was allocated
在这种情况下,您可以检查是否分配了应该由指针指向的对象
if ( t != NULL ) { /* some action */ }
or you can write
或者你可以写
if ( t && t->head ) { /* some action */ }
Take into account that if a pointer is defined outside any function that is if it has the static storage duration then it initialized implicitly by the compiler to a null pointer constant.
考虑到如果一个指针是在任何函数之外定义的,如果它具有静态存储持续时间,那么它会被编译器隐式初始化为空指针常量。
回答by too honest for this site
You do notdeclare the struct, but a pointerto the struct. You even definethe pointer (bring it into life and reserve memory space for it).
您不声明结构,而是声明结构的指针。您甚至可以定义指针(使其生效并为其保留内存空间)。
To declare (and define) a variableof that struct (presumed it has been declaredsomewhere before with typedef):
要声明(并定义)该结构的变量(假设它之前已经用 typedef声明过):
text my_struct_var;
This will reserve the space at compile time. The struct is initialized with 0 by the run-time system before main() is called (this may also be done by the OS functions which load the program). This means all fields are set to 0.
这将在编译时保留空间。在调用 main() 之前,运行时系统将 struct 初始化为 0(这也可以由加载程序的 OS 函数完成)。这意味着所有字段都设置为 0。
If you stick to to pointer-approach, you first have to malloc()("memory allocate") the required space for such an object (or an array of such objects) and assign it to the pointer. This allocation is called "dynamic", as the meory is allocated at run-time under program control. Similar, it can be free()d by the program code after which it must neverbe accessed again.
如果您坚持使用指针方法,您首先必须malloc()(“内存分配”)此类对象(或此类对象的数组)所需的空间并将其分配给指针。这种分配称为“动态”,因为内存是在程序控制下在运行时分配的。类似地,它可以free()由程序代码 d 之后再也不能被访问。
The allocated memory is notinitialized, thus it willhave random data. Therefore, you cannot test for anything inside.
已分配的内存没有初始化,所以就会有随机数据。因此,您无法测试内部的任何内容。
A priciple problem with dynamic memory allocation is that it might fail (no momory available). Therefore, you always have to check the result of malloc()if a valid pointer has been returned:
动态内存分配的一个主要问题是它可能会失败(没有内存可用)。因此,您始终必须检查malloc()是否返回了有效指针的结果:
text *my_text_struct_ptr = malloc(sizeof(text));
if ( my_text_struct_ptr == NULL ) {
// no memory allocated treat correspondingly
// most basic reaction: exit(1) the program immediately
}
That might be what you call "the struct is empty". However, it is the pointerto the struct which is invalid("points to nowhere"). For some more advanced data structures such a null pointersometimes is a synptom ofthe structure bein empty (e.g. a list).
这可能就是您所说的“结构为空”。但是,指向无效结构的指针(“指向无处”)。对于一些更高级的数据结构,这样的空指针有时是结构为空(例如列表)的症状。
Further readings: about null pointers, declaration vs. definition, pointers and structs.
进一步阅读:关于空指针、声明与定义、指针和结构。

