'How to check if a struct is NULL in C or C++

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.

any help is appreciated



Solution 1:[1]

You need some way to mark AnotherStruct stData empty.

  • 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 if stData is empty.

  • You can also interpret some values (like, empty string? Full of 0xFF byte?) of data1 and/or data2 meaning empty stData.

  • 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 pointers or 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.

Solution 2:[2]

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:

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.

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:

Solution 3:[3]

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
}

Solution 4:[4]

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

Solution 5:[5]

Struct is user-defined type as int is built in type.

struct x;
int y;

First try to answer "How can you determine if your int is empty OR not after first declaring it"

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.

Solution 6:[6]

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.

So if there is a 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Community
Solution 3 thomas
Solution 4 Rajath N R
Solution 5 ravi
Solution 6 Arne Mertz