Is struct() equivalent to ZeroMemory() ?



 DEVELOP > c-Plus-Plus > Is struct() equivalent to ZeroMemory() ?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Jim Langston"
Date: 24 Aug 2007 08:19:12 PM
Object: Is struct() equivalent to ZeroMemory() ?
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++
.

User: "Alf P. Steinbach"

Title: Re: Is struct() equivalent to ZeroMemory() ? 24 Aug 2007 08:26:30 PM
* 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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: "Jim Langston"

Title: Re: Is struct() equivalent to ZeroMemory() ? 24 Aug 2007 09:38:41 PM
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:13cv1abg4odqj15@corp.supernews.com...

* 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.

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.

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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

.
User: "Old Wolf"

Title: Re: Is struct() equivalent to ZeroMemory() ? 27 Aug 2007 06:17:12 PM
On Aug 25, 2:38 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:

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?
.
User: "Jim Langston"

Title: Re: Is struct() equivalent to ZeroMemory() ? 27 Aug 2007 11:12:14 PM
"Old Wolf" <oldwolf@inspire.net.nz> wrote in message
news:1188256632.351557.269480@q4g2000prc.googlegroups.com...

On Aug 25, 2:38 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:

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?

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





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER