// There is a problem in this source file related to // array indices being out of bounds. First, compile // this file by issuing the following command: // // gcc -g -Wall -std=gnu99 -o backtrace backtrace.c // // Next, run the program under GDB: // // gdb ./backtrace // // Set a conditional breakpoint to take a look at the // status of the variables inside of the Store() // function when index is greater than 100. As with // conditional.c, it may be obvious what the problem // is but it would still be good practice to pretend // to investigate what is going on with the code. // // Some commands you'll want to use: // "b" to set a breakpoint // "n" to move to the next statement, skipping over // function calls // "s" to step to the next statement, not skipping // over function calls // "p" to print values // "bt" to print a backtrace of the call stack for // the current state of the program // "frame" to switch to a different stack frame to // look at the variables in it // // The section slides have more information, and you // can also use "help [command]" within GDB to see // how to use them. #include static void Store(int* array, int index, int value) { array[index] = value; } // Append the contents of array a to the end of array // b. a_len is the number of elements in array a and // b_len is the number of elements in array b, so at // the end array b should contain a_len + b_len elements // in total. static void Append(int* a, int a_len, int* b, int b_len) { for (int i = 0; i < b_len; ++i) { Store(b, b_len + i, a[i]); } } int main(int argc, char* argv[]) { // Create an array of size 100 and store 40 values // in it. int a[100]; int a_len = 40; for (int i = 0; i < a_len; ++i) { a[i] = i; } // Create an array of size 100 and store 60 values // int it. int b[100]; int b_len = 60; for (int i = 0; i < b_len; ++i) { b[i] = i; } // Append the contents of array a to the end of array // b and then print the combination of the two. Append(a, a_len, b, b_len); for (int i = 0; i < a_len + b_len; ++i) { printf("%d\n", b[i]); } return 0; }