Printing a C array in the debugger Console

I’m by no means a GDB expert.  I know the basics, but I just learned this gem today.  Say you have a C array with five items created with something like this:


int *array = malloc(5 * sizeof(int));

And you want to print it out in the debugger quickly.  You can’t just print the pointer because GDB doesn’t know to print it as an array, but this will:


p *array@5

Output:


$1 = {0, 0, 0, 0, 0, 1}

To use this the left hand side, before the ‘@’ should be an array, and the right hand side needs to be the length of the array.  Very handy.

Advertisements

Posted on March 2, 2012, in Debugging and tagged , , . Bookmark the permalink. 1 Comment.

  1. that’s awesome. thanks for posting that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: