nested for loops behaving oddly

Discussion in 'C++' started by richardveitch77@gmail.com, Dec 8, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. mlimber Guest

    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-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
    mlimber, Dec 8, 2005
    #2
    1. Advertising

  3. Guest

    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, 2005
    #3
  4. Ron Natalie Guest

    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?
    Ron Natalie, Dec 8, 2005
    #4
  5. mlimber Guest

    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
    mlimber, Dec 8, 2005
    #5
  6. Guest

    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, 2005
    #6
  7. Ron Natalie Guest

    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?
    Ron Natalie, Dec 8, 2005
    #7
  8. Guest

    Ron Natalie wrote:
    > 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, 2005
    #8
  9. mlimber Guest

    wrote:
    > Ron Natalie wrote:
    > > 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....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
    mlimber, Dec 8, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Vincent

    unittest behaving oddly

    David Vincent, Jun 20, 2006, in forum: Python
    Replies:
    3
    Views:
    1,042
    Fredrik Lundh
    Jun 20, 2006
  2. Ben
    Replies:
    7
    Views:
    304
    Steven D'Aprano
    Dec 28, 2006
  3. Siam
    Replies:
    4
    Views:
    408
    Pete Becker
    Jul 27, 2006
  4. Replies:
    13
    Views:
    189
    Ilya Zakharevich
    Feb 22, 2011
  5. babygodzilla
    Replies:
    4
    Views:
    114
    babygodzilla
    Sep 18, 2007
Loading...

Share This Page