13

Assume we have struct like this:

struct A
{
    int x;
    int y;
};

what is the difference between

A a = {0};

and

A a;
memset(&a,0,sizeof(A));
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Avi Moraly
  • 249
  • 2
  • 3
  • 9
  • 1
    Well, is `A a = { 0 };` guaranteed to set the padding to 0 as well? because if not, that's a difference too. –  Dec 25 '12 at 15:08
  • @H2CO3 In C2011, it is, it's not guaranteed in C99. (But it's C, so it needs to be `struct A a = {0};` anyway.) – Daniel Fischer Dec 25 '12 at 15:10
  • @H2CO3 are we talking about the memory region an access to which is undefined behavior? – John Dvorak Dec 25 '12 at 15:10
  • @JanDvorak Yes, we are. But technically, it's still a difference, whether or not we want to access it. –  Dec 25 '12 at 15:11
  • @H2CO3 If anything that could affect the outcome of an UD is to be considered, then there's nothing that doesn't matter, especially when it reaches the compiler. You can't reason about UD - it doesn't have to make sense. – John Dvorak Dec 25 '12 at 15:13
  • @DanielFischer: Does C11 guarantee that initializing an integer to zero also zeroes the padding, or does it merely guarantee that all-bits-zero must be *a* valid representation of zero (but not necessarily the only one)? – supercat Sep 15 '15 at 19:57
  • @supercat You mean padding bits in the integer type itself? I don't think the standard says anything about that - except that you don't get a trap representation from doing valid things. But I may have overlooked it. IIRC, initialising an integer to `0` or assigning `0` to one is guaranteed to give you "unsigned or positive zero", so all value bits `0`, and in case of a signed type also the sign bit `0`. But I'm too out of it to be quite sure of that. – Daniel Fischer Sep 15 '15 at 20:27

2 Answers2

14

None. The final outcome is that both initialize structure members to 0.

C99 Standard 6.7.8.21

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

Your structure A is an aggregate and above rule applies to it. So all the structure members are initialized with value same as for static storage duration. Which is 0.

C99 Standard 7.21.6.1 The memset function:

void *memset(void *s, int c, size_t n);

The memset function copies the value of c (converted to an unsigned char) into each of the first n characters of the object pointed to by s.

In simple words all the members including the alignment/padding bits in object of your structure A are set to 0.

Note that only difference between the two constructs in C is that memset sets the alignment/padding to 0 as well, while the aggregate initialization only guarantees that your structure members are set to 0.

In either case you do not have access to the alignment/padding bytes through convention language constructs and hence both get you the same effect.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    I forgat about the value, but is this matter if the struct has padding or is it aligned ? – Avi Moraly Dec 25 '12 at 15:06
  • @LuchianGrigore: Ofcourse, it is a typo. It's about time we see the questions in bigger context that petty typos. – Alok Save Dec 25 '12 at 15:06
  • 4
    I say it's time people put minimum effort in their questions. At least copy-paste properly - it's not asking for much. – Luchian Grigore Dec 25 '12 at 15:08
  • 1
    With respect to petty typos: "in bigger context tha **t** petty typos" – Daniel Fischer Dec 25 '12 at 15:09
  • 4
    @LuchianGrigore: It is not asking for much, but rather expected from an new user with rep `1`. I don't think it is too much to expect to take a moral high ground and answer the Q asked. I would say maybe even edit the Q to reflect the obvious & leaving OP note about being careful next time. Instead of doing anything of that, its disappointing that high rep users jump to cash in rep by throwing in flashy blistering answers which don't really help the OP or for that matter anyone at all. – Alok Save Dec 25 '12 at 15:16
  • 1
    @AlokSave I guess that not-really-useful-but-extremely-quick answers are what makes some users so high-rep. – John Dvorak Dec 25 '12 at 15:18
  • 1
    @JanDvorak: I agree. And some of the high rep users should consider taking the moral high ground instead of going after the rep. – Alok Save Dec 25 '12 at 15:33
  • @AlokSave does the {0} form offer the opportunity for the zeroing to be done at compile time instead of runtime? – James Dec 25 '12 at 15:44
  • 2
    It's not about taking the high ground or not, it's about pushing towards a clean question. **Especially** for a user with 1 rep. It's education. Instead of "here, let me fix that for you, don't let it bother you", I choose "here's the answer to your question, if it's not what you wanted, take the time to formulate a good question". – Luchian Grigore Dec 25 '12 at 15:51
  • 1
    @LuchianGrigore: *"Pushing towards a clean question"* The `memset` missing an parameter is obviously an typo which any c programmer can spot, understand and comperehend.The Q didn't have enough ambiguity to seek clarification on. Not enough to do so through an answer..really?? Please do not mask your obvious rep whoring under "Education" practices. Anyhow, the silly argument you chose to defend your actions brought me a smile, So thanks for that :) – Alok Save Dec 25 '12 at 16:00
  • 2
    I never attempted to "mask" my "rep whoring" - I've made it very clear I do it, mostly because there's nothing wrong with it. At least I admit it though - you, on the other hand, apparently think you don't do it, when merely answering questions like these is full proof that you do. If I wanted to milk the question for votes, I assure you I too could have edited & answered the intended version. It's not rocket science. So yes, in this case, I did want to push towards a cleaner question. In this case, it isn't I who is rep whoring. ;) – Luchian Grigore Dec 25 '12 at 16:54
  • @LuchianGrigore: The Q is not what you could or can do the Q is what you did. You added a flashy useless answer just to milk the rep & on top of that you try to discourage others from answering a perfectly valid Q in lieu of your rep whoring answer.The more you try to justify it the more your arguments look silly and pointless leading me to the conclusion that arguing with you is wasting time, effort and patience.So I will restrain myself from doing so now and in future.Carry on with your merry rep whoring. – Alok Save Dec 26 '12 at 06:19
6

Both are setting memory to 0

The first is used to set only a static allocation memory to 0

A a ={0}; // set a staic memory to 0

And you could not do it in this way:

A *a = malloc(sizeof(A)); a = {0} // This could not be done

The second is used to set both dynamic and static allocation memory to 0

A a;
memset(&a,0,sizeof(A));

And you could do also

A *a = malloc(sizeof(A)); memset(a,0,sizeof(A));

Another thing

when using memset to set your memory to 0, here your are calling a function (and this take time). And when setting with {0}, you are not calling a function. So the {0} could be faster than memset

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • The OP asks a specific difference for the example code he cited.The difference stated in the answer though valid & important does not really answer OP's Q. – Alok Save Dec 25 '12 at 15:36
  • 1
    The tile of his question is general. so this answer could be answer for the title question – MOHAMED Dec 25 '12 at 15:38
  • 1
    You are using the term "static" incorrectly. In C, static describes a storage class. You can also set non-static (i.e. automatic) structs to zero. – Jens Sep 15 '15 at 12:43
  • 1
    The point about not being able to set dynamic allocation is 100% incorrect. In your second example, you can just use `*a = (A){0}`. – Craig Barnes May 10 '18 at 09:08