I have the following code:
#include <stdlib.h>
#include <stdio.h>
#define SIZE 100
int* arr;
main()
{
int i;
arr = (int*)malloc(SIZE*sizeof(int));
if (arr == NULL) {
printf("Could not allocate SIZE(=%d)", SIZE);
}
for (i=0; i<SIZE; i++) {
arr[i] = 0;
}
free(arr);
}
I wan’t to watch for arr[10] and see when that array element is being modified.
How can I do this? gdb says the following:
$ gcc -g main.c
$ gdb a.out
...
(gdb) watch arr[10]
Cannot access memory at address 0x28
Is there a way to tell gdb to watch an invalid memory and stop only when it becomes valid?
PS: I have gdb versions 6.0, 6.3, 6.4, 6.6, 6.8, 7.0 and 7.1
Thanks
For some reason, I was using gdb-6.3 (it was in my PATH and I didn’t notice it). But, when I tried with gdb-7.1 it worked!
Since gdb 7.0 you can watch memory that isn’t yours at the moment.
With the following source code:
You can compile with:
And then debug with:
Hope it helps for somebody else.
NOTE: I tried adding this as a comment in the post by Neil, but as it wasn’t formatted, I preferred writing an answer to my question.