Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 954
user2101801 Avatar asked Sep 18 '14 05:09

user2101801


People also ask

Can a struct be null in C?

3 answers. It is not possible to set a struct with NULL as it is declared. Fila f = NUll; error: invalid initializer. So cast% from% to NULL , which is not even a type in this code, or assign Fila to a primitive type variable is "wrong".

How do I check if a struct is empty?

1) To check if the structure is empty:fmt. Println( "It is an empty structure." ) fmt. Println( "It is not an empty structure." )

How do you check if a struct has been initialized in C?

The only way you could determine if a struct was initialized would be to check each element within it to see if it matched what you considered an initialized value for that element should be.

What is struct at null?

To answer your first question: Yes, <struct at NULL> means that the reference to the object (struct or class) is null. This message can appear with a dangling reference, as appears to be your case.


Video Answer


3 Answers

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.

like image 161
hyde Avatar answered Oct 13 '22 01:10

hyde


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
}
like image 20
thomas Avatar answered Oct 13 '22 01:10

thomas


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:

  • https://stackoverflow.com/a/4829036/1727678
  • http://www.boost.org/doc/libs/1_63_0/libs/type_traits/doc/html/boost_typetraits/reference/is_empty.html
like image 45
sirajissani Avatar answered Oct 12 '22 23:10

sirajissani