Is struct() equivalent to ZeroMemory() ?

J

Jim Langston

For some code I copied off of MSDN it uses the Cism of

OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));

I was wondering if
OPENFILENAME ofn = OPENFILENAME();
is equivalent for all cases of a structure?

Would this also asign pointers a value of NULL (which this structure does
use, pointers).

Or should I just stick with the Cism of ZeroMemory for a structure that can
be used in C and C++
 
A

Alf P. Steinbach

* Jim Langston:
For some code I copied off of MSDN it uses the Cism of

OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));

I was wondering if
OPENFILENAME ofn = OPENFILENAME();
is equivalent for all cases of a structure?

No, it does a better job, invoking C++ constructors if that's required,
and just zeroing memory where that's sufficient.

Also, it's safe and can be optimized by the compiler.

However, the usual way to deal with a POD structure is

OPENFILENAME ofn = {0};

Here, if the first member of the structure is the byte size, you simply
use a sizeof expression instead of 0.

Would this also asign pointers a value of NULL (which this structure does
use, pointers).
Yes.


Or should I just stick with the Cism of ZeroMemory for a structure that can
be used in C and C++

No, it's a good idea to generally avoid ZeroMemory, memset, memcpy etc.,
because they're unsafe and may not always do what you think they do
(also, contrary to what many believe, they may simply be inefficient).

Cheers, & hth.,

- Alf
 
J

Jim Langston

Alf P. Steinbach said:
* Jim Langston:

No, it does a better job, invoking C++ constructors if that's required,
and just zeroing memory where that's sufficient.

Also, it's safe and can be optimized by the compiler.

However, the usual way to deal with a POD structure is

OPENFILENAME ofn = {0};

Here, if the first member of the structure is the byte size, you simply
use a sizeof expression instead of 0.

Ahh, very good point. You must of used this structure before since, indeed,
the first byte is the sizeof. Meaning I can replace the code:

OPENFILENAME ofn;

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);

with

OPENFILENAME ofn = {sizeof(OPENFILENAME}};

although I think some people would use

OPENFILENAME ofn = {sizeof(ofn)};

Not sure which is prefered, but I generally use the class/structure name in
sizeof instead of an instance.
 
O

Old Wolf

OPENFILENAME ofn = {sizeof(OPENFILENAME}};

although I think some people would use

OPENFILENAME ofn = {sizeof(ofn)};

Not sure which is prefered, but I generally use the
class/structure name in sizeof instead of an instance.

I can't think of any rational reason why you would use the
former. Can you enlighten me?
 
J

Jim Langston

Old Wolf said:
I can't think of any rational reason why you would use the
former. Can you enlighten me?

You can't think of any rational reason to use the sizeof the class, or the
sizeof the instance?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top