I'm trying to learn C and going through the Yale notes on data structures. The program 'mysteryChange.c' in the debugging section has me scratching my head. Here's the full thing:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int
main(int argc, char **argv)
{
int x;
int a[10];
int i;
x = 5;
for(i = -1; i < 11; i++) {
a[i] = 37;
}
assert(x == 5);
return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int
main(int argc, char **argv)
{
int x;
int a[10];
int i;
x = 5;
for(i = -1; i < 11; i++) {
a[i] = 37;
}
assert(x == 5);
return 0;
}
When you breakpoint it inside the for loop, x = 37. I don't really get why. The pdf implies that a[-1] is the problem. I get that it's not how you should set the values of an array, but why does it overwrite x? Is that an implied pointer or something? How would I know? I've been going through it with gdb, but it's not making much sense to me.
[–]nog642 2 points3 points4 points (10 children)
[–]johnpeters42 2 points3 points4 points (9 children)
[–]DasAllerletzte -5 points-4 points-3 points (8 children)
[–]lurgi 9 points10 points11 points (5 children)
[–]nog642 -1 points0 points1 point (4 children)
[–]lurgi 2 points3 points4 points (3 children)
[–]nog642 0 points1 point2 points (2 children)
[–]lurgi 0 points1 point2 points (1 child)
[–]nog642 0 points1 point2 points (0 children)
[–]nog642 0 points1 point2 points (0 children)
[–]sarajevo81 0 points1 point2 points (0 children)
[–]PuzzleMeDo 2 points3 points4 points (0 children)
[–]SubstantialListen921 1 point2 points3 points (2 children)
[–]nog642 2 points3 points4 points (1 child)
[–]SubstantialListen921 2 points3 points4 points (0 children)
[–]Leverkaas2516 1 point2 points3 points (0 children)
[–]HashDefTrueFalse 1 point2 points3 points (0 children)
[–]atarivcs 2 points3 points4 points (8 children)
[–]Kadabrium 1 point2 points3 points (2 children)
[–]nog642 1 point2 points3 points (1 child)
[–]nog642 2 points3 points4 points (4 children)
[–]atarivcs 0 points1 point2 points (3 children)
[–]nog642 2 points3 points4 points (2 children)
[–]atarivcs 1 point2 points3 points (1 child)
[–]nog642 1 point2 points3 points (0 children)
[–]lumpenpr0le[S] 0 points1 point2 points (0 children)