473,386 Members | 1,705 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,386 software developers and data experts.

nested for loops behaving oddly

I'm writting a peice of code for the numerical reconstruction of
digital holograms. I have carried out a two dimensional fft using
fftw. The output of which is stored in an array of fftw_complex.

After the fft I need to manipulate the output array so I have used a
nested for loop to cycle through the values. My code for the loop
looks like this

double intensity_array [MAXX][MAXY];
for (int xx=0 ; xx < width ; xx++)
{
for (int yy=0 ; yy < height ; yy++)
{
intensity_array[xx][yy] =
(out[yy+height*xx][0]*out[yy+height*xx][0])+(out[yy+height*xx][1]*out[yy+height*xx][1]);
}
}

This code compiles fine with msvs.net but when I run the program I get
a message stating that my program has encountered a problem and needs
to end.

Through experimentation I have found that if I change the code so that
the result of each iteration is stored in one column the original array
like this
for (int xx=0 ; xx < width ; xx++)
{
for (int yy=0 ; yy < height ; yy++)
{
out[yy+height*xx][0] =
(out[yy+height*xx][0]*out[yy+height*xx][0])+(out[yy+height*xx][1]*out[yy+height*xx][1]);
}
}

my code will run fine. I'd like to avoid this however as I loose the
original data which I may use later. Any help or advise would be
greatly appreciated.

Dec 8 '05 #1
8 1977
ri*************@gmail.com wrote:
I'm writting a peice of code for the numerical reconstruction of
digital holograms. I have carried out a two dimensional fft using
fftw. The output of which is stored in an array of fftw_complex.

After the fft I need to manipulate the output array so I have used a
nested for loop to cycle through the values. My code for the loop
looks like this

double intensity_array [MAXX][MAXY];
for (int xx=0 ; xx < width ; xx++)
{
for (int yy=0 ; yy < height ; yy++)
{
intensity_array[xx][yy] =
(out[yy+height*xx][0]*out[yy+height*xx][0])+(out[yy+height*xx][1]*out[yy+height*xx][1]);
}
}

This code compiles fine with msvs.net but when I run the program I get
a message stating that my program has encountered a problem and needs
to end.

Through experimentation I have found that if I change the code so that
the result of each iteration is stored in one column the original array
like this
for (int xx=0 ; xx < width ; xx++)
{
for (int yy=0 ; yy < height ; yy++)
{
out[yy+height*xx][0] =
(out[yy+height*xx][0]*out[yy+height*xx][0])+(out[yy+height*xx][1]*out[yy+height*xx][1]);
}
}

my code will run fine. I'd like to avoid this however as I loose the
original data which I may use later. Any help or advise would be
greatly appreciated.


Add this statement before the original loop and see if it dies (you
might also need #include <cassert>):

assert( width <= MAXX && height <= MAXY );

I'm guessing that you're over-running the bounds of your array. Also
consider not using arrays but using std::vector (cf.
http://www.parashift.com/c++-faq-lit....html#faq-34.1) or a
Matrix class:

http://www.parashift.com/c++-faq-lit...html#faq-16.19

Cheers! --M

Dec 8 '05 #2
I've purposefully made the MAXX and MAXY values larger then the image
that I'm using to test the code. I'll try using a vector instead but
I'd like to know why my code doesn't work since it seems to be correct.

Dec 8 '05 #3
ri*************@gmail.com wrote:
I'm writting a peice of code for the numerical reconstruction of
digital holograms. I have carried out a two dimensional fft using
fftw. The output of which is stored in an array of fftw_complex.

After the fft I need to manipulate the output array so I have used a
nested for loop to cycle through the values. My code for the loop
looks like this

double intensity_array [MAXX][MAXY];
for (int xx=0 ; xx < width ; xx++)
{
for (int yy=0 ; yy < height ; yy++)


What ar ethe relationship between width and MAXX and height
and MAXY?
What is the definition of out?
Dec 8 '05 #4
ri*************@gmail.com wrote:
I've purposefully made the MAXX and MAXY values larger then the image
that I'm using to test the code. I'll try using a vector instead but
I'd like to know why my code doesn't work since it seems to be correct.


First, please quote the message you are responding to. It makes it
easier for others not using Google Groups to follow the discussion.
(BTW, you can do it automatically on Google Groups by clicking "show
options" and then "Reply" in the revealed header.)

Second, it's hard for us to tell much of anything from the little code
you posted, but such problems are usually caused by wayward pointers
and bad array indices (sometimes in a completely different part of the
program!). If you can reduce your code to the smallest compilable
sample that demonstrates the problem, we might be able to help you
more. Otherwise, check into a tool for your system that does bounds
checking for you (e.g., valgrind).

Cheers! --M

Dec 8 '05 #5
MAXX and MAXY are defined as

#define MAXX 3000
#define MAXY 3000

width and height are the dimensions of an image opened from file. At
the moment I am testing with an image 752x574 pixels.

out is an array[width*height][2] which holds the real and imaginary
parts of a set of complex numbers in seperate columns.

Dec 8 '05 #6
ri*************@gmail.com wrote:
..

out is an array[width*height][2] which holds the real and imaginary
parts of a set of complex numbers in seperate columns.

No it is not. C++ doesn't let you declare arrays with variable sizes.

What is the DECLARATION of out?
How did the values get into out?
Dec 8 '05 #7

Ron Natalie wrote:
ri*************@gmail.com wrote:
.

out is an array[width*height][2] which holds the real and imaginary
parts of a set of complex numbers in seperate columns.

No it is not. C++ doesn't let you declare arrays with variable sizes.

What is the DECLARATION of out?
How did the values get into out?


out is the result of my fft.

fftw_complex *in;
fftw_complex *out;
in = (fftw_complex *)fftw_malloc(width*height * sizeof(fftw_complex));
out = (fftw_complex *)fftw_malloc(width*height * sizeof(fftw_complex));

p = fftw_plan_dft_2d(width, height, in, out, FFTW_BACKWARD,
FFTW_ESTIMATE);
fftw_execute(p);

Dec 8 '05 #8
richardveitc...@gmail.com wrote:
Ron Natalie wrote:
ri*************@gmail.com wrote:
.

out is an array[width*height][2] which holds the real and imaginary
parts of a set of complex numbers in seperate columns.

No it is not. C++ doesn't let you declare arrays with variable sizes.

What is the DECLARATION of out?
How did the values get into out?


out is the result of my fft.

fftw_complex *in;
fftw_complex *out;
in = (fftw_complex *)fftw_malloc(width*height * sizeof(fftw_complex));
out = (fftw_complex *)fftw_malloc(width*height * sizeof(fftw_complex));

p = fftw_plan_dft_2d(width, height, in, out, FFTW_BACKWARD,
FFTW_ESTIMATE);
fftw_execute(p);


Off-topic, but a cursory search through the FFTW docs
(http://www.fftw.org/fftw3_doc/Multi_...s-of-Real-Data)
yielded:

"Since the complex data is slightly larger than the real data, some
complications arise for in-place transforms. In this case, the final
dimension of the real data **must be padded with extra values to
accommodate the size of the complex data**-two values if the last
dimension is even and one if it is odd." (emphasis added)

If you have not padded, that could be the memory overrun causing your
problem.

I reiterate: post a minimal compilable sample of code that demonstrates
the problem or get a bounds checking tool for your system.

Cheers! --M

Dec 8 '05 #9

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

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
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...
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....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
3
by: David Vincent | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hello I'm hoping to get some insight into a situation that seems odd to me. My Python experience is limited; I've just started using the unittest module....
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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...
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...

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.