简体   繁体   English

如何检查初始化的结构是否为空?

[英]How to check if an initialised struct is empty or not?

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;

Later on I want to check with an if statement whether my struct is empty or not. 稍后,我想用一个if语句检查我的结构是否为空。 if (t!=NULL) does not seem to work as t has an address. if (t!=NULL)似乎不起作用,因为t有一个地址。 Also doing something like if (t->head != NULL) gives me a segfault as I haven't allocated any memory for the struct. 还做类似if (t->head != NULL)给我一个段错误,因为我没有为该结构分配任何内存。

I don't want to malloc as soon as I declare t . 我不想在声明t立即进行malloc分配。 Is there a way to check if my struct is empty? 有没有办法检查我的结构是否为空?

Thanks in advance! 提前致谢!

Just make sure you initialise the pointer to NULL : 只要确保将指针初始化为NULL

text* t = NULL;

then later you can malloc on demand, eg: 然后,您可以按需malloc ,例如:

if (t == NULL)
{
    t = malloc(sizeof *t);
    // NB: check for error here - malloc can fail!
}
t->head = foo;

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. 考虑到如果在函数具有静态存储持续时间的任何函数之外定义了指针,则编译器会隐式将其初始化为空指针常量。

You do not declare the struct, but a pointer to the struct. 声明该结构,而是声明该结构的指针 You even define the pointer (bring it into life and reserve memory space for it). 您甚至可以定义指针(使指针生效并为其保留存储空间)。

To declare (and define) a variable of that struct (presumed it has been declared somewhere 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). 在调用main()之前,运行时系统会将结构初始化为0(这也可以由加载程序的OS函数来完成)。 This means all fields are set to 0. 这意味着所有字段都设置为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. 如果坚持使用指针方法,则首先必须malloc() (“内存分配”)此类对象(或此类对象的数组)所需的空间,并将其分配给指针。 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 never be accessed again. 类似地,它可以由程序代码free() d禁止,之后再也不能再次访问它。

The allocated memory is not initialized, thus it will have 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 pointer to the struct which is invalid ("points to nowhere"). 但是, 指向结构的指针 无效 (“指向无处”)。 For some more advanced data structures such a null pointer sometimes is a synptom ofthe structure bein empty (eg a list). 对于某些更高级的数据结构,此类空指针有时是该结构为空(例如列表)的症状。

Further readings: about null pointers, declaration vs. definition, pointers and structs. 进一步阅读:关于空指针,声明与定义,指针和结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM