Help needed understanding memory locations in gdb

Viewing 2 reply threads
  • Author
    Posts
    • #8775
      bahr
      Participant

      Hi, I’m currently trying to understand how memory locations work, and I’m a bit confused in regards to how many bytes a memory location stores. I would really appreciate some clarity on this, and I’m sorry for the length of this post.

      I have written this small C program to look at how integers are stored in memory:


      #include

      int main(){
      int i = 5;
      return 0;
      }

      I know that integers require 4 bytes to be stored. So I tried debugging this in gdb on a ubuntu virtual machine, and I get the following:

      Ex: 1


      (gdb) x/xw &i
      0xbffff834: 0x00000005

      So the way I understand this is, that memory location 0xbffff834 contains the value 5. Since I have used the ‘w’ format I get the result as 4 byte word in hex.

      So then I tried to look at the individual bytes:

      Ex: 2


      (gdb) x/4xb
      0xbffff834: 0x05 0x00 0x00 0x00 0x00

      This does also make sense to me.

      Now this where I get confused. When I examin the memory location and print it as 4 words in hex I get this:

      Ex: 3


      (gdb) x/4xw 0xbffff834
      0xbffff834: 0x00000005 0xbffff898 0xb7eafebc 0x00000001

      However if I do the same and show 5 words i get this:

      Ex: 4


      (gdb) x/5xw 0xbffff834
      0xbffff834: 0x00000005 0xbffff898 0xb7eafebc 0x00000001
      0xbffff844: 0xbffff8c4

      So I’m a bit confused how to interpret this. Does the above result mean, that the memory location 0xbffff834 can contain 4 words, each being 4 bytes, thus one memory location can contain 16 bytes? The reason I’m confused is because when I examine each memory location indvidually I get this:

      Ex: 5


      (gdb) x/xb 0xbffff834
      0xbffff834: 0x05
      (gdb) x/xb 0xbffff835
      0xbffff835: 0x00
      (gdb) x/xb 0xbffff836
      0xbffff836: 0x00
      (gdb) x/xb 0xbffff837
      0xbffff837: 0x00
      (gdb) x/xb 0xbffff838
      0xbffff838: 0x98

      So I interpret this as, one memory location is actually storing one byte only since, 0xbffff834 contains 0x05, and 0xbffff835 contains 0x00 in it. Why does Ex. 4 then seem to list 4 words at the 0xbffff834 memory location before printing the content of the next memory location 0xbffff844? Is this some gdb specific thing to preserve space on the screen or something ???

    • #54066
      apollo
      Participant

      However if I do the same and show 5 words i get this:

      Ex: 4
      Code:

      (gdb) x/5xw 0xbffff834
      0xbffff834: 0x00000005 0xbffff898 0xb7eafebc 0x00000001
      0xbffff844: 0xbffff8c4

      Here is what is going on. 4 words = 16 bytes. What you are seeing here is the 5 words, printing out 16 bytes at a time for readability. The first grouping of 16 bytes starts out at 0xbffff834, when you add 16 to it, it becomes 0xbffff844. Treat each line as a 16 byte array and it explains what you’re looking at


      00 00 00 05 bf ff f8 98 b7 ea fe bc 00 00 00 01
      0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

      When you do 5xw at a memory address it will print 5 words (5*4 bytes) = 20 bytes. That’s why you have 16 on one line, then 4 on the next. It will continue to walk down the memory locations from there, 6xw would print 8 additional bytes on the next line.

      Does this make sense ?

    • #54067
      bahr
      Participant

      @apollo wrote:

      However if I do the same and show 5 words i get this:

      Ex: 4
      Code:

      (gdb) x/5xw 0xbffff834
      0xbffff834: 0x00000005 0xbffff898 0xb7eafebc 0x00000001
      0xbffff844: 0xbffff8c4

      Here is what is going on. 4 words = 16 bytes. What you are seeing here is the 5 words, printing out 16 bytes at a time for readability. The first grouping of 16 bytes starts out at 0xbffff834, when you add 16 to it, it becomes 0xbffff844. Treat each line as a 16 byte array and it explains what you’re looking at


      00 00 00 05 bf ff f8 98 b7 ea fe bc 00 00 00 01
      0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

      When you do 5xw at a memory address it will print 5 words (5*4 bytes) = 20 bytes. That’s why you have 16 on one line, then 4 on the next. It will continue to walk down the memory locations from there, 6xw would print 8 additional bytes on the next line.

      Does this make sense ?

      Thank you for the explanation!

      Yes it makes sense now. I was confused whether it was for readability or due to some concept I had missed to understand.

      It was nice getting that clarified 🙂

Viewing 2 reply threads
  • You must be logged in to reply to this topic.

Copyright ©2020 Caendra, Inc.

Contact Us

Thoughts, suggestions, issues? Send us an email, and we'll get back to you.

Sending

Sign in with Caendra

Forgot password?Sign up

Forgot your details?