Skip to content

push_back()/emplace_back() on array invalidates pointers to existing array items #1586

@samwcoulter

Description

@samwcoulter

I am serializing some internal structures to Json and would like to populate an array with null json values, storing a pointer to each null value and then later filling that value with the appropriate data. Eg:

    json arr = json::array();
    arr.emplace_back(nullptr);
    auto* ptr0 = &arr.at(0);
    arr.emplace_back(nullptr);
    auto* ptr1 = &arr.at(1);
    arr.emplace_back(nullptr);
    auto* ptr2 = &arr.at(2);
    //Some time later:
    *ptr0 = 0;
    *ptr1 = 1;
    *ptr2 = 2;

However, this apparently yields an array where the first two elements are null, and only the last element in the array contains the actual data (an integer == 2).

I am not sure if I am simply using the API incorrectly, or there is an actual issue that needs to be addressed. I would very much appreciate any help, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions