How to check if a structure is NULL in C or C ++

i has the following structure

typedef struct { char data1[10]; char data2[10]; AnotherStruct stData; }MyData; 

for some reason, developers prefer not to make stData as a pointer, so I have to live with this. my problem is how to check if stData element is empty? because if it is empty, I need to skip some things in my code.

any help is appreciated

+8
c ++ c data-structures
source share
6 answers

You need to somehow mark AnotherStruct stData empty.

  • First check (or double check) the documentation and comments related to AnotherStruct , perhaps ask those who did this, if available, to find out if there is an official way to do what you want.

  • Perhaps this structure has a pointer, and if it is a null pointer, the structure is empty. Or maybe there is a whole field where 0 or -1, or something might mean empty. Or even a logical field to mark it blank.

  • If this does not happen, perhaps you can add such a field or such an interpretation of a field.

  • If the above fails, add a boolean field to MyData to indicate whether stData empty.

  • You can also interpret some values ​​(for example, an empty string? Full byte 0xFF?) data1 and / or data2 , which means empty stData .

  • If you cannot change or rethink the contents of any of the structures, then you can put empty and non-empty elements in different containers (array, list, everything that you have). If MyData elements are allocated from the heap one by one, then this is essentially the same as a free list .

  • The change above, if you have empty and non-empty elements, all mixed in one container, then you may have another container with pointers or indexes for non-empty elements (or for empty elements, or whatever suits you). This has the added complication that you need to synchronize two containers, which may or may not be trivial.

+8
source share

if it is not a pointer, then the memory for the structure element will be allocated when creating the MyData object. When you define your structures, they all set to zero using calloc or memset, and then you can compare to 0

+1
source share

you can find some flag variable. e.g.

 struct AnotherStruct { bool valid; char aother_data1[10]; char aother_data1[10]; //... }; if (stData.valid==true){ //do something } 
+1
source share

I am in a similar fix as you (did). I have to package this structure and know that the exact number of bytes in the structure will help me serialize the structure. However, some structures are empty and, therefore, serialization does not allow to align the exact number of bytes.

Although it's already 3 years later, I found the following solution that worked for me:

 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 requested as is_empty<T>::value and is available at compile time.

 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 links:

+1
source share

Struct is a user-defined type, since int is built in the type.

 struct x; int y; 

First try to answer "How can you determine if your int is empty or not after the first declaration of it"

Regarding the solution: - Use your structure in this way if you want to know if its OR is initialized: -

 struct X { bool b; X() : b(false) {} }; 

set to true at initialization.

0
source share

I assume that defining the structure is part of some third-party functions / libraries where the third party may be someone inside your own company.

If the developers decided not to make the stData pointer, then there are reasons. They will have an idea of ​​how to express " stData empty", even if allowed to be empty. You should definitely try to find these semantics in the documentation or talk to them. Do not try to add your own semantics to a structure that has a specific purpose and semantics.

So, if there is a predefined way to express that part of the structure is empty, use this path. If it cannot be empty for the purpose for which it is intended, then do not try to make it empty. In short, do not use the class / structure so that it is not used. Instead, if you find yourself in a situation where you only have some of the data needed for "MyData" to make sense, then write your own structure "MyPartialData" to handle this situation and translate it into "MyData" as only you have everything you need and are ready to interact with a third-party API.

0
source share

All Articles