Skip to content

Question about get_ptr() #127

Closed
Closed

Description

@dariomt

I see the following in the documentation of get_ptr()

"@warning Writing data to the pointee of the result yields an undefined state."

IIUC this means that the following code yields an undefined state:

json value = 17;
auto p = value.get_ptr<json::number_integer_t*>();
*p = 42;

i.e. I cannot modify the integer value inside the json

Why is this so?

Activity

  1. dariomt commented on Oct 2, 2015

    @dariomt
    ContributorAuthor

    In fact, if "writing data to the pointee of the result yields an undefined state", why not simply return a pointer to const in all cases, so the compiler will make sure I cannot modify the pointee.

  2. nlohmann commented on Oct 3, 2015

    @nlohmann
    Owner

    Hi @dariomt,

    your code is not wrong in the sense that it would not compile and do what you expect, the warning only states that you should not write to the pointer. A longer discussion about this can be found in #91.

    Regards
    Niels

  3. dariomt commented on Oct 5, 2015

    @dariomt
    ContributorAuthor

    Sorry, I've read #91 but I still don't get it.
    According to the documentation, my code would compile but "yields an undefined state".
    What does "undefined state" mean in this context?
    Is value still valid after the assignment to p or not?
    i.e. does value hold a json::number_integer_t equal to 42?

  4. nlohmann commented on Oct 6, 2015

    @nlohmann
    Owner

    Sorry about the confusion. It is OK to write to the pointer if you are sure that the underlying JSON object did not change. Once that object changed, the pointer is not valid any more.

  5. dariomt commented on Oct 7, 2015

    @dariomt
    ContributorAuthor

    OK, that's much more clear now. Thanks for the clarification.
    I suggest improving the documentation to make this point clear.
    A good example could help:

    json value = 17;
    auto p = value.get_ptr<json::number_integer_t*>();
    
    // writing through p is fine
    // 'value' can be changed though the pointer
    *p = 42;
    assert( value == 42 );
    
    // de-referencing p after an assignment to 'value' is still valid 
    // as long as the assignment didn't change the type of the json object
    value = 1234; 
    assert( *p == 1234 ); 
    
    // warning! *p is not valid after this assignment
    // i.e. de-referencing p will yield undefined behavior
    value = "doh!"
    *p = 42;  // <<< K-BOOM!
    
  6. nlohmann commented on Dec 16, 2015

    @nlohmann
    Owner

    I'll add a comment to the function in a later commit. Thanks for the discussion!

  7. added a commit that references this issue on Dec 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions