Hi Can anyone help me out with this! thesis problem PLease :(

A

Alan

Hi, I m working on thesis on mechanical engieering. I m using C to
solve a particualr problem.
I have this code underneath which is giving me problems:


for (t=0;t<=maxt;t++){
printf("testing loop\n");
for (x=0;x<=maxx;x++){
for(y=0;y<=maxy;y++){
printf(".");
if (x==0){
printf("hello");
tp[t][x][y]=tp[t-1][x+1][y]; //Problem here
}
}}}

I simplified as much as possible.

the problem is at the indicated line.

tp is a 3 dimensional array, allocated using calloc().

Im filling array with some values.

When the program arrives at this line, it exits and prints only one "."
and one "hello".

But without this line, it prints "." and "hello" according to the loop
iterations.

Can anhybody indetify the problem please ?

Im using Lcc win as a compiler and the array is a pointer to pointer to
pointer i.e. --> double ***tp

Thanks!!!

Alan
 
S

santosh

Alan said:
Hi, I m working on thesis on mechanical engieering. I m using C to
solve a particualr problem.
I have this code underneath which is giving me problems:


for (t=0;t<=maxt;t++){
printf("testing loop\n");
for (x=0;x<=maxx;x++){
for(y=0;y<=maxy;y++){
printf(".");
if (x==0){
printf("hello");
tp[t][x][y]=tp[t-1][x+1][y]; //Problem here
}
}}}

I simplified as much as possible.

When t == 0, tp[t-1]... is accessing an area of memory which is not a
part of the array. This is probably what's causing UB.
 
D

David Resnick

Alan said:
Hi, I m working on thesis on mechanical engieering. I m using C to
solve a particualr problem.
I have this code underneath which is giving me problems:


for (t=0;t<=maxt;t++){
printf("testing loop\n");
for (x=0;x<=maxx;x++){
for(y=0;y<=maxy;y++){
printf(".");
if (x==0){
printf("hello");
tp[t][x][y]=tp[t-1][x+1][y]; //Problem here
}
}}}

I simplified as much as possible.

the problem is at the indicated line.

tp is a 3 dimensional array, allocated using calloc().

Im filling array with some values.

When the program arrives at this line, it exits and prints only one "."
and one "hello".

But without this line, it prints "." and "hello" according to the loop
iterations.

I'd make the "hello" line display t, x, and y. It might be
illuminating to see where/why this fails.

For example, what if x = 0 and t = 0? Looks like an out of bounds
access with tp[-1][1][y]... If maxx is also 0 the 1 is also out of
bounds.

Also, if you calloc standard doesn't guarantee that all bits 0 is a
valid float I believe, assign to 0.0 in a loop.

Also, if you calloc it this array as one big block, you can't use
multiple subscripts. See http://c-faq.com/aryptr/dynmuldimary.html.

-David
 
E

Eric Sosman

santosh wrote On 11/20/06 13:47,:
Alan said:
Hi, I m working on thesis on mechanical engieering. I m using C to
solve a particualr problem.
I have this code underneath which is giving me problems:


for (t=0;t<=maxt;t++){
printf("testing loop\n");
for (x=0;x<=maxx;x++){
for(y=0;y<=maxy;y++){
printf(".");
if (x==0){
printf("hello");
tp[t][x][y]=tp[t-1][x+1][y]; //Problem here
}
}}}

I simplified as much as possible.


When t == 0, tp[t-1]... is accessing an area of memory which is not a
part of the array. This is probably what's causing UB.

Another potential trouble spot is the [x+1] index,
once the program gets up to x == maxx.

Yet another potential trouble spot is the use of <=
instead of < in the loop conditions. It might be all
right, but only if the array dimensions are a little
larger than might be expected. (For example, if maxt
is 5, the leftmost dimension requires at least 6 elements:
0,1,2,3,4,5 inclusive are six distinct index values.)
 
M

Martin Ambuhl

Alan said:
for (t=0;t<=maxt;t++){
printf("testing loop\n");
for (x=0;x<=maxx;x++){
for(y=0;y<=maxy;y++){
printf(".");
if (x==0){
printf("hello");
tp[t][x][y]=tp[t-1][x+1][y]; //Problem here
}
}}}

In the first time through the loop t==0 so the assignment refers to
tp[-1][x+1][y], which is out of bounds.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top