1

I wish to use gdb to print all array elements of allocated memory.

int main() {
   int* p=(int*)malloc(7*sizeof(int)); 
   for(size_t j=0;j<7;++j) {
         p[j]=j; 
   } 
   return 0; 
} 

So I compiled this program with -g3, use gdb to start it, and set break

> print p@7 

I expected with will print out 1-7, but the actual output was

$1 = {0x6160b0, 0x5, 0xff00000000, 0x615c60, 0x615c80, 0x615c20, 0x615c40} 

Why my expectation!=actual result, anything wrong with my program?

1 Answer 1

3

The GDB Manual says:

The left operand of `@' should be the first element of the desired array and be an individual object. The right operand should be the desired length of the array. The result is an array value whose elements are all of the type of the left argument

So if you say

 print p@7 

you tell GDB to print an array of 7 pointers.

What you need is:

print *p@7 
Sign up to request clarification or add additional context in comments.

Comments

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.