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.

3
  • 1
    Employed Russian's edit changes the meaning of the question slightly, but the @<size> in an expression works for both display and print commands. Commented Oct 30, 2009 at 20:16
  • Do you want to watch it (break on read / write) or print it whenever GDB stops? Commented Jul 27, 2015 at 16:25
  • 1
    To watch (break) on the entire array: stackoverflow.com/questions/11004374/… Commented Jul 27, 2015 at 16:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I prefer to use p instead of "display", as it is easier to type. I've added it as a separate answer here.
"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, 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.
Yeah, the editor of the question actually changed the meaning. I should probably change it back so I don't look like a buffoon.
@mkb, Don't you mean display *kk@size? Considering that size is the variable you want to use.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.