Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "memset" vs "= {0}"...Are they equivalent if your initializing variables?

Reply
Thread Tools

"memset" vs "= {0}"...Are they equivalent if your initializing variables?

 
 
JKop
Guest
Posts: n/a
 
      09-22-2004
JKop posted:

>> Not necessarily. Of the three methods I've tested, Blah

()
>> appears to be the slowest.
>>
>> The method:
>>
>> Blah poo;
>> memset(&poo, 0, sizeof(Blah));
>>
>> is the fastest, and
>>
>> Blah poo = {0}
>>
>> is a very close second;
>>
>> Basically, you must compromise speed for safety and

portability.
>>
>> This is first time I've ever timed functions. I'm

Visual Studio.NET
>> 2003, SDK, WindowsXP.
>>
>> My timing algorithm:
>> LARGE_INTEGER start;
>> LARGE_INTEGER end;
>> QueryPerformanceCounter( &start );
>> ...Do Something
>> QueryPerformanceCounter( &end );
>> TRACE( TEXT( "Time = %d\n" ), end.QuadPart-

start.QuadPart );
>>
>> Granted, we're talking minimal differences, but that's

besides the
>> point.
>>
>> Please explain a little deeper as to why memset might

not set a
>> pointer or a floating point number to zero. It does on

my computer.
>>
>> -Nollie

>
> Sometimes it takes me 10mins 23secs to get to the bustop.

Sometimes it
> takes 9mins 58secs...
>
> I wouldn't use "QueryPerformanceCounter" as an indicator

for this,
> unless you time it a few thousand times and get the

average, but even
> still, I'm not sure how accurate it'd be.
>
> As regards pointers... well let's say that on a certain

system, a
> pointer variable takes up 32 bits in memory, as so:
>
>
> 0000 0000 0000 0000 0000 0000 0000 0000
>
>
> What you're looking at above is "all bits zero". On

Windows, this
> indicates that the pointer is a null pointer. Now imagine

a system
> where the memory address 0 is a valid one, ie. it's the

first byte of
> memory, and that on this particular system, the null

pointer value is:
>
> 1111 1111 1111 1111 1111 1111 1111 1111
>
>
> When you write a program with the following line in it:
>
>
> int* p_k = 0;
>
> The compiler doesn't produce code that sets all bits to

zero... no
> no... what it does is produce code that sets it to the

null pointer
> value for that system (and/or for that type, I believe

systems may
> choose to have different null pointer values depending on

the type...).
> But "memset" doesn't have a clue about this, all it does

is set all
> bits to zero, which may be a valid memory address on some

systems,
> hence it's not portable.
>
> And as regards floating point numbers, implementations

aren't obligated
> to represent the value zero as "all bits zero" either.
>
> I don't see how either of the three could be

slower/faster than each
> other, they should all yield the same machine code

(except maybe the
> call to "memeset" might add overhead if it's not

inline...)
>
>
> -JKop


And I hope you realize what havoc "memset" would cause with
a non-POD type...

-JKop
 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      09-22-2004
"JKop" <(E-Mail Removed)> wrote in message
news:1Zc4d.31715$(E-Mail Removed)...

>> struct MYSTRUCT
>> {
>> int x;
>> int y;
>> int w;
>> int h;
>> };
>>

>
>> MYSTRUCT myStruct = {0};

>
> All this does is set "myStruct.x" to 0, all the others
> still contain no particular value.


I think not. If you brace-initialize any elements of an aggregate, the ones
you don't mention get set to zero.

So, for example:

int x[5] = { 42 };

and

int x[5] = { 42, 0, 0, 0, 0 };

are equivalent.


 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      09-22-2004
One thing I forgot to mention:

When you do "memset", padding bits (if any) get altered
too. I'm not sure if the Standard allows this...


-JKop
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-22-2004

"JKop" <(E-Mail Removed)> wrote in message news:OYf4d.31743$(E-Mail Removed)...
> One thing I forgot to mention:
>
> When you do "memset", padding bits (if any) get altered
> too. I'm not sure if the Standard allows this...
>

Allows what? You are allowed to access them, yes their state
is undefined during normal assignment/initialization.

 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-22-2004
Ron Natalie posted:

>
> "JKop" <(E-Mail Removed)> wrote in message
> news:OYf4d.31743$(E-Mail Removed)...
>> One thing I forgot to mention:
>>
>> When you do "memset", padding bits (if any) get altered
>> too. I'm not sure if the Standard allows this...
>>

> Allows what? You are allowed to access them, yes their

state
> is undefined during normal assignment/initialization.
>
>


I'm not sure if the Standard allows you to access the
padding bits.

For instance, if you have:

struct Blah
{
int a;
char b;
char c;
char d;
int b;
};


Chances are that it will be lain out in memory as so:

| a | b | c | padding | d


You can access the object as follows:

poo.a
poo.b
poo.c
poo.d

And a copy constructor and a assignment operator will both
copy the four member variables, but I'm not sure if the
Standard gives us control over the padding bits... anyone
know? I recall looking in the Standard for this before and
finding nothing.

Let's say hypothetically speaking that you *can't* mess
with padding bits. A certain implementation, when you do
the following:

Blah poo;
char x;

Might stick "x" into memory as so:

| a | b | c | x | d |


Again, I stress *hypothetically speaking*.

So does anyone know if the Standard allows messing with
padding?


-JKop




-JKop
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-22-2004

"JKop" <(E-Mail Removed)> wrote in message news:YPh4d.31764$(E-Mail Removed)...
>
> I'm not sure if the Standard allows you to access the
> padding bits.


Read the last paragraph of 3.10 of the standard.

> And a copy constructor and a assignment operator will both
> copy the four member variables, but I'm not sure if the
> Standard gives us control over the padding bits... anyone
> know? I recall looking in the Standard for this before and
> finding nothing.


The standard doesn't specify any behavior as to what happens to the
pad bytes in assignment or construction.

> So does anyone know if the Standard allows messing with
> padding?
>

If you mean aliasing it to char* and then accessing the chars, it is allowed.
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-22-2004
>> I'm not sure if the Standard allows you to access the padding bits.
>
> Read the last paragraph of 3.10 of the standard.


<QUOTE>
15 If a program attempts to access the stored value of an object through an
lvalue of other than one of the following
types the behavior is undefined4:
the dynamic type of the object,
a cv-qualified version of the dynamic type of the object,
a type that is the signed or unsigned type corresponding to the dynamic
type of the object,
a type that is the signed or unsigned type corresponding to a cv-qualified
version of the dynamic type of
the object,
an aggregate or union type that includes one of the aforementioned types
among its members (including,
recursively, a member of a subaggregate or contained union),
a type that is a (possibly cv-qualified) base class type of the dynamic
type of the object,
a char or unsigned char type.
</QUOTE>


Hmmm... I suppose.


Anyone?


-JKop
 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      09-22-2004
In article <(E-Mail Removed)>,
<(E-Mail Removed)> wrote:
>Please explain a little deeper as to why memset might not set a
>pointer or a floating point number to zero. It does on my computer.


Because all bits zero is not a valid representation for
floating points or pointers on many platforms.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tokina, Sigma, Tamron --- Are they equivalent in quality? Dave Digital Photography 40 04-11-2007 12:34 PM
Are they equivalent ? grid C Programming 14 12-18-2005 02:55 AM
Re: "memset" vs "= {0}"...Are they equivalent if your initializing variables? C++ 1 09-23-2004 02:03 PM
Re: "memset" vs "= {0}"...Are they equivalent if your initializing variables? C++ 0 09-23-2004 01:28 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments