nested for loops behaving oddly

  • Thread starter richardveitch77
  • Start date
R

richardveitch77

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.
 
M

mlimber

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-lite/containers.html#faq-34.1) or a
Matrix class:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.19

Cheers! --M
 
R

richardveitch77

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.
 
R

Ron Natalie

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?
 
M

mlimber

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
 
R

richardveitch77

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.
 
R

Ron Natalie

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?
 
R

richardveitch77

Ron said:
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);
 
M

mlimber

Ron said:
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....html#Multi_002dDimensional-DFTs-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
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top