Skip to content

Commit

Permalink
📝 update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Nov 30, 2024
1 parent c98f988 commit c69d547
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/mkdocs/docs/api/basic_json/get_ptr.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ Constant.

The pointer becomes invalid if the underlying JSON object changes.

Consider the following example code where the pointer `ptr` changes after the array is resized. As a result, reading or writing to `ptr` after the array change would be undefined behavior. The address of the first array element changes, because the underlying `std::vector` is resized after adding a fifth element.

```cpp
#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
json j = {1, 2, 3, 4};
auto* ptr = j[0].get_ptr<std::int64_t*>();
std::cout << "value at " << ptr << " is " << *ptr << std::endl;

j.push_back(5);

ptr = j[0].get_ptr<std::int64_t*>();
std::cout << "value at " << ptr << " is " << *ptr << std::endl;
}
```

Output:

```
value at 0x6000012fc1c8 is 1
value at 0x6000029fc088 is 1
```

## Examples

??? example
Expand All @@ -54,6 +82,10 @@ Constant.
--8<-- "examples/get_ptr.output"
```

## See also

- [get_ref()](get_ref.md) get a reference value

## Version history

- Added in version 1.0.0.
Expand Down
4 changes: 4 additions & 0 deletions docs/mkdocs/docs/api/basic_json/get_ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ Constant.
--8<-- "examples/get_ref.output"
```

## See also

- [get_ptr()](get_ptr.md) get a pointer value

## Version history

- Added in version 1.1.0.
Expand Down

0 comments on commit c69d547

Please sign in to comment.