Initialize/reset struct to zero/null

CStructInitialization

C Problem Overview


struct x {
    char a[10];
    char b[20];
    int i;
    char *c;
    char *d[10];
};

I am filling this struct and then using the values. On the next iteration, I want to reset all the fields to 0 or null before I start reusing it.

How can I do that? Can I use memset or I have to go through all the members and then do it individually?

C Solutions


Solution 1 - C

Define a const static instance of the struct with the initial values and then simply assign this value to your variable whenever you want to reset it.

For example:

static const struct x EmptyStruct;

Here I am relying on static initialization to set my initial values, but you could use a struct initializer if you want different initial values.

Then, each time round the loop you can write:

myStructVariable = EmptyStruct;

Solution 2 - C

The way to do such a thing when you have modern C (C99) is to use a compound literal.

a = (const struct x){ 0 };

This is somewhat similar to David's solution, only that you don't have to worry to declare an the empty structure or whether to declare it static. If you use the const as I did, the compiler is free to allocate the compound literal statically in read-only storage if appropriate.

Solution 3 - C

Better than all above is ever to use Standard C specification for struct initialization:

struct StructType structVar = {0};

Here are all bits zero (ever).

Solution 4 - C

In C, it is a common idiom to zero out the memory for a struct using memset:

struct x myStruct;
memset(&myStruct, 0, sizeof(myStruct));

Technically speaking, I don't believe that this is portable because it assumes that the NULL pointer on a machine is represented by the integer value 0, but it's used widely because on most machines this is the case.

If you move from C to C++, be careful not to use this technique on every object. C++ only makes this legal on objects with no member functions and no inheritance.

Solution 5 - C

If you have a C99 compliant compiler, you can use

mystruct = (struct x){0};

otherwise you should do what David Heffernan wrote, i.e. declare:

struct x empty = {0};

And in the loop:

mystruct = empty;

Solution 6 - C

You can use memset with the size of the struct:

struct x x_instance;
memset (&x_instance, 0, sizeof(x_instance));

Solution 7 - C

I believe you can just assign the empty set ({}) to your variable.

struct x instance;

for(i = 0; i < n; i++) {
    instance = {};
    /* Do Calculations */
}

Solution 8 - C

 struct x myX;
 ...
 memset(&x, 0, sizeof(myX));

Solution 9 - C

I asked a compiler engineer at work about which option is better (memset vs {0}). Instead of giving my an opinion he pointed me to Compiler Explorer. It's interesting to see how all three options compile out:

https://godbolt.org/z/bPfKeG9Yh

Here's a preview of the code:

// Type your code here, or load an example.
#include "string.h"

struct MyStruct {
    int age;
    int sin;
    char *name;
    int cats;
    char something[64];
};

const struct MyStruct empty_struct = {0};

int test() {
    struct MyStruct blah = {0};
    memset(&blah, 0, sizeof(blah));
    blah = empty_struct;

    blah.age = 99;
    blah.sin = 123456789;
}

The compiler makes different decisions on how to zero memory depending on the member types of the struct. Try commenting out something for example or choosing a non-x86 target.

Solution 10 - C

Memset with NULL is dangerous function.

For C++ better by this way for most simple data structures >>

template<class T>
void Null_MyType(T &obj)
{
	constexpr T o_null = T{};
	obj = o_null;
}

Solution 11 - C

debugger screenshot

Take a surprise from gnu11!

typedef struct {
	uint8_t messType;
	uint8_t ax;  //axis
	uint32_t position;
	uint32_t velocity;
}TgotoData;

TgotoData tmpData = { 0 };

nothing is zero.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhariView Question on Stackoverflow
Solution 1 - CDavid HeffernanView Answer on Stackoverflow
Solution 2 - CJens GustedtView Answer on Stackoverflow
Solution 3 - Cuser411313View Answer on Stackoverflow
Solution 4 - CtemplatetypedefView Answer on Stackoverflow
Solution 5 - CRudy VelthuisView Answer on Stackoverflow
Solution 6 - CDiego SevillaView Answer on Stackoverflow
Solution 7 - CArchmedeView Answer on Stackoverflow
Solution 8 - CJosh MatthewsView Answer on Stackoverflow
Solution 9 - CRandomInsanoView Answer on Stackoverflow
Solution 10 - CRedeeView Answer on Stackoverflow
Solution 11 - CАндрей ТернитиView Answer on Stackoverflow