简体   繁体   中英

Is there a fast way to check if all attributes inside an initialized struct are uninitialized?

Consider the following struct:

typedef struct {
    int id;
    int *key;
    char *message;
    char ret;
} some_type;

and inside my code I have now:

some_type *a = (some_type *) malloc(sizeof(some_type));

I initialized the struct but not its attributes yet. Is there a simple way to retrieve that kind of information? I want a function which gives me true if all attributes of an initialized struct are uninitialized.

EDIT: I am doing this because I want to realize a specific functionality in a getopt in my program. Arguments that are passed over command line by the user are collected in this struct. If no arguments except an "-h" is passed, I want to print out a help section. Thus, I thought I could use such a function to check if nothing else is initialized inside my struct. If this is not possible, can somebody else think of a short way to achieve my target?

I was using CRC for every data structure when I was programming uCs working in the radioactive environment. Use calloc, add CRC and probability that data is wrong will be minimized

edit

struct data {
    uint8_t a;
};

typedef struct
{
    struct data d;
    uint8_t _padding[4 * (sizeof(struct data) & 3) - (sizeof(struct data) & 3)];
    uint32_t CRC;
} __attribute__((packed)) data1;

First, we're going to have some repetition of types, so remove the gratuitous and actively harmful one:

some_type *a = (some_type *) malloc(sizeof(some_type));

becomes:

some_type *a = malloc(sizeof *a);

Now, there's no way to "probe" whether particular/all members are initialized, but you can at least ensure that everything not explicitly initialized gets a default initializeation, by assigning an initialized struct to yours:

*a = (some_type){ .id = x, .key = y }; // message and ret have default initializers

This isn't quite as good as what you wanted, but better than nothing.

If you don't just want to help catch mistaken use of uninitialized data, but actually have a long-term need to know validity status, then dbush's answer is for you. You need either a separate member indicating validity flags for the optional members, or you need sentinel values (like null pointers, negative integers, etc.) which you will interpret as "lacking any value", and you need to set them up (which requires doing each individually, whereas a boolean flag could work with my default-initializer approach above).

In general, there's no way to know whether a variable is initialized or not. For memory returned by malloc , all memory is uninitialized.

You need some way of keeping track of that yourself, either by assigning some known default values or separate flag variables.

Regarding command line arguments passed to your program, each of them should have a default value. Then if particular arguments contain the default value then you can display your help message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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