5

i'm using eclipse and netbeans for c, and i'd like to check out the values of variables that are dynamically allocated in the memory when i'm debugging (both in eclipse and netbeans).

for some reason, i can only see the value of the pointer itself, and it's first item.

to illustrate: with this code:

int foo[10];

i can check the value of the entire array later on (when debugging). for example, i can check out the value of foo[7] in the watches window.

but with this code:

int *bar = malloc(10*sizeof(int));

i can only check out where bar is pointing, and the value of bar[0] (but not the other values).

how can i watch all the values of the array?


UPDATE: the issue was solved in both eclipse and netbeans.

in eclipse: right click the desired variable in the Variables window -> select Display As Array -> fill in the start index and the array length.

in netbeans: in the Watches window add a new watch with the following format:

*((bar)+0)@10

where bar should be the pointer name, 0 should be your start index and 10 should be its length

if i may add something personal: this is my first ever message on stackoverflow. i hope you found it useful.

Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41
dvir
  • 2,546
  • 2
  • 18
  • 15
  • Come to think about it, have you _tried_ `bar[1]`? It _should_ work, especially when used in code like printing it (`printf("bar[2] = %d\n", bar[2]);`). – Some programmer dude Dec 02 '11 at 15:50
  • 2
    @dvir Instead of adding the solution to your question, you should add an answer and instead of adding Solved to the title you should mark your answer as accepted. This is the proper way of doing this so that others will know that there is a solution. – Jonathan Spooner Dec 03 '11 at 18:56

2 Answers2

3

As you are on a pointer variable, the only knowledge that the debugging tool can infer automatically is that you have an address on an integer value. After that fact you can have theoretically anything behind your integer value, it is not possible for the tool to guess that the pointer is in fact the first element of an integer array.

That said, you can try to add a custom watch expression (at least on Eclipse, i don't know netbeans) which casts your pointer into the integer array. I don't know if you can cast with a precise array length. Something like (int[])bar will certainly work but maybe this form may work either (int[10])bar.

Another solution is looking directly on the memory view at the pointer address, but it is more of a mental sport to convert raw endianness hexadecimal output to integer values...

Now if your pointer is always allocated to a memory block of 10 integers, you should preferably consider to statically allocate it using the array form int bar[10];

greydet
  • 5,509
  • 3
  • 31
  • 51
2

I don't know if it will work in Eclipse or Netbeans, but you could try adding a watch on *(bar + 1) for the second "entry". However you probably can't use bar as an array unless the debugger allows you to typecast it to an array (like (int[])bar, which I have no idea if it will even work in real C).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • wait... so what do people do when they need to check entries on run time? printing the values with `printf` seems a bad solution. – dvir Dec 02 '11 at 15:44
  • I don't know about others, but I use the first method: `*(bar + x)`. – Some programmer dude Dec 02 '11 at 15:46
  • i've just tried it and it works, so thank you! however, i'm confused. doesn't this method only allows you to check one entry at a time? For the `int foo[10]` code you'll see all the entries at once, but with the dynamic allocation code + your method it is only possible to check a specific entry. – dvir Dec 02 '11 at 15:55
  • 1
    @dvir It might work if the debugger allows you to somehow cast the pointer to an array. But otherwise, it's not possible. It works for arrays because either there is debug-information in the program that tells the debugger it's an array and how big it is, or the debugger parses the source for to know it's an array and it's possible limit. – Some programmer dude Dec 02 '11 at 15:59
  • 2
    trying to add a watch for `(int[100])foo` turned it into a 100 entries array in the "wathces" window, but it's values were not right... i guess i'd have to check them in another way. thank you anyway! – dvir Dec 02 '11 at 16:13
  • i managed to solve this problem. i updated the original message for future askers :) thanks for all of your help! – dvir Dec 02 '11 at 16:38