mirror of
https://github.com/nlohmann/json.git
synced 2026-02-19 09:56:26 +00:00
Fix return value of get_ptr for unsigned integers (#4525)
* 🐛 fix return value of get_ptr for unsigned integers * 📝 update documentation
This commit is contained in:
@@ -35,7 +35,35 @@ Constant.
|
||||
|
||||
!!! danger "Undefined behavior"
|
||||
|
||||
Writing data to the pointee of the result yields an undefined state.
|
||||
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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -40,7 +40,7 @@ Constant.
|
||||
|
||||
!!! danger "Undefined behavior"
|
||||
|
||||
Writing data to the referee of the result yields an undefined state.
|
||||
The reference becomes invalid if the underlying JSON object changes.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user