Global array

Posted By: Sphin

Global array - 10/08/17 16:59

Code:
var Trend[5];

void main() {
	int i;
	for(i = 0; i < 100; i++) {
 		Trend[i] = i;
		printf("nTrend[%d]: %.0f",i,Trend[i]);
	}
}


Output:
Code:
...
Trend[97]: 97
Trend[98]: 98
Trend[99]: 99


How many elements does this array contain?
Posted By: DdlV

Re: Global array - 10/08/17 17:47

Hi Sphin,

I believe the correct question is: What have you coded to be sure your intended # of elements hasn't been exceeded? laugh

HTH.
Posted By: Sphin

Re: Global array - 10/09/17 16:51

Hi DdlV,

the story behind: I had a problem building the average of a number of values of an array filled through a loop - it was always too high. The cause: there was a Trend[5]-element that I thought was not permitted to exist. I expected the script to crash in such a case. So I extended the try a bit, I also tried a higher number but with #115 the script always crashes. This works only if the array was defined as global, defined as local the script crashes as it should. Okay, seems to be a C secret ... laugh

bye
Posted By: jcl

Re: Global array - 10/10/17 06:23

C/C++ does not check array lengths, so you can write past the array size. What then happens depends on what's in the memory past the array. Chance is that's another global array, or some variable that you then overwrite.
Posted By: Sphin

Re: Global array - 10/10/17 17:23

Thanks, I didn't know this.
© 2024 lite-C Forums