Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > loop vs memset to initialize array.. different results?

Reply
Thread Tools

loop vs memset to initialize array.. different results?

 
 
silversurfer2025
Guest
Posts: n/a
 
      07-27-2006
Hello everybdy,
I am a little confused for the following reason:

In my code I used a simple for-loop in order to initialize a 2D-array
of floats to zero. Because of efficiency reasons, I changed it to use
memset and I get totally different results.. How can this be?

Here is the example:

float gaborfilter[filtersize][filtersize];

memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);

OR

for(int i = 0; i < filtersize;i++){
for(int j = 0; j < filtersize;j++){
gaborfilter[i][j] = 0;
}
}

Later calculations give totally different results using these two
methods. Is there something really obviously different, which I am
currently missing?

Thanks a lot
Tim

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-27-2006
silversurfer2025 wrote:
> Hello everybdy,
> I am a little confused for the following reason:
>
> In my code I used a simple for-loop in order to initialize a 2D-array
> of floats to zero. Because of efficiency reasons, I changed it to use
> memset and I get totally different results.. How can this be?
>
> Here is the example:
>
> float gaborfilter[filtersize][filtersize];
>
> memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);
>
> OR
>
> for(int i = 0; i < filtersize;i++){
> for(int j = 0; j < filtersize;j++){
> gaborfilter[i][j] = 0;
> }
> }
>
> Later calculations give totally different results using these two
> methods. Is there something really obviously different, which I am
> currently missing?


A "zero" in a floating point variable is not necessarily the same as
"all bits zero". 'memset' clears all bits. Assigning 0 to the elements
of the array does THE RIGHT THING(tm). Have you thought about simply
initialising the array or do you need to periodically clear it?

float blah[one][two] = {}; // sets all elements to 0

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      07-27-2006

"silversurfer2025" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hello everybdy,
> I am a little confused for the following reason:
>
> In my code I used a simple for-loop in order to initialize a 2D-array
> of floats to zero. Because of efficiency reasons,


Have you proven with timing or profiling that there
is indeed an efficiency problem?

> I changed it to use
> memset



Don't.

> and I get totally different results.. How can this be?


Because the representation of 0.0 isn't necessarily
all-bits-zero.

>
> Here is the example:
>
> float gaborfilter[filtersize][filtersize];
>
> memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);


This is only guaranteed to work with unsigned integer types
(e.g. 'unsigned int', 'unsigned char'.


> OR
>
> for(int i = 0; i < filtersize;i++){
> for(int j = 0; j < filtersize;j++){
> gaborfilter[i][j] = 0;


This is the only portable way to assign values to
each element of the array of type 'float' objects.


But if you want all values to be zero, you could simply
initialize the array when you create it:

float gaborfilter[filtersize][filtersize] = {0};
/* (all elements now have value of zero) */

> }
> }
>
> Later calculations give totally different results using these two
> methods. Is there something really obviously different, which I am
> currently missing?


See above.

Also, you should use type 'double' rather than 'float'.
There's even a possibility that type 'double' could be
more 'efficient' (but this ultimately depends upon your
platforrm).

And finally, this is C++, so imo you should be using
containers rather than arrays.

std::vector<float>(filtersize, std::vector<float>(filtersize));

/* All elements now have values of zero. */

-Mike



 
Reply With Quote
 
silversurfer2025
Guest
Posts: n/a
 
      07-27-2006

Mike Wahler schrieb:

> "silversurfer2025" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > Hello everybdy,
> > I am a little confused for the following reason:
> >
> > In my code I used a simple for-loop in order to initialize a 2D-array
> > of floats to zero. Because of efficiency reasons,

>
> Have you proven with timing or profiling that there
> is indeed an efficiency problem?
>
> > I changed it to use
> > memset

>
>
> Don't.

OK
>
> > and I get totally different results.. How can this be?

>
> Because the representation of 0.0 isn't necessarily
> all-bits-zero.
>
> >
> > Here is the example:
> >
> > float gaborfilter[filtersize][filtersize];
> >
> > memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);

>
> This is only guaranteed to work with unsigned integer types
> (e.g. 'unsigned int', 'unsigned char'.

Ah, ok.. wat a pitty... I thought that I could get my code to work
faster..

>
>
> > OR
> >
> > for(int i = 0; i < filtersize;i++){
> > for(int j = 0; j < filtersize;j++){
> > gaborfilter[i][j] = 0;

>
> This is the only portable way to assign values to
> each element of the array of type 'float' objects.
>
>
> But if you want all values to be zero, you could simply
> initialize the array when you create it:
>
> float gaborfilter[filtersize][filtersize] = {0};

I tried this one before, but I get the compiler error:
error: variable-sized object 'gaborfilter' may not be
initialized
Anything I can do about it?

> /* (all elements now have value of zero) */
>
> > }
> > }
> >
> > Later calculations give totally different results using these two
> > methods. Is there something really obviously different, which I am
> > currently missing?

>
> See above.
>
> Also, you should use type 'double' rather than 'float'.
> There's even a possibility that type 'double' could be
> more 'efficient' (but this ultimately depends upon your
> platforrm).

I'll change it right away, I thought that float would be better for
memory-reasons..

>
> And finally, this is C++, so imo you should be using
> containers rather than arrays.

I am only using them for calculations, the results are later put into a
vector. (please do not ask!)

>
> std::vector<float>(filtersize, std::vector<float>(filtersize));
>
> /* All elements now have values of zero. */
>
> -Mike


Tim

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-27-2006
silversurfer2025 wrote:
>> [..]
>> But if you want all values to be zero, you could simply
>> initialize the array when you create it:
>>
>> float gaborfilter[filtersize][filtersize] = {0};

> I tried this one before, but I get the compiler error:
> error: variable-sized object 'gaborfilter' may not be
> initialized
> Anything I can do about it?


Variable-sized object? Are you using g++ extensions? You might want
to consider a dynamic array then...

> [..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      07-27-2006

"silversurfer2025" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> Mike Wahler schrieb:
>
>> "silversurfer2025" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) ups.com...
>> > Hello everybdy,
>> > I am a little confused for the following reason:
>> >
>> > In my code I used a simple for-loop in order to initialize a 2D-array
>> > of floats to zero. Because of efficiency reasons,

>>
>> Have you proven with timing or profiling that there
>> is indeed an efficiency problem?
>>
>> > I changed it to use
>> > memset

>>
>>
>> Don't.

> OK
>>
>> > and I get totally different results.. How can this be?

>>
>> Because the representation of 0.0 isn't necessarily
>> all-bits-zero.
>>
>> >
>> > Here is the example:
>> >
>> > float gaborfilter[filtersize][filtersize];
>> >
>> > memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);

>>
>> This is only guaranteed to work with unsigned integer types
>> (e.g. 'unsigned int', 'unsigned char'.

> Ah, ok.. wat a pitty... I thought that I could get my code to work
> faster..


Have you proven that it's not fast enough?

>
>>
>>
>> > OR
>> >
>> > for(int i = 0; i < filtersize;i++){
>> > for(int j = 0; j < filtersize;j++){
>> > gaborfilter[i][j] = 0;

>>
>> This is the only portable way to assign values to
>> each element of the array of type 'float' objects.
>>
>>
>> But if you want all values to be zero, you could simply
>> initialize the array when you create it:
>>
>> float gaborfilter[filtersize][filtersize] = {0};

> I tried this one before, but I get the compiler error:
> error: variable-sized object 'gaborfilter' may not be
> initialized
> Anything I can do about it?


Yes. Don't use non-const expressions to specify array
dimensions. They're not allowed in C++. Either use
a literal constant, a const-qualified object, or a macro:

float gaborfilter[10][10];

or

const size_t filtersize(10);
float gaborfilter[filtersize][filtersize];

or

#define filtersize 10

float gaborfilter[filtersize][filtersize];

But as I've pointed out already, a container (e.g.
vector) would be better.

>
>> /* (all elements now have value of zero) */
>>
>> > }
>> > }
>> >
>> > Later calculations give totally different results using these two
>> > methods. Is there something really obviously different, which I am
>> > currently missing?

>>
>> See above.
>>
>> Also, you should use type 'double' rather than 'float'.
>> There's even a possibility that type 'double' could be
>> more 'efficient' (but this ultimately depends upon your
>> platforrm).

> I'll change it right away, I thought that float would be better for
> memory-reasons..


Stop guessing. Prove there's a problem before trying to solve it.

-Mike


 
Reply With Quote
 
silversurfer2025
Guest
Posts: n/a
 
      07-27-2006

Victor Bazarov schrieb:

> silversurfer2025 wrote:
> >> [..]
> >> But if you want all values to be zero, you could simply
> >> initialize the array when you create it:
> >>
> >> float gaborfilter[filtersize][filtersize] = {0};

> > I tried this one before, but I get the compiler error:
> > error: variable-sized object 'gaborfilter' may not be
> > initialized
> > Anything I can do about it?

>
> Variable-sized object? Are you using g++ extensions? You might want
> to consider a dynamic array then...


Hm.. I do not even know what these extensions are. I do compile with
g++ but the declaration I gave above is all I wrote. I guess it says
dynamic because there is a variable (filtersize) giving the length of
the array which should be created?

Thanks once more
Tim

>
> > [..]

>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-27-2006
Mike Wahler wrote:
>> Here is the example:
>>
>> float gaborfilter[filtersize][filtersize];
>>
>> memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);

>
> This is only guaranteed to work with unsigned integer types
> (e.g. 'unsigned int', 'unsigned char'.
> ...


Strictly speaking, this is only guaranteed to work with 'char' types (signed and
unsigned). It is not guaranteed to work with larger integral types because it
might set their padding bits (if any) incorrectly.

There's a C99 proposal to require it to work with all integral types (I don't
know its current status).

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      07-27-2006
Andrey Tarasevich wrote:

>
> Strictly speaking, this is only guaranteed to work with 'char' types (signed and
> unsigned). It is not guaranteed to work with larger integral types because it
> might set their padding bits (if any) incorrectly.
>


To underscore the "strictly speaking" portion: this works just fine with
every floating-point implementation I've used. The problem is
undoubtedly somewhere else, despite the apparent certainty expressed in
most of the answers.
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      07-27-2006

"silversurfer2025" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hello everybdy,
> I am a little confused for the following reason:
>
> In my code I used a simple for-loop in order to initialize a 2D-array
> of floats to zero. Because of efficiency reasons, I changed it to use
> memset and I get totally different results.. How can this be?
>
> Here is the example:
>
> float gaborfilter[filtersize][filtersize];
>
> memset(gaborfilter, 0, sizeof(float) * filtersize * filtersize);
>
> OR
>
> for(int i = 0; i < filtersize;i++){
> for(int j = 0; j < filtersize;j++){
> gaborfilter[i][j] = 0;
> }
> }
>
> Later calculations give totally different results using these two
> methods. Is there something really obviously different, which I am
> currently missing?
>


What differs in the results? And how do you know (for sure) that it's
related to the initialization above and not some other code that's run
later?

-Howard



 
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
Memset is faster than simple loop? AndersWang@gmail.com C Programming 24 03-12-2013 09:42 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
initialize two dimension array of pointers using memset davoti@gmail.com C Programming 5 12-29-2008 10:29 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
memset to initialize double array? Joakim Hove C Programming 3 11-08-2005 06:40 PM



Advertisments