PDA

View Full Version : Array takes control over my loop! C


MK2
10-08-2005, 06:38 PM
Hello,

can somebody please take a look at my program and tell my what exactly is the problem with my array? i just wanna write the factor in my array with a for loop but somehow my array takes control over the loop...?
.

#include <stdio.h>

int main()
{
unsigned int aArray [3][3]; //test Array
unsigned int zeile=0, //rows
spalte=0, //col
factor=2;
for (zeile=0; zeile<4; zeile++) // counting up the Rows
{
printf("--- ROW: %d !\n",zeile);
getchar();

for (spalte=0; spalte<4; spalte++) // counting up the Columns
{

printf("ROW:%d",zeile);
getchar();

printf("ColumnValue:%d",spalte);
getchar();

aArray[zeile][spalte]=factor; // VERY STRANGE BEHAVIOR!! Makes an endless loop!
}
}
printf("Loop End!");
getchar();
return 0;
}

.
Looks like the Code-Flag isnt that good. sorry for the lost formatation...but it should compile anyway (if you need to)...

KaMe
10-08-2005, 10:13 PM
I don't get an endless loop here. Have you tried pressing enter after execution? Those getchar() calls seem to be there only to give the user some time to think.

Oh, and C arrays start at 0, so you should go up to 2 on those indexing variables.

MK2
10-08-2005, 11:08 PM
Hi, thanks for looking!

Yes, these getchars are made to "stop" the prog. a bit like break points.
The intressting thing is that if i execute the Prog. and the row count reaches 3, the array in the Column counter sets the counter for the rows back to two... i really dont understand why this happens... I never reach the Loop End! Message :(

Heres a picture from the console...
http://www.syntetics.ch/mk/BUG.gif

KaMe
10-08-2005, 11:30 PM
As I said before, you should have the loops like this:
for (zeile=0; zeile<3; zeile++) and
for (spalte=0; spalte<3; spalte++)

What probably is happening is that your compiler is allocating your variables next to each other in memory. When you go over the bounds of your array ( aArray[3] ), you are actually writing over your zeile variable.

When you declare an array like this:
int a[3];

the elements you have are:
a[0], a[1] and a[2].

so when you write to a[3], you are actually writing past the end of your array. And the cool thing is that no one tells you about that, until you run your program and get some wierd error...

Kencho
10-08-2005, 11:40 PM
I think you're causing a memory corruption by going out of the array indices. That might explain the strange behaviors. This is (hipothetically) the "memory map" of your program:
(aArray:9)(zeile:1)(spalte:1)(factor:1)
12 ints in total. I'll post the memory map along the sequence:

0 0 0 0 0 0 0 0 0 0 0 2 //This is just when entering both loops. zeile=0, spalte=0, factor=2
2 0 0 0 0 0 0 0 0 0 1 2 //zeile=0, spalte=1, factor=2
2 2 0 0 0 0 0 0 0 0 2 2 //zeile=0, spalte=2, factor=2
2 2 2 0 0 0 0 0 0 0 3 2 //zeile=0, spalte=3, factor=2
2 2 2 2 0 0 0 0 0 1 0 2 //zeile=1, spalte=0, factor=2
2 2 2 2 2 0 0 0 0 1 1 2 //zeile=1, spalte=1, factor=2
2 2 2 2 2 2 0 0 0 1 2 2 //zeile=1, spalte=2, factor=2
2 2 2 2 2 2 2 0 0 1 3 2 //zeile=1, spalte=3, factor=2
2 2 2 2 2 2 2 2 0 2 0 2 //zeile=2, spalte=0, factor=2
2 2 2 2 2 2 2 2 2 2 1 2 //zeile=2, spalte=1, factor=2
2 2 2 2 2 2 2 2 2 2 2 2 //zeile=2, spalte=2, factor=2; Have you seen what happened here?

As you can see, because of an array overrun, you've flooded another variable, in this case the one that kept track of the rows. This isn't exactly what happens, as the output log of your program doesn't correspond, but I'm pretty sure it's happening this. Try changing the value of factor and see the results.

As KaMe told, the loops should go from 0 to 2, so use for(zeile=0;zeile<3;zeile++) ;) When you declare an array, the number you pass is the number of cells of that array, not the number of the last one ;)

Hope that helps

MK2
10-08-2005, 11:59 PM
okay, i get it now!
Runs fine again... as you said, i increased the array size
I didnt know that i dont get a message from the Compiler or the Prog itself when writing more data in the array than it can take. But its good to have such a mistake... because the prog. is for my trainig to understand the arrays... now i really learned something. :)

1. When i write int a[3] it means 3 elements(0,1,2) not from 0 to 3.
2. I shall not write 4 things in an array like a[3], the result are BUGS!
3 If iam not sure how much i can write in my array i should a) dont use one or b) make it real big ([4,234,567,899*100] or something)! :)

Thank you for your fast help!
Iam can now play again with these arrays and find out how they work!

MK2
10-09-2005, 12:11 AM
Kencho:

yes, i tried it again and it was the factor which hijacked the loop!
With your explanation i think i understand what happening inside my memory.

I thank you also that you took the time to help me!

phantastikfan
10-11-2005, 08:16 AM
3 If iam not sure how much i can write in my array i should a) dont use one or b) make it real big ([4,234,567,899*100] or something)! :)


You should NEVER use solution b), if you want to get a serious programmer, because that's really bad programming style!
Using arrays ever needs to know how big it is, in a programming language like C/C++ unlike skripting languages like PHP or Python, here you can use arrays without mentioning how big they are or even what data type might there be in (it even doesn't matter whether there is more than one data type, but that is another topic!)

rendermaniac
10-11-2005, 09:55 AM
You don't need to set the length if you use STL vectors. This is about as close as you'll get to dynamic arrays in C++. See http://www.cprogramming.com/tutorial/stl/vector.html

However I don't think they are 2 dimensional. This shouldn't be a problem as long as you know how long a row is as you can just use modulus to split the rows up.

Simon

MK2
10-11-2005, 11:59 PM
@ phantastikfan

Das war nur ein Witz! Ich denke kaum dass jemand überhaupt soviel Memory hat :)

Mein Problem war das ich bei der Deklaration zwar blah[4] schreibe aber die Nummerierung
bei Null beginnt. KaMe hat mir das sehr gut erklärt und seit dem habe ich keine Probleme mehr!

Ich werde mich in Zukunft immer darauf achten das mir das nicht nochmals passiert! Wie gesagt ich habe was gelernt und das so ziemlich mit einem Schlag in die Fresse :) Du kannst dir nicht vorstellen wie ich geflucht habe, hehehe.

@rendermaniac

Thanks for that tip! I will check that out!

Kencho
10-14-2005, 04:57 PM
@MK2, I suggest you to take a look to pointers theory first, and the usage of functions like malloc & free in C or the reserved words new & delete in C++. That will help you greatly to understand how to work with dynamically changing memory in your applications ;)

Ignifex
10-28-2005, 12:35 PM
However I don't think they are 2 dimensional. This shouldn't be a problem as long as you know how long a row is as you can just use modulus to split the rows up.


Vectors can be 2 dimensional, it's just that most programmers prefer 1D, because it's usually easier to handle.


//example 1
vector<int> 1Dvector;
for (int i = 0; i < 5; i++){
1Dvector.push_back(i);
}
1Dvector[2] = 2;
1Dvector.at(2) = 2; //this is already 2 of course :)
//example 2
vector<vector<int> > 2Dvector; //mind the space between > >, otherwise it's the >> operator.
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
1Dvector.push_back(i*10+j);
}
}
2Dvector[2][4] = 24;
2Dvector.at(2).at(4) = 24; //this is, once again, already 24 ;)



Normally you would declare a vector like in example 1, but in example 2, the vector is declared as a 2D vector and can be accessed like I showed.

CGTalk Moderation
10-28-2005, 12:35 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.