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

nested for loops behaving oddly

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


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

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

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

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

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

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

P: n/a

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

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

Replies have been disabled for this discussion.