Skip to content

Commit c69d547

Browse files
committed
📝 update documentation
1 parent c98f988 commit c69d547

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

docs/mkdocs/docs/api/basic_json/get_ptr.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@ Constant.
3737

3838
The pointer becomes invalid if the underlying JSON object changes.
3939

40+
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.
41+
42+
```cpp
43+
#include <iostream>
44+
#include <nlohmann/json.hpp>
45+
46+
using json = nlohmann::json;
47+
48+
int main()
49+
{
50+
json j = {1, 2, 3, 4};
51+
auto* ptr = j[0].get_ptr<std::int64_t*>();
52+
std::cout << "value at " << ptr << " is " << *ptr << std::endl;
53+
54+
j.push_back(5);
55+
56+
ptr = j[0].get_ptr<std::int64_t*>();
57+
std::cout << "value at " << ptr << " is " << *ptr << std::endl;
58+
}
59+
```
60+
61+
Output:
62+
63+
```
64+
value at 0x6000012fc1c8 is 1
65+
value at 0x6000029fc088 is 1
66+
```
67+
4068
## Examples
4169

4270
??? example
@@ -54,6 +82,10 @@ Constant.
5482
--8<-- "examples/get_ptr.output"
5583
```
5684

85+
## See also
86+
87+
- [get_ref()](get_ref.md) get a reference value
88+
5789
## Version history
5890

5991
- Added in version 1.0.0.

docs/mkdocs/docs/api/basic_json/get_ref.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ Constant.
5858
--8<-- "examples/get_ref.output"
5959
```
6060

61+
## See also
62+
63+
- [get_ptr()](get_ptr.md) get a pointer value
64+
6165
## Version history
6266

6367
- Added in version 1.1.0.

0 commit comments

Comments
 (0)