0

I am currently writing a small part of an embedded OS, where I some times need to assign all the variables in a large struct to 0. The elements range from uint8 to uint64. As of now, the only thing I have gotten to work, is to loop through each of the types of variables, assigning each of them to 0, but this takes many cycles and the code is "ugly". I was thinking if it was possible to make a pointer of the type of the struct, and then assigning the value of the pointed element to 0, as that element should have the size sizeof(MyStruct)?

The struct to assign to 0:

struct AMD64Context{
uint8_t fp_context[512] __attribute__((alligned(16)));
uint64_t rax;
.... 
....
};
Orpedo
  • 483
  • 5
  • 14

1 Answers1

7

Two "classic" methods:

  • memset

    AMD64Contex context;
    memset(&context, 0, sizeof(context));
    
  • initialization with {0}

    AMD64Contex context={0};
    

Notice that the result is slightly different: in the first case, you are brutally zeroing all the memory, which guarantees that all the integral fields are zero and any padding that may be present is zeroed too. For FP values, the zeroing is not guaranteed by the standard (but it's so on any "normal" platform anyway; also, since you are writing an OS you surely know if on your platform an "all zero" FP value is actually zero).

The second one instead is guaranteed to zero the fields, but it's unspecified if it affects the padding. If you are not interested in the values of the padding, you should probably use this one - it's more succinct, and it's more obvious to the compiler what is the intent (so it may optimize this better).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Maby I should have mentioned, that I was programming in an environment using C99, compiler, which doesn't support memset/memcpy. But in the thread http://stackoverflow.com/questions/6891720/initialize-reset-struct-to-zero-null some guy have answered with a compliant method of typecasting: mystruct = (struct x){0}; – Orpedo Dec 22 '13 at 22:03
  • 3
    @Orpedo: What kind of environment doesn't have `memcpy`? – Oliver Charlesworth Dec 22 '13 at 22:04
  • 1
    @Orpedo: You *did* remember to `#include `, right? – user2357112 Dec 22 '13 at 22:04
  • @Orpedo: that's exactly equivalent to my "second method" - that syntax creates a temporary zeroed object and assigns it to the target; here I was assuming that you wanted to initialize a new `struct`, to reset an existing instance you want the syntax you found in the other question. – Matteo Italia Dec 22 '13 at 22:07
  • A GNU99 compiler. string.h isn't a part of the standard package either. Nor would I like to include it, as it is an embedded system... – Orpedo Dec 22 '13 at 22:08
  • For some unknown reason the compiler doesn't approve of any of the methods. Getting error "missing braces around initializer", but the syntax should be right... Thanks anyway. – Orpedo Dec 22 '13 at 22:15
  • 2
    Write you own memset - it's quite easy. – DoxyLover Dec 22 '13 at 23:10