2

I try to print the contents of an array like this, and it's successful:

p/x t->arr
$1 = {0x63, 0x61, 0x74, 0x31, 0x2e, 0x6a, 0x70, 0x67, 0x0 <repeats 248 times>}

However, when I try a different way like this:

(gdb) p &t->arr
$2 = (char (*)[256]) 0x60c4d0
 p/100x *0x60c4d0
Item count other than 1 is meaningless in "print" command.

t->arr is defined as arr[256] in the struct. Do I do something wrong?

3
  • I'm not sure I understand what your goal is? Or, what are you hoping p/100x will do? Commented Aug 12, 2013 at 23:38
  • @macattack I'm trying to produce the same output as p/x t->arr. However, in the latter case, I try to print the address of array instead of using "t->arr" Commented Aug 12, 2013 at 23:40
  • Maybe if you did p/100x (char *)*0x60c4d0? So that gdb knows it is s char * at the address. I've never used the p/#x syntax. Commented Aug 12, 2013 at 23:45

3 Answers 3

3

In gdb, you can cast a literal pointer value to whatever type you think is appropriate. So, if you want to treat the address as a pointer to an array, you can simply cast it as such, and print that.

(gdb) p argv
$1 = (char **) 0x7fffffffe898
(gdb) p *(char *(*)[2])0x7fffffffe898
$2 = {0x7fffffffeae1 "/tmp/a.out", 0x0}
Sign up to request clarification or add additional context in comments.

Comments

2
p/100x *0x60c4d0

Here you are asking gdb to print the 100 elements in the memory location 0x60c4d0, which is meaning less because the memory location 0x60c4d0 can contain only one element. Hence the error states that anything other than 1 is meaningless.

p/256x 0x60c4d0

This gives 256 addresses starting at 0x60c4d0, which will be the addresses of each of the 256 char elements in the array.

2 Comments

since " p &t->arr" print (char (*)[256]) 0x60c4d0, I think the array is located in that address right? thus, I should be able to print 100 element of that array
array is not located "in" that address, but it starts at that address.
0

There are two aspects to consider:

  1. Given a pointer GDB prints the address and not what it points to. So, if you have a variable pc of type char *, p will print only the address.

  2. Dereferencing the pointer variable gets the value it points, as GDB can determine its type. So, if you print *pc, you'll get the value since the type is known. On the other hand, if you had pa of type char [25], p will know the type and treat it as an array and print its elements.

However, if you want GDB to treat an address as an array, you'll have to teach it. So when you use the array start as an address, GDB would now need to know the type: - If you say it's a char *, GDB will print the address. - If you say it's a char * and dereference it, GDB will print the char value at that address. - if you say it's a char (*) [25], GDB will print those elements.

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.