12

Consider the following example:

int size = 10, *kk = new int[size];

for (int i = 0; i < size; i++) {
    kk[i] = i;
}

delete [] kk;

How can I add a watch for the whole array? I can add a watch one by one (kk[0],kk[1]...), but since I know the array's length is there a way to do it automatically? I mean something like kk[0..size-1] or so.

I'm using NetBeans IDE together with cygwin g++ and gdb.

Matt K
  • 13,370
  • 2
  • 32
  • 51
Keiji
  • 245
  • 1
  • 4
  • 7

1 Answers1

12

Try display *kk@<size> From the doc for the print command:

@ is a binary operator for treating consecutive data objects anywhere in memory as an array. FOO@NUM gives an array whose first element is FOO, whose second element is stored in the space following where FOO is stored, etc. FOO must be an expression whose value resides in memory.

Matt K
  • 13,370
  • 2
  • 32
  • 51
  • I prefer to use p instead of "display", as it is easier to type. I've added it as a separate answer here. – Debajit Oct 27 '10 at 09:52
  • 1
    "print" and "display" are not the same command. "display" sets a watch on a variable so you see it every time you get a prompt, assuming the variable is in scope. – Matt K Oct 27 '10 at 12:15
  • @Matt, you're right. I didn't see that the question was about watching the variable. I have removed my misleading answer. Thanks for pointing it out. – Debajit Nov 02 '10 at 19:28
  • 1
    Yeah, the editor of the question actually changed the meaning. I should probably change it back so I don't look like a buffoon. – Matt K Nov 02 '10 at 21:18
  • @mkb, Don't you mean `display *kk@size`? Considering that `size` is the variable you want to use. – Erik B Jun 30 '11 at 13:48
  • I actually meant to use the literal size of the array. I didn't realize that you could use a variable there. The doc I quoted implies that a literal is necessary. I didn't read the section in the GDB manual on Expressions before responding. – Matt K Jun 30 '11 at 16:53