Why std::copy changes my pointer position?

In the following context.
`data` is a `uint8_t*` representing sequence of bytes.
`Col` in a std::vector containing some `uint64_t`.
Task: just to copy the `uint64_t` into the `data` under assumption, that before copy is the `data` empty but allocated.

1
2
3
4
5
6
            uint64_t* ptr = reinterpret_cast<uint64_t*>(data);
            printf("before: pointer to write: %p\n", data);
            printf("before: ptr             : %p\n", ptr);
            std::copy(col.begin(), col.end(), ptr);
            printf("after : pointer written : %p\n", data);
            printf("after : ptr             : %p\n", ptr);


Output:

before: pointer to write: 0x55fdd1493084
before: ptr : 0x55fdd1493084
after : pointer written : 0x100000000
after : ptr : 0x55fdd1493084


A bit Observation: As we know if we run this program many times, 0x55fdd1493084 will keep changing. BUT 0x100000000 this one always reminds here every time!
So I could not find the bug, do you have ideas? If you need more information, just tell me.

---------------------------------------------------------------------------------
Edit1 as Example

if using `std::vector<uint64_t> row {3, 2, 1, 4, 5, 6, 7, 8, 9, 257};`
We can have following things in `ptr` but nothing defined in `data`.


ptr has value 3, ptr has in pointer 0x55fdd1493084
ptr has value 2, ptr has in pointer 0x55fdd149308c
ptr has value 1, ptr has in pointer 0x55fdd1493094
ptr has value 4, ptr has in pointer 0x55fdd149309c
ptr has value 5, ptr has in pointer 0x55fdd14930a4
ptr has value 6, ptr has in pointer 0x55fdd14930ac
ptr has value 7, ptr has in pointer 0x55fdd14930b4
ptr has value 8, ptr has in pointer 0x55fdd14930bc
ptr has value 9, ptr has in pointer 0x55fdd14930c4
ptr has value 257, ptr has in pointer 0x55fdd14930cc


Edit 2:
Peter87 has right. The allocated space is not enough and some part is overwritten.
Thanks all.
Last edited on
Ignoring any strict aliasing issues... Is data pointing to an array with at least sizeof(uint64_t) * col.size() elements?
No. But ptr starts with the uint64_t numbers.
But I hope `data` would not change and has the same address as `ptr`.
See edit.

Also we could discuss about the strict aliasing, to achieve this copying into bytes.
Last edited on
The reason I ask about the size of the data array is because if it's not large enough std::copy might accidentally overwrite other variables (such as the data pointer).
You should post the complete code.
Topic archived. No new replies allowed.