473,473 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

increment and loop error!

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[i][j];
oneD2[ii] = TwoD2[i][j];
oneD3[ii] = TwoD3[i][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

Dec 6 '06 #1
8 1639
"Sheldon" <sh******@gmail.comwrites:
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[i][j];
oneD2[ii] = TwoD2[i][j];
oneD3[ii] = TwoD3[i][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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 6 '06 #2
In article <11**********************@16g2000cwy.googlegroups. com>,
Sheldon <sh******@gmail.comwrote:
>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[i][j];
oneD2[ii] = TwoD2[i][j];
oneD3[ii] = TwoD3[i][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
Dec 6 '06 #3

Keith Thompson skrev:
"Sheldon" <sh******@gmail.comwrites:
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[i][j];
oneD2[ii] = TwoD2[i][j];
oneD3[ii] = TwoD3[i][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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
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[i][j];
ret.bias100[ii] = work.bias1002D[i][j];
ret.bias75[ii] = work.bias752D[i][j];
ret.bias50[ii] = work.bias502D[i][j];
ret.bias25[ii] = work.bias252D[i][j];
ret.latitude[ii] = work.latitude2D[i][j];
ret.longitude[ii] = work.longitude2D[i][j];
ret.valconcen[ii] = work.valconcen2D[i][j];
ii++;
}
}
for (i = 0; i < AreaCol; i++) {
free(work.lightcon2D[i]);
free(work.bias1002D[i]);
free(work.bias752D[i]);
free(work.bias502D[i]);
free(work.bias252D[i]);
free(work.latitude2D[i]);
free(work.longitude2D[i]);
free(work.valconcen2D[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);

return 1;
}

Dec 6 '06 #4

Walter Roberson skrev:
In article <11**********************@16g2000cwy.googlegroups. com>,
Sheldon <sh******@gmail.comwrote:
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[i][j];
oneD2[ii] = TwoD2[i][j];
oneD3[ii] = TwoD3[i][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

Dec 6 '06 #5
"Sheldon" <sh******@gmail.comwrites:
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[i][j];
oneD2[ii] = TwoD2[i][j];
oneD3[ii] = TwoD3[i][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?

--
Ben.
Dec 6 '06 #6
"Sheldon" <sh******@gmail.comwrites:
Keith Thompson skrev:
>"Sheldon" <sh******@gmail.comwrites:
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[i][j];
oneD2[ii] = TwoD2[i][j];
oneD3[ii] = TwoD3[i][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.
<sig snipped>
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[i][j];
ret.bias100[ii] = work.bias1002D[i][j];
ret.bias75[ii] = work.bias752D[i][j];
ret.bias50[ii] = work.bias502D[i][j];
ret.bias25[ii] = work.bias252D[i][j];
ret.latitude[ii] = work.latitude2D[i][j];
ret.longitude[ii] = work.longitude2D[i][j];
ret.valconcen[ii] = work.valconcen2D[i][j];
ii++;
}
}
for (i = 0; i < AreaCol; i++) {
i < AreaCol/res? AreaCol look suspicious given the loop above.

--
Ben.
Dec 6 '06 #7
In article <11*********************@80g2000cwy.googlegroups.c om>,
Sheldon <sh******@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 6 '06 #8
Sheldon wrote:
>
.... 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 7 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: deko | last post by:
I need to populate a file with past unix time stamps over a period of time specified by the user. The time stamps should be evenly spaced, based on user-defined parameters. My question is: How...
6
by: john brown | last post by:
I'm new to perl programming and would like some help if at all possible. I'm trying to make a simple script where I can input a filename. To be exact something like "/home/song1.mp3". I want to...
8
by: Mantorok Redgormor | last post by:
The only adder I was able to come up with for incrementing by one, uses a for loop. I was trying to do this without using a for loop while emulating i++(it's for obfuscated code) Anyone know...
13
by: kailasam | last post by:
Hello, Iam having a doubt. Which is more efficient post increment or Pre increment? I have read that preincrement is efficient than Post increment. Iam not able to think how it is? For an...
5
by: blade_in_exile | last post by:
initialize a var to 1 outside of your mysql_fetch_array() loop. Then, within the mysql_fetch_array() loop, use the var as necessary and increment it by 1
5
by: per9000 | last post by:
Hi, today I experimented with the increment/++ operator. It seems the behavior depends on the compiler (I used cl/gcc + cs/mcs) and the language (I tested C and C#). Is the ++-operator well...
0
chumlyumly
by: chumlyumly | last post by:
Hello scripters - OS: Mac OSX Language: PHP w/ MySQL database I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The...
1
by: avinash6174 | last post by:
#include <iostream> using namespace std; typedef char TUCHAR8; typedef signed char TINT8; /* Must force decl to be signed */ typedef unsigned char TUINT8; /* Must force decl to be...
2
by: agarwalsunitadhn | last post by:
Hello I have some problem in for loop. actually i want to write some procedures based on oracle and i have to increment 5 for each loop. i want to know is it possible to change the default...
1
by: shivanee | last post by:
I am using 2 froms i.e salaryinfo is the main form, and salaryincrement is the subform. By selecting the empno i hv to enter his joining salary i.e.totalctc . If there is any increment then i hv to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.