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

E

ertis6

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
 
L

Lionel B

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,
 
E

ertis6

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top