increment and loop error!

S

Sheldon

Hi,

I have a problem that and the answer eludes me:

I am converting a 2D array to a 1D array within a for loop that looks
something like this:

ii=0;
for (i = 0; i < 300; i++) {
for (j = 0; j < 250; j++) {
oneD1[ii] = TwoD1[j];
oneD2[ii] = TwoD2[j];
oneD3[ii] = TwoD3[j];
ii++;
}
}

I get a segmentation fault and when I print out ii, the values exceeds
the product of 300*250.

Why does this happen when ii starts at zero and increments with 1 after
use? Even if I change ii++ to ii += 1, the same thing happens.

Thanks in advance,
Sheldon
 
K

Keith Thompson

Sheldon said:
I have a problem that and the answer eludes me:

I am converting a 2D array to a 1D array within a for loop that looks
something like this:

ii=0;
for (i = 0; i < 300; i++) {
for (j = 0; j < 250; j++) {
oneD1[ii] = TwoD1[j];
oneD2[ii] = TwoD2[j];
oneD3[ii] = TwoD3[j];
ii++;
}
}

I get a segmentation fault and when I print out ii, the values exceeds
the product of 300*250.

Why does this happen when ii starts at zero and increments with 1 after
use? Even if I change ii++ to ii += 1, the same thing happens.


Your actual code "looks something like this"? Obviously it doesn't
look exactly like what you posted, and the source of your problem is
probably one of the differences between your actual code and the loose
approximation you posted.

(It's obvious that changing ii++ to ii += 1 won't make any difference;
they do exactly the same thing.)

Don't make us guess. Show us real code that exhibits the problem.
 
W

Walter Roberson

I am converting a 2D array to a 1D array within a for loop that looks
something like this:
ii=0;
for (i = 0; i < 300; i++) {
for (j = 0; j < 250; j++) {
oneD1[ii] = TwoD1[j];
oneD2[ii] = TwoD2[j];
oneD3[ii] = TwoD3[j];
ii++;
}
}

I get a segmentation fault and when I print out ii, the values exceeds
the product of 300*250.

What type have you declared ii as? What is the maximum value that can
be stored with that type? *Exactly* how are you printing out the
value?

My -guess- is that you have declared ii as being an int, that
your int only hold 32767, and that when you print out the value,
you are not using a format element appropriate for the type of
variable you are using. Try converting the code so ii is type long,
and print it out with a %ld format element.
 
S

Sheldon

Keith Thompson skrev:
Sheldon said:
I have a problem that and the answer eludes me:

I am converting a 2D array to a 1D array within a for loop that looks
something like this:

ii=0;
for (i = 0; i < 300; i++) {
for (j = 0; j < 250; j++) {
oneD1[ii] = TwoD1[j];
oneD2[ii] = TwoD2[j];
oneD3[ii] = TwoD3[j];
ii++;
}
}

I get a segmentation fault and when I print out ii, the values exceeds
the product of 300*250.

Why does this happen when ii starts at zero and increments with 1 after
use? Even if I change ii++ to ii += 1, the same thing happens.


Your actual code "looks something like this"? Obviously it doesn't
look exactly like what you posted, and the source of your problem is
probably one of the differences between your actual code and the loose
approximation you posted.

(It's obvious that changing ii++ to ii += 1 won't make any difference;
they do exactly the same thing.)

Don't make us guess. Show us real code that exhibits the problem.


The code is over 1800 lines long and I didn't think it was appropriate
to post it all. The function IS this:

static int TwoDToOneD(void) {

int i,j, ii;

MALLOC(ret.lightcon, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.valconcen, sizeof(int*)*AreaRow/res*AreaCol/res);
MALLOC(ret.bias100, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.bias75, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.bias50, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.bias25, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.latitude, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.longitude, sizeof(float*)*AreaRow/res*AreaCol/res);

ii = 0;
printf("Row: %d\tCol: %d\n", AreaRow/res, AreaCol/res);
for (i = 0; i < AreaRow/res; i++) {
for (j = 0; j < AreaCol/res; j++) {
printf("ii: %d\ti: %d\tj: %d\n", ii, i, j);
ret.lightcon[ii] = work.lightcon2D[j];
ret.bias100[ii] = work.bias1002D[j];
ret.bias75[ii] = work.bias752D[j];
ret.bias50[ii] = work.bias502D[j];
ret.bias25[ii] = work.bias252D[j];
ret.latitude[ii] = work.latitude2D[j];
ret.longitude[ii] = work.longitude2D[j];
ret.valconcen[ii] = work.valconcen2D[j];
ii++;
}
}
for (i = 0; i < AreaCol; i++) {
free(work.lightcon2D);
free(work.bias1002D);
free(work.bias752D);
free(work.bias502D);
free(work.bias252D);
free(work.latitude2D);
free(work.longitude2D);
free(work.valconcen2D);
}
free(work.lightcon2D);
free(work.bias1002D);
free(work.bias752D);
free(work.bias502D);
free(work.bias252D);
free(work.latitude2D);
free(work.longitude2D);
free(work.valconcen2D);

return 1;
}
 
S

Sheldon

Walter Roberson skrev:
I am converting a 2D array to a 1D array within a for loop that looks
something like this:
ii=0;
for (i = 0; i < 300; i++) {
for (j = 0; j < 250; j++) {
oneD1[ii] = TwoD1[j];
oneD2[ii] = TwoD2[j];
oneD3[ii] = TwoD3[j];
ii++;
}
}

I get a segmentation fault and when I print out ii, the values exceeds
the product of 300*250.

What type have you declared ii as? What is the maximum value that can
be stored with that type? *Exactly* how are you printing out the
value?

My -guess- is that you have declared ii as being an int, that
your int only hold 32767, and that when you print out the value,
you are not using a format element appropriate for the type of
variable you are using. Try converting the code so ii is type long,
and print it out with a %ld format element.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton



Thanks! I have declared it as "int".
Will change and try again.

/S
 
B

Ben Bacarisse

Sheldon said:
Hi,

I have a problem that and the answer eludes me:

I am converting a 2D array to a 1D array within a for loop that looks
something like this:

When odd things are happening, posting exact (and complete,
i.e. compilable) code is the only way.
ii=0;
for (i = 0; i < 300; i++) {
for (j = 0; j < 250; j++) {
oneD1[ii] = TwoD1[j];
oneD2[ii] = TwoD2[j];
oneD3[ii] = TwoD3[j];
ii++;
}
}


If I wrap this up in a main function and declare the arrays I get
exactly what I (and you) would expect (ii==300*250 after the loops).
The problem is elsewhere. Can you produce the problem in a small,
complete, example?
 
B

Ben Bacarisse

Sheldon said:
Keith Thompson skrev:
Sheldon said:
I have a problem that and the answer eludes me:

I am converting a 2D array to a 1D array within a for loop that looks
something like this:

ii=0;
for (i = 0; i < 300; i++) {
for (j = 0; j < 250; j++) {
oneD1[ii] = TwoD1[j];
oneD2[ii] = TwoD2[j];
oneD3[ii] = TwoD3[j];
ii++;
}
}

I get a segmentation fault and when I print out ii, the values exceeds
the product of 300*250.

Why does this happen when ii starts at zero and increments with 1 after
use? Even if I change ii++ to ii += 1, the same thing happens.


Your actual code "looks something like this"? Obviously it doesn't
look exactly like what you posted, and the source of your problem is
probably one of the differences between your actual code and the loose
approximation you posted.

(It's obvious that changing ii++ to ii += 1 won't make any difference;
they do exactly the same thing.)

Don't make us guess. Show us real code that exhibits the problem.


The code is over 1800 lines long and I didn't think it was appropriate
to post it all. The function IS this:

static int TwoDToOneD(void) {

int i,j, ii;

MALLOC(ret.lightcon, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.valconcen, sizeof(int*)*AreaRow/res*AreaCol/res);
MALLOC(ret.bias100, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.bias75, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.bias50, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.bias25, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.latitude, sizeof(float*)*AreaRow/res*AreaCol/res);
MALLOC(ret.longitude, sizeof(float*)*AreaRow/res*AreaCol/res);

ii = 0;
printf("Row: %d\tCol: %d\n", AreaRow/res, AreaCol/res);
for (i = 0; i < AreaRow/res; i++) {
for (j = 0; j < AreaCol/res; j++) {
printf("ii: %d\ti: %d\tj: %d\n", ii, i, j);
ret.lightcon[ii] = work.lightcon2D[j];
ret.bias100[ii] = work.bias1002D[j];
ret.bias75[ii] = work.bias752D[j];
ret.bias50[ii] = work.bias502D[j];
ret.bias25[ii] = work.bias252D[j];
ret.latitude[ii] = work.latitude2D[j];
ret.longitude[ii] = work.longitude2D[j];
ret.valconcen[ii] = work.valconcen2D[j];
ii++;
}
}
for (i = 0; i < AreaCol; i++) {


i < AreaCol/res? AreaCol look suspicious given the loop above.
 
R

Richard Tobin

The code is over 1800 lines long and I didn't think it was appropriate
to post it all. The function IS this:
[...]

Assuming that your MALLOC macro does the obvious thing, and that
you fix the bug mentioned by someone else in the free loop, it looks
basically OK. Some possibilities:

- does you MALLOC macro check for error returns from malloc?
- are the 2d arrays set up correctly with the right size?
- do the expressions
sizeof(float*)*AreaRow/res*AreaCol/res
not overflow? (Because you did not bracket AreaCol/res, you are
in fact calculating a number res times bigger than the size.)

and the obvious one:

- have you run it under a debugger? where did it fail?

-- Richard
 
C

CBFalconer

Sheldon said:
.... snip ...

The code is over 1800 lines long and I didn't think it was
appropriate to post it all. The function IS this:

Then cut it down to a minimum complete _compilable_ program that
demonstrates the problem and publish that. In the process you are
very likely to figure it out, and actually learn something.
 

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
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top