Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • string of pointer ?

    Is there a way to get the string representation of a pointer in Mata?

    For example, if I have a pointer like
    Code:
    : p = &1
    
    : p
      0x38bff10
    is there a way to get the value 0x38bff10 as a string variable?

  • #2
    James,

    I'm curious to see what others say, but my sense is no. The most logical way to do something like that would be with the strofreal() function, but strofreal(p) doesn't work and the Mata manual specifically says that arithmetic with pointers is not allowed. (This may be, in part, because Mata pointers are stored in 8 bytes and Mata reals are only 4 bytes.)

    Maybe there is a Mata function or command that captures the output of another Mata operation, but I haven't come across such a thing.

    Regards,
    joe

    Comment


    • #3
      Originally posted by James Fiedler View Post
      Is there a way to get the string representation of a pointer in Mata?
      Suppose you could, how would you use it?

      This is not just curiosity, but a precaution (based on the limited information available on the subject). Remember that you are not in control of the memory. Suppose your program is (in pseudocode):
      Code:
      X=GetAddressOf(A)
      Z=Peek(X)
      The problem is that between the first line and the second line Stata's memory manager may decide to reshuffle the memory the way it sees fit, and the addresses of everything (including A) will change. It will update all the internal references (pointers to pointers so to speak), but it wouldn't know anything about your off-side reference. By the time your second line starts executing, the address X captured in the first line might be already obsolete, with unpredictable consequences.

      I think it is possible to capture the address one way or another, but it is the using of an address that is a problem (imho).

      Perhaps if you explain what you are trying to achieve - it will be easier to look for a suggestion.

      2Joe: Capacity of type should not be a problem. James is looking for a string representation of a pointer and strings can be of virtually any size, and definitely large enough to accommodate 8 or 48 bytes.

      Best, Sergiy Radyakin

      Comment


      • #4
        Sergiy,

        I agree with your caution about using pointer memory references and I wondered the same thing. The Mata manual justifies being able to see the memory location on these grounds:

        Listing the value of pointers often helps in debugging because, by comparing addresses,
        you can determine where pointers are pointing and whether some are pointing to the same
        thing.
        But Stata is probably (and justifiably) concerned about what someone would plan to do with such a variable if they could actually manipulate it.

        My comment about the number of bytes in a pointer memory reference had less to do with the size of strings and more to do with the fact that the Mata manual explicitly mentions that arithmetic with pointers is prohibited. Aside from the potential dangers involved, a practical constraint is that it would be difficult to store an 8-byte pointer in a 4-byte real. A string could technically hold the 8-byte string, but the strofreal() function would be expecting a 4-byte real.

        In retrospect, however, I suspect that your explanation is probably more relevant than mine. If Stata really wanted to allow people to play around with pointers, they could easily have provided a mechanism to work with 8-byte memory locations.

        Regards,
        Joe

        Comment

        Working...
        X