Jump to content

Jorge

Member
  • Posts

    2
  • Joined

  • Last visited

Everything posted by Jorge

  1. Wasn't aware that new staff members need approval from a random player. Quite respectful reply too
  2. what is up with double pointers

    1. ThatOneTqnk

      ThatOneTqnk

      Yeah, so for double pointers what I like doing now is referring to an array of strings. It is a good example for double pointers.

      Take this scenario:

      spacer.png

      In that example I have an array of char pointers, since that is what an array of strings is. I simply place the pointers of the char arrays defined in lines 5 and 6, in lines 7 and 8. Notice that I have marked line 9 as a breakpoint so we can take a look at memory whilst in the debugger.

      So conceptually, here's how it should be:

      spacer.png

      Each string is a char array. And when a char array is created, the array is assigned the memory address of the first element. But to create a pointer to that memory address, we have to store that pointer somewhere too. So when those two independent strings were created, it created two char arrays, but also two pointers that contain memory addresses of the first character in each of the words. So, there is a pointer for 'h' in hello and a pointer for 't' in there. But then, we want to create an array of those pointers. So we need to store the memory addresses of those pointers, so we need to create pointers for those pointers. Those are allocated orderly, but since this is an array, we need the memory address of that first pointer.

      So effectively, we have a byte in memory that contains a memory address. At that memory address, are two memory addresses. Those two memory addresses contain memory addresses of the memory address of the first characters.

       

      lemme also try to show it in text

       pointy boi -->  0x012ff85c

                                (pointy boi,        pointy boi)

                                       |                        |

                                       |                        |

                                      V                       V

                              0x012ff850          0x012ff844

                              pointy boi             pointy boi    (NOT NECESSARILY TOGETHER)

                                     |                            |

                                     |                            |

                                    V                           V

                            0x002deb30         0x012ff844

                                'hello'                     'there'         (NOT NECESSARILY TOGETHER)                 

       

      To show this is what happens,  first let's took a look at the values of our locals

      spacer.png

       

      We see that the memory address of `yike` is 0x012ff85c 

      The memory addresses for the char pointers as shown in the image are:

      0x012ff850 (this memory address has the memory address that has the memory address of the first character in 'hello')

      0x012ff844 (this memory address has the memory address that has the memory address of the first character in 'there')

      IT also shows us the memory address for the 'h' in hello and 't' in there

      'h' in hello - 0x002deb30

      't' in there - 0x002deb44

       

      Let's start from the beginning, the double pointer.

      The memory address of that is 0x012ff85c:

      And at that memory address, we see the expected two memory addressesspacer.png

      Those are

      0x012ff850

      0x012ff844

       

      Those two point to the pointers of the first character of their respective words

      So let's yeet 0x012ff850 into the memory window.

      spacer.png

      And so we get 0x002deb30 for the memory address of 'h'

      So if we go to 0x002deb30 we get:

      spacer.png

      And so we have 68 65 6c 6c 6f

      If we convert 68 hex to dec we get:

      spacer.png

      And 104 dec in ASCII is our 'h'!

      spacer.png

       

      We can do the same for 0x012ff844 (pointer to pointer for 't')

      Then we get 0x002deb44 for the memory address of 't'

      spacer.png

      spacer.png

       

      And that's a good example of a practical usage of double pointers

    2. Jorge
×
×
  • Create New...