473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

using exp() inside many nested for loops causing Memory overflow

Hi all,

I need to calculate a value inside 8 nested for loops. 2 additional
for loops are used during calculation. It was working fine with 4
loops. My code is like this:

...
for(int i1=0; i1<x1; i1 = i1++){
...
for(int i2=0; i2<x2; i2 = i2++){
...
for(int i3=0; i3<x3; i3 = i3++){
...
for(int i4=0; i4<x4; i4++){
calculate(i1, i2, i3, i4);
...
}
...
}
...
}
...
}
...

And the 'calculate' function is like this (I've simplified the code
for demonstration, the syntax is different):

for (int i=0; i<x; i++){
for (int j=0; j<y; j++){
array[2*i+j] = (float)exp( (-x1^2) / (x2^2) );
}
}

x1 are x2 are also float values.
The code throws OutOfMemoryException when using the exp() function.
I'm using VC++ 2005.

I've tried to increase virtual memory of my windows system to max. 4GB
but it didn't work
Any ideas about how I can I prevent memory overflow for this
calculation?

Any help would be appreciated, this is a little urgent.
Thanks
Aug 25 '08 #1
5 1791
Sam
er****@googlemail.com writes:
Hi all,

I need to calculate a value inside 8 nested for loops. 2 additional
for loops are used during calculation. It was working fine with 4
loops. My code is like this:

...
for(int i1=0; i1<x1; i1 = i1++){
...
for(int i2=0; i2<x2; i2 = i2++){
...
for(int i3=0; i3<x3; i3 = i3++){
...
for(int i4=0; i4<x4; i4++){
calculate(i1, i2, i3, i4);
...
}
...
}
...
}
...
}
...

And the 'calculate' function is like this (I've simplified the code
for demonstration, the syntax is different):

for (int i=0; i<x; i++){
for (int j=0; j<y; j++){
array[2*i+j] = (float)exp( (-x1^2) / (x2^2) );
}
}

x1 are x2 are also float values.
The code throws OutOfMemoryException when using the exp() function.
There is nothing, above, that involves memory allocation. I find it highly
unlikely that exp() has a memory leak. This is just a mathematical
calculation.
I'm using VC++ 2005.

I've tried to increase virtual memory of my windows system to max. 4GB
but it didn't work
Any ideas about how I can I prevent memory overflow for this
calculation?
Either you're leaking memory elsewhere in other parts of your code that you
did not show, or it's a memory leak in VC++.

Try replacing the exp() function with something else. Just replace the whole
thing with the constant 0, and see if you're still leaking memory.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkizTcYACgkQx9p3GYHlUOJbfQCeO0zdgm6kOR zMcVWtuUEYcbNR
EO8AnRCj43g8/Celnf5+3MpNFcOiN6oj
=jZOh
-----END PGP SIGNATURE-----

Aug 26 '08 #2
On Mon, 25 Aug 2008 16:51:57 -0700, ertis6 wrote:
Hi all,

I need to calculate a value inside 8 nested for loops. 2 additional for
loops are used during calculation. It was working fine with 4 loops. My
code is like this:

...
for(int i1=0; i1<x1; i1 = i1++){
Huh? Why not just:

for(int i1=0; i1<x1; i1++){
...
for(int i2=0; i2<x2; i2 = i2++){
...
for(int i3=0; i3<x3; i3 = i3++){
...
for(int i4=0; i4<x4; i4++){
calculate(i1, i2, i3, i4);
...
}
...
}
...
}
...
}
...

And the 'calculate' function is like this (I've simplified the code for
demonstration, the syntax is different):

for (int i=0; i<x; i++){
for (int j=0; j<y; j++){
array[2*i+j] = (float)exp( (-x1^2) /
(x2^2) );
}
}

x1 are x2 are also float values.
The code throws OutOfMemoryException when using the exp() function. I'm
using VC++ 2005.

I've tried to increase virtual memory of my windows system to max. 4GB
but it didn't work
Any ideas about how I can I prevent memory overflow for this
calculation?
Nothing obvious I can see in the code you've posted. Chances are the
problem exists in code you *haven't* posted. Post a *complete*, minimal
program that demonstrates the problem and maybe someone can help. Running
a memory checker (like valgrind) might also be a good idea - to check for
memory leaks.

Cheers,

--
Lionel B
Aug 26 '08 #3
er****@googlemail.com wrote:
for(int i1=0; i1<x1; i1 = i1++){
That must be one of the most obfuscated ways of incrementing the value
of a variable I have seen.
Aug 26 '08 #4
On 2008-08-26 07:23:25 -0400, Juha Nieminen <no****@thanks.invalidsaid:
er****@googlemail.com wrote:
> for(int i1=0; i1<x1; i1 = i1++){

That must be one of the most obfuscated ways of incrementing the value
of a variable I have seen.
Not to mention its undefined behavior. But it's obviously a typo, so
nothing to get worked up about.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 26 '08 #5
On Aug 26, 1:01*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-08-26 07:23:25 -0400, Juha Nieminen <nos...@thanks.invalidsaid:
ert...@googlemail.com wrote:
* * * * * * * *for(int i1=0; i1<x1; i1 *= i1++){
* That must be one of the most obfuscated ways of incrementing the value
of a variable I have seen.

Not to mention its undefined behavior. But it's obviously a typo, so
nothing to get worked up about.

--
* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Thanks a lot to all responses.

I first replaced a float array definition inside the loops and made it
a static array. That removed the memory overflow and made me realize
that there was an infinite loop due to a typo.

Lionel, I've not heard about valgrind before, thanks for advice.

Juna, I was using some complicated experssions in for loops that I
wanted to remove for readability, sorry about that.

Thanks again, the case is solved (for now)
Aug 26 '08 #6

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

Similar topics

7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
10
by: Pavan | last post by:
Hi i have two nested loops as shown below: 1. for(i=0;i<=1000;i++) { for(i=0;i<=100;i++) { .....; .....; }
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
10
by: pcnerd | last post by:
I'm a VB.NET newbie. I've created a program that plots pixels at random on the form. I have a 19" LCD monitor with a resolution set to 1280 by 1024. If you do the math, that means that there are...
3
by: Luna Moon | last post by:
My friend has three nested loops, each has 10000, 1000, 100 iterations, respectively. What should be the most efficient way of layout out the three nested loops? for (i=0; i<10000; i++) for...
50
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.