Re: [Dev-C++] Stress with structs and pointers.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-06-12 19:47:28
|
When the declaration of a struct variable contains an "=3D { xx }"
assignment. the compiler will automatically add zero-values for the fields
that hasn't been specifically given a value. If { xx } contains values for
the three first fields, but the struct has five fields, the compiler will
assume zero for the remaining two fields.
A consequence of this is that if an existing program is modified, and new
member fields are added to an existing structure, you should either add
the new fields last, or specifically check all initialization of
corresponding struct variables, to make sure that a { member1 =3D 10,
member2 =3D 0, member3 =3D 12, ... } initialization doesn't suddenly starts=
to
assign to the wrong member fields.
Don't forget that the compiler is required to clear all global,
non-initialized, variables to zero on startup. A global variable declared
as "struct mystruct s;" will set all member fields of s to zero in a C
program. In C++, the program might override this behaviour by supplying a
constructor for the struct/class type.
/Per W
On Thu, 12 Jun 2003, OROSZI Bal=E1zs wrote:
> Per Westermark wrote:
> > memset() works very well in C programs. However, there are some
> > things to remember about memset() on structures.
> >
> > If the code is later moved to C++, and the struct gets virtual methods,
> > the memset() code will be deadly, since it will clear the address of th=
e
> > virtual method table (VMT), which will make calls to the methods kill t=
he
> > program. Also, the VMT is normally used for run-time type checking, suc=
h
> > as dynamic cast.
> >
> > One more thing about memset() of structures is that in C++, the struct =
may
> > contain members that are themself classes with a constructor. The work
> > done by these constructors will be overwritten.
> >
> > Memset of structures that are part of a multiple-inherritance aggregate=
is
> > also dangerous, since it kills the hidden fields the compiler added to
> > safely locate the addresses of the different base classes.
>
> Thanks for this info, I didn't know about this :)
> Indeed is useful.
>
> But, as a last note, if you (the original asker) use PURE C only, then
> you can safely use memset().
> But if you are in the C++ world, you should rarely use the standard C
> library anyway (where memset() is for example).
>
> > Note that most of the time, it is enough to just write:
> >
> > struct mystruct s =3D {0};
> >
> > and then the compiler will assign zero to all members of the structure.
> > This kind of initialization is directly portable to c++ without any
> > dangerous side effects.
>
> Are you sure, this works 100% in just C? I don't know, that's why I ask.
> What happens, when I have a struct with 5 elements, and I write:
>
> struct mystruct s =3D {0,0,0}; /* 3 only */
>
> How many will be initialised?
> Thanks in advance.
>
> --
> Greetings,
> Bal=E1zs
>
>
>
> -------------------------------------------------------
> This SF.NET email is sponsored by: eBay
> Great deals on office technology -- on eBay now! Click here:
> http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|