why is initialized struct being cleared first?

John Belmonte jvb@ibm.net
Tue Apr 13 17:24:00 GMT 1999


On my platform (MIPS) when I use structure initialization in C++, the
compiler generates a memset to clear the structure, and *then* loads
the given values.  (See example below.)  I've checked from egcs
1.1.2 all the way back to gcc 2.8.1 and get the same result.

Is this a compiler bug, configuration problem, me being clueless?  If
someone would point me in the right direction I'd appreciate it.

John Belmonte
john@nanaon-sha.co.jp

//////////////////////////////////////////////////////////////////////////

struct Point { int x; int y; int z; };

// external function
void Do( Point* p );

void func1( int x, int y, int z )
{
    Point p = { x, y, z };  // why?: generates a memset to clear
structure
                            // before loading x, y, z
    Do( &p );
}

void func2( int x, int y, int z )
{
    Point p;               // ok: no memset called, much faster
    p.x=x; p.y=y; p.z=z;

    Do( &p );
}


More information about the Gcc-bugs mailing list