By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,505 Members | 1,168 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,505 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.