Dear all

Can anyone explain to me how a (local) counter can be modified in a loop without being touched in the code? ... and how I am best to debug this?

Thanks

Erich

void test(double *x, int l, int N, int K, double *p, Comp * y)
{
  int q, w;
  double *buf1 = new double[l];
  double *buf2 = new double[l];
  int i_out    = 0;

  for (int n = 0 ; n < l; n=n+N)
  {
    /* ... main code, does not touch i_out */

    i_out = i_out + K;
  }

  delete[] buf1;
  delete[] buf2;
}

Could you please give more details?

Hi Gerard

I am running this code (snippet above), and the counter goes semi-random first time it is accessed, i.e.

../bin/my_code < input > output

.. Processing frame (idx_frame) 0
c_out = 0                                        # initialise c_out
..
<some other output>
..
c_out = 1061908948                               # what changed it?
y[c_out].r = buf2[0];
Segmentation fault (core dumped)
# only afterwards should c_out be modified/incremented

So, somehow c_out is modifed, but it's a local variable? How?

Thanks for your help.

Erich

To debug this, build with debug symbols, set your debugger to watch the variable you are interested in, and when the variable is changed the debugger will interrupt the program and you can see what changed the variable.

Your original example does not include a c_out variable. It might as well be a guessing game for all of us until you post a representitive example we can compile and run ourselves.
Reduce your problem to the minimum code necessary to repreduce the effect and post that here. In all likelihood, this will help you narrow the problem space yourself.

Hi l7Sqr

Sorry for the difference, the variable is c_out, not i_out, my typo in the code snippet.

As to reducing the code, that's what I am working on.

Thanks for your help.

Erich

Hi Moschops

What debugging tool would you recommend?

As you will have already figured out, I am not a C/C++ programmer, but inherited the code and am now working on getting it running from matlab via mex.

Thanks

Erich

I generally use gdb. If you're on a windows system, you'll have to use something else.

Thanks

Erich

Looks as if fft_mayer is the culprit. Any comments?

Hardware watchpoint 2: ci

Old value = 0
New value = 1061908948
fht (fz=0x7ffffffef260, n=96) at fft_mayer.c:255
255                 fi[k1]  = f1        + b;

You are modifying variable names, providing incomplete examples and giving snippets of a debugger session that does not seem to match the original example. This makes it very hard for us to diagnose your true problem.

My guess is that you overrun a buffer someplace - perhaps k1 is larger than the size of fi - and that is causing nasty side effects.

Again, without a complete sample we are not going to be able to properly diagnose your problem.

Hello L7Sqr

Yes, I am, but unfortunately I cannot share the code online.

But
a) debugging using gdb I found the problem
b) knowing it is fft_mayer.c I could search for it and found that it is indeed known to misbehave and I am replacing it with fftw

So, thank you all for your kind help.

Erich

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.