Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Functional Programming in C Control Flow - For Loops

Sebastian Nitu
Sebastian Nitu
8,616 Points

Somewhat of a problem which I do not understand...

I have attempted to "play around" with the for loop and I have got some strange results.

Here is the code:

'''

include <stdio.h>

int main() {

int numbers[] = {2, 4, 6, 8};
int sum = 0;

for(int i = 0; i <= 6; i++) {

    sum += numbers[i];
    printf("when I is %d then the operation is %d \n", i, sum);

}


return 0;

}

'''

And here are the results:

  • when I is 0 then the operation is 2
  • when I is 1 then the operation is 6
  • when I is 2 then the operation is 12
  • when I is 3 then the operation is 20
  • when I is 4 then the operation is 1606416612
  • when I is 5 then the operation is 1606416612
  • when I is 6 then the operation is 415681978
  • Program ended with exit code: 0

The question is: What's with the numbers? Where is 1606416612 coming from (and twice!) and also, what about 415681978?

Thank you very much for any assistance / advice!

3 Answers

"Random numbers" isn't entirely correct. What's happening is what's known as a buffer overflow.

You initialize the array with 4 elements of int, which Is probably...2(?) bytes each. The way C treats arrays is like pointers, so when you go to numbers[1], what you're actually getting is the value at the memory address of numbers[0] + 2 bytes.

There's nothing that alerts you when you traverse past the 'end' of the array, so when you do, the following memory addresses are just there. They may have stuff in them or they might not. They might be used by another program or they might not.

Buffer overflows are can result in your program going awry and returning weird junk, but they're PARTICULARLY DANGEROUS if you modify those overflowed values, because you're technically reaching into memory used by some other program. This could end up in your computer/OS crashing.

Working with memory with the shortcuts that C allows is really powerful, but it's a double-edged sword. It can save lots of time, but without proper bounds checking it could crash not only itself but other programs, too...and without producing any meaningful errors, thus making diagnosis...more painful than otherwise.

you have only specified 4 numbers... [3] 0, 1, 2, 3, (2, 4, 6, 8)

if that makes sense... that's why you get random numbers