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

10 October 2005, 05:38 PM

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
for (zeile=0; zeile<4; zeile++) // counting up the Rows
printf("--- ROW: %d !\n",zeile);

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



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

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

10 October 2005, 09: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.

10 October 2005, 10: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...

10 October 2005, 10: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...

10 October 2005, 10: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:
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

10 October 2005, 10: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!

10 October 2005, 11:11 PM

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!

10 October 2005, 07: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!)

10 October 2005, 08: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

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.


10 October 2005, 10: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.


Thanks for that tip! I will check that out!

10 October 2005, 03: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 ;)

10 October 2005, 11:35 AM
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[2] = 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++){
2Dvector[2][4] = 24; = 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 October 2005, 11:35 AM
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.