如何在 C 或 C++ 中检查结构体是否为 NULL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25904993/
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 a struct is NULL in C or C++
提问by user2101801
i have the following structure
我有以下结构
typedef struct
{
char data1[10];
char data2[10];
AnotherStruct stData;
}MyData;
for some reason the implementors choose not to make the stData as pointer, so i have to live with that. my problem is how do i check if the stData member is empty? because if it is empty i need to skip certain things in my code.
出于某种原因,实现者选择不将 stData 作为指针,所以我必须接受这一点。我的问题是如何检查 stData 成员是否为空?因为如果它是空的,我需要跳过代码中的某些内容。
any help is appreciated
任何帮助表示赞赏
采纳答案by hyde
You need some way to mark AnotherStruct stData
empty.
您需要某种方法来标记为AnotherStruct stData
空。
First check (or double-check) the documentation and comments related to
AnotherStruct
, possibly ask those who made it if they are available, to find out if there is an official way to do what you want.Perhaps that struct has a pointer, and if it is null pointer, the struct is empty. Or perhaps there is an integer field where 0 or -1 or something can mean empty. Or even a boolean field to mark it empty.
If there aren't any of the above, perhaps you can add such a field, or such an interpretation of some field.
If above fails, add a boolean field to
MyData
to tell ifstData
is empty.You can also interpret some values (like, empty string? Full of 0xFF byte?) of
data1
and/ordata2
meaning emptystData
.If you can't modify or reinterpret contents of either struct, then you could put empty and non-empty items in different containers (array, list, whatever you have). If
MyData
items are allocated from heap one-by-one, then this is essentially same as having a free list.Variation of above, if you have empty and non-empty items all mixed up in one container, then you could have another container with pointersor indexes to the the non-empty items (or to the empty items, or whatever fits your need). This has the extra complication, that you need to keep two containers in sync, which may or may not be trivial.
首先检查(或仔细检查)与 相关的文档和评论
AnotherStruct
,可能询问制作它的人是否可用,以了解是否有官方的方式来做你想做的事。也许那个结构体有一个指针,如果它是空指针,则结构体是空的。或者可能有一个整数字段,其中 0 或 -1 或其他值可能意味着空。或者甚至是一个布尔字段来将其标记为空。
如果以上都没有,也许你可以添加这样一个字段,或者对某个字段进行这样的解释。
如果以上失败,添加一个布尔字段
MyData
来判断是否stData
为空。您还可以解释一些值(例如,空字符串?充满 0xFF 字节?)的
data1
和/或data2
表示空的stData
。如果您无法修改或重新解释任一结构的内容,那么您可以将空和非空项目放在不同的容器(数组、列表,无论您拥有什么)中。如果
MyData
项目是从堆中一一分配的,那么这本质上与拥有空闲列表相同。上面的变化,如果您在一个容器中混合了空和非空项目,那么您可以拥有另一个容器,其中包含指向非空项目(或空项目,或任何适合您的需要)的指针或索引. 这有额外的复杂性,您需要保持两个容器同步,这可能是也可能不是微不足道的。
回答by Rajath N R
if it not a pointer then memory for structure member will be allocated when object MyData is created.When you define your structures set them all to zero with calloc or memset, then later you can compare to 0
如果它不是指针,则在创建对象 MyData 时将分配结构成员的内存。当您定义结构时,使用 calloc 或 memset 将它们全部设置为零,然后您可以与 0 进行比较
回答by thomas
you can find some flag variable. ex.
你可以找到一些标志变量。前任。
struct AnotherStruct {
bool valid;
char aother_data1[10];
char aother_data1[10];
//...
};
if (stData.valid==true){
//do something
}
回答by sirajissani
I find myself in a similar fix as you (did). I am required to packetize a given structure and knowing the exact number of bytes in the structure would help me serialize the structure. However, some structures are empty and hence, serialization fails to align exact number of bytes.
我发现自己和你一样(做了)。我需要对给定的结构进行打包,并且知道该结构中的确切字节数将有助于我对结构进行序列化。但是,某些结构是空的,因此序列化无法对齐确切的字节数。
Although this is 3 years later, I found the following solution that worked for me:
虽然这是 3 年后,但我发现以下解决方案对我有用:
template <typename T> struct is_empty {
struct _checker: public T { uint8_t dummy; };
static bool const value = sizeof(_checker) == sizeof(T);
};
The result can be be queried as is_empty<T>::value
and is available at compile time.
结果可以被查询,is_empty<T>::value
并且在编译时可用。
template <typename S>
typedef union {
struct __attribute__((__packed__)) {
ObjId id;
S obj;
} dataStruct;
std::array<uint8_t, (sizeof(dataStruct) - is_empty<S>::value)> byteArray;
} _msg_t;
Here are the references:
以下是参考资料:
回答by ravi
Struct is user-defined type as int is built in type.
Struct 是用户定义的类型,因为 int 是内置类型。
struct x;
int y;
First try to answer "How can you determine if your int is empty OR not after first declaring it"
首先尝试回答“你如何确定你的 int 是空的还是在第一次声明之后不是”
Regarding solution:- Use your struct this way if you want to know whether its initialized OR not:-
关于解决方案:- 如果您想知道它是否已初始化,请以这种方式使用您的结构:-
struct X
{
bool b;
X() : b(false) {}
};
set it true when initialized.
初始化时设置为真。
回答by Arne Mertz
I assume the struct definition is part of some third party functionality/library, where the third party may well be someone inside your own company.
我假设结构定义是某些第三方功能/库的一部分,其中第三方很可能是您公司内部的某个人。
If the implementors chose to not make stData
a pointer, then there are reasons. They will have an idea about how to express "stData
is empty", if it is even allowed to be empty. You should definitely try to look up those semantics in the documentation or talk to them. Don't try to add your own semantics to a structure that has a specific purpose and semantics.
如果实现者选择不制作stData
指针,那是有原因的。他们会知道如何表达“stData
是空的”,如果它甚至被允许是空的。您绝对应该尝试在文档中查找这些语义或与他们交谈。不要尝试将您自己的语义添加到具有特定目的和语义的结构中。
So if there isa predefined way to express that part of the struct is empty, use that way. If it may not be empty for the uses it is intended for, then don't try to make it empty. In a nutshell, don't use a class/struct in a way it is not meant to be used. Instead, if you find yourself in a situation where you only have part of the data that is needed for the 'MyData' to make sense, then write your own 'MyPartialData' struct to deal with that situation and translate it to a 'MyData' once you have everything needed and are ready to interact with the third party API.
所以,如果有是表达结构的一部分预先定义的方式是空的,使用这种方式。如果它的用途可能不是空的,那么不要试图让它空。简而言之,不要以不应该使用的方式使用类/结构。相反,如果您发现自己只有“MyData”所需的部分数据才能有意义,那么请编写您自己的“MyPartialData”结构来处理这种情况并将其转换为“MyData”一旦您拥有所需的一切并准备好与第三方 API 交互。