Re: struct initilization via memset

From: Andrew Koenig (ark_at_acm.org)
Date: 02/14/05


Date: Mon, 14 Feb 2005 04:22:27 GMT


<ma740988@pegasus.cc.ucf.edu> wrote in message
news:1108348175.249340.269030@l41g2000cwc.googlegroups.com...

> // vendors_file.h
> enum enum_test { x1 = 0, x2 = 2 };
> struct test
> {
> enum_test e;
> void (*fc)(int var1, int var2);
> int idx;
> } ;

> X1()
> {
> memset(&me, 0, sizeof me);
> }

> Perhaps I'm mistaken and will seek clarification, nonetheless, one of
> my peers (who seem to know what he's talking about when it comes to the
> language) leads me to believe that memset in this context is a bad
> idea.

Your peer is correct.

> Admittidely, I was walking 'out the door' when he said this. I've also
> consulted a few of my c++ text but I can't see what the issue is with
> using memset in this context?

There is no guarantee as to what it will do. Its behavior depends on the
particular way in which your implementation happens to represent integers,
enums, and pointers.

If you want to obtain the effect of assigning zero, appropriate converted,
to each element of an object named "me" of type test, I suggest you do it
this way:

    me = test();