KaruF
KaruF

Reputation: 47

What does it mean initialize to static struct definition with memset() set zero;?

I have reading some example code on C language from ON Semiconductor. So I have saw like this structure definition in header file:

typedef struct //that is example structure
{

    uint16_t features;

    bool blt;

    const struct anotherstruct *structinfo;
} name;

So there is the definition on .c file and then using like this in foo() function with memset:

static name lastname;

   foo()
   {
        memset(&lastname,0,sizeof(name ))
        lastname.features=23;
        lastname.structinfo=info; //there isn't assigned any value for blt.But there will be...(maybe ı am thinking this is the reason...)
    }

I have got some searching on this site and I know already, static definitions equal to zero. So why ON Semiconductor set to 0 lastname in addition?

DP:foo() function has called just once by designer. So that can't be reset function because this structure definition lastname used first in foo() function

Upvotes: 2

Views: 541

Answers (4)

klutt
klutt

Reputation: 31409

It's hard to say by just looking at the short code pieces you have provided.

  • You are correct that static variables are zero initialized, so there are strictly speaking no reason to zero them before using them.

  • However, in real environments, it can be a good idea to not assume things. It's quite possible that this is just a "better safe than sorry".

  • It can also be a reset function. That is, it is called more than once. If it's not called more than once, that can still be the original intention.

  • A similar to the situation above can be that the reset function was written because there had been some code modifying lastname that has been removed, and then they forgot to remove foo.

  • One more option is that the programmer don't know that static variables are zeroed on initialization, or just forgot. Don't assume that all code you find has a valid reason for looking the way it does.

  • There is also a small chance that the programmer is tired of having to explain that static variables are zeroed and that globals are static to other programmers.

  • Another (however unlikely) reason is that the programmer discovered a bug in the compiler, and this was a workaround.

It's impossible for us to figure out what another programmer thought when they wrote the code. Especially if we only have a small piece of the codebase. If you really want to know in this particular case, ask the author of the code.

Upvotes: 3

Anders Sahlström
Anders Sahlström

Reputation: 156

In addition to the other answers it can be added that in some certified safety critical applications there is general mistrust of operations performed by non-certified start up code, as oposed to operations performed by certified code produced in-house. (Btw I opposed this policy on a number of reasons.)

Upvotes: 2

Ctx
Ctx

Reputation: 18410

Usually, the bss is cleared by startup code. Some free-standing C environments - especially in the context of embedded systems - choose to not initialize the bss for performance reasons. It is necessary to explicitly zero static variables then.

See for example the Texas Instruments TMS320C6000:

https://www.ti.com/lit/ug/spru187o/spru187o.pdf (Section 6.10)

The ANSI/ISO C standard specifies that global (extern) and static variables without explicit initializations must be initialized to 0 before the program begins running. This task is typically done when the program is loaded. Because the loading process is heavily dependent on the specific environment of the target application system, the compiler itself makes no provision for preinitializing variables at run time. It is up to your application to fulfill this requirement.

Upvotes: 1

user13709754
user13709754

Reputation:

It means the binary representation of the struct will be all 0's, it seems the foo() function resets it to those values.

memset(&lastname,0,sizeof(name ))
lastname.features=23;
lastname.structinfo=info;

Upvotes: 3

Related Questions