473,595 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

loop vs memset to initialize array.. different results?

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(gaborfil ter, 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

Jul 27 '06 #1
22 26703
silversurfer202 5 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(gaborfil ter, 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
Jul 27 '06 #2

"silversurfer20 25" <ki****@web.dew rote in message
news:11******** **************@ 75g2000cwc.goog legroups.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(gaborfil ter, 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<flo at>(filtersize, std::vector<flo at>(filtersize) );

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

-Mike

Jul 27 '06 #3

Mike Wahler schrieb:
"silversurfer20 25" <ki****@web.dew rote in message
news:11******** **************@ 75g2000cwc.goog legroups.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(gaborfil ter, 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<flo at>(filtersize, std::vector<flo at>(filtersize) );

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

-Mike
Tim

Jul 27 '06 #4
silversurfer202 5 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
Jul 27 '06 #5

"silversurfer20 25" <ki****@web.dew rote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
>
Mike Wahler schrieb:
>"silversurfer2 025" <ki****@web.dew rote in message
news:11******* *************** @75g2000cwc.goo glegroups.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(gaborfil ter, 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
Jul 27 '06 #6

Victor Bazarov schrieb:
silversurfer202 5 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
Jul 27 '06 #7
Mike Wahler wrote:
>Here is the example:

float gaborfilter[filtersize][filtersize];

memset(gaborfi lter, 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
Jul 27 '06 #8
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.
Jul 27 '06 #9

"silversurfer20 25" <ki****@web.dew rote in message
news:11******** **************@ 75g2000cwc.goog legroups.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(gaborfil ter, 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

Jul 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1862
by: Jim Moseby | last post by:
I stumbled across this while trying to update a table with a timestamp type column. At the time, I didn't know that the timstamp column would update itself when a row was changed. A kind gentleman in another group pointed that out to me. I added a column to a table and wanted to update the rows to populate the new column. When doing something like this I usually will create a temporary table and perform a dry run, just to make sure I am...
4
2421
by: kinne | last post by:
The following code is supposed to reverse the date in "yyyy-mm-dd" format, but it produces different results in Firefox 1.0 and in Internet Explorer 6SP1. In Firefox, the result is correct ("2004-11-29") but it's wrong in Internet Explorer 6SP1 ("00:20:15-11-29"). If I change "dateParts" to "dateParts", it's exactly the opposite that occures: a correct result in IExplorer but a fault in Firefox. Is there a workaround? Where do I miss the...
1
1751
by: RM | last post by:
I am using DataSet's .ReadXml() method to parse an XML file that has an inline schema and Im getting "different" results on various machines using the same myDataSet.ReadXml("myExample.xml"). Out of 5 test machines, my results are as follows. 1 Windows 2000 Server and 2 Windows XP Professional PC's that parses the XML, builds 2 tables with appropriate format columns (string, int, etc) and populates the tables with rows of data as...
3
2685
by: google | last post by:
I'm using ADODB to connect to an Access database from VB6. I'm running a query that's returning strange results- the following query returns different results in VB & Access: SELECT * FROM Robes WHERE strSize NOT LIKE '*+'; I've also run the following three queries in VB with the record count indicated beneath. Access seems to handle the queries fine, but VB seems to be having problems with the WHERE clause.
0
2211
by: taras.di | last post by:
Hi everyone, I've come across an extremely strange problem. The exact same query in both mysql command line client, and mysql query browser gives entirely different results. I was hoping someone out there could shed some light. Ok, the query (I've stripped it bare, the real query is a bit more complex)...
3
6224
by: vduber6er | last post by:
Lets say I have this structure: typedef struct numbers { double first = 0.0; double second = 0.0; double third = 0.0; } MYVALUES; and I initialize an array of MYVALUES like the following
1
1590
by: sunshine19992 | last post by:
Not sure if others have come acrossed this bit I have a program for a C# class I am taking and during my troubleshooting I have found that if I turn on a breakpoint and then press F5 to continue until the program finishes I am receiving different results than if I didn't insert a breakpoint. (Put breakpoint at 1st curly bracket after AddToSumCount(rollSum); line in RollDice() method): Here is a copy of my code if you want to see it: using...
6
1930
by: Avi | last post by:
I need to implement the following calculation: f = (a*b) + (c*d) where a,b,c,d are given double values and f is a double variable for the result I found out that using two different implementations gives two different results: 1) In the first implementation I compute the entire equation in memory 2) In the second implementation I store intermediate to variables, and then load them to compute the final result.
10
1215
by: Geoff Cox | last post by:
Hello, Can anyone tell me why these 2 sets of code give different results? for (J in Next) { <action> } <function>
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8019
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6674
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.