Zero Memory in C++

Apr 3, 2010 at 4:31pm
Hey,

i am a little confused. I want to initialize a struct, so it will be zero'd.
But here is the restriction: Since memset, (or on windows ZeroMemory) are not really safe when it comes to member which are more complex objects, i dont want to use them.

So i looked into some alternatives. std::fill or std::fill_n are nice to fill an array of datatype given, but they do not fit my problem of filling a single struct. (or does they?)

In plain C memset was fine. But in C++ its very unsafe to fill a struct with memset anyway.. So...

I looked more further and found something like this:

1
2
// sample struct addrinfo for using e.g. ipv6
addrinfo hints = { 0 };


This even worked, and gives a compile-time error i use this on struct where you have class members. (like a std::string member) Thats very nice. BUT!

Is this really standard behaviour? Where can i check this? (means is it written down somewhere as standard?)


Thanks, Maikel
Last edited on Apr 3, 2010 at 5:05pm
Apr 3, 2010 at 4:42pm
If you want to use non-PODs you should write constructors
Apr 3, 2010 at 4:56pm
Thanks, but that i am aware of this and in my classes and structures i use constructors.
The problem is just, that i write some different web-based applications and now i am reconsidering the design of the implementation.
Since this is a border between C and C++, the POSIX standard defines structures like addrinfo without constructors or destructors (so it is C-compatible).

Maybe i should ask more specific: How can I initialize a struct, so its members are zero'd without to be able to use constructors, in a safe manner for sure. (means bytewise 0x00 everything)

But thank you again for this fast answer,
Maikel

Edit:

this is not a so very specific question, because e.g. addrinfo needs to be zero'd before i can use the function getaddrinfo(...). This is the 'new' POSIX resolving way of hostnames to determine dynamicly if it is capable of IPV6 or IPV4 and so on..

Even so, i found a way like this:

 
addrinfo hints = {0};


But this has surpised me, and now my real question is: Is this standard behaviour for POD-structs? I cannot find answers at google (but im sure i googled not enough)

Edit2:

My problem with POD-structs in C++ and memset or ZeroMemery is following:
POD-structs contain afaik some padding, which C-structs do not. Thats why i thought memset and ZeroMemory are not garanteed to work as expected on these. Am I wrong?
Last edited on Apr 3, 2010 at 5:09pm
Apr 3, 2010 at 5:25pm
addrinfo hints = {0};
But this has surpised me, and now my real question is: Is this standard behaviour for POD-structs? I cannot find answers at google (but im sure i googled not enough)
Yes

My problem with POD-structs in C++ and memset or ZeroMemery is following:
POD-structs contain afaik some padding, which C-structs do not. Thats why i thought memset and ZeroMemory are not garanteed to work as expected on these. Am I wrong?
PODs are C structs.
Apr 3, 2010 at 5:33pm
I found an interresting link: http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html

PODs are C structs.


Oh i checked again. I believe i misread something on wikipedia. Thanks again for pointing this out.


Maikel
Apr 3, 2010 at 5:34pm
I found an interresting link: http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html

PODs are C structs.


Oh i checked again. I believe i misread something on wikipedia. Thanks again for pointing this out.


Maikel
Topic archived. No new replies allowed.