Q: Local variables initialization shortcut.



Hi all,

The function f() has some local (double)
which should all be initialized to zero :

a = b = c = ... = y = z = 0.0;

Can I use a shortcut like this :

memset( &a, 0, number_of_variables * sizeof(double) );

If yes, can I do this :

memset( &a, 0, (&z - &a + sizeof(double)) );

Like this :

void f(void)
{
double a,b,c,d,...,x,y,z;

memset( &a, 0, (unsigned int)( &z - &a + sizeof(double) ) );

...
}

--------

PS :
Of course the # of variables is not always 26,
that's why I want an easy way to implement initialization to zero.

.



Relevant Pages

  • Re: Storage of Variables
    ... code at entry to a function zero out the stack. ... Local variables are stack based. ... think), the C Standard, Paragraph A8.7 Initialization has this to say: ... The stack and the data segment are generally identical. ...
    (comp.os.linux.misc)
  • Re: Strange result from coding error?
    ... You happened to choose zero. ... checking is done that "it might be nice" if the array had zero size. ... Initialization isn't even related except ... "uninitialized" state, but that allocatables can't? ...
    (comp.lang.fortran)
  • Re: C to Fortran?
    ... On 8/28/2012 2:58 PM, glen herrmannsfeldt wrote: ... real variables to be initialised to zero. ... initialization identically as does the old compiler and does not produce ...
    (comp.lang.fortran)
  • Re: zero-initialization of variables
    ... I have seen several thousand FORTRAN and Fortran ... assumed that the compiler initialized to zero. ... requests for compiler options to implement zero initialization in order ... Scalars were allocated on the stack, and stack cells were allocated by ...
    (comp.lang.fortran)
  • Re: Strange result from coding error?
    ... You happened to choose zero. ... Unallocated arrays don't have a size. ... Speaking of which, if you are talking only about initialization, then ... allocatables. ...
    (comp.lang.fortran)