Mi duda es que tengo este codigo el cual imprime memoria, pero no lo gro descifrar que signicado tienen los 8 bytes anteriores a los 4 bytes de mi objeto, y tampoco entiendo porque al hacer delete del p2 ni se inmuta su direccion de memoria, alguien que me puede ayudar?
#include <iostream>
#include <cstdint>
#include <iomanip>
void printRawMem(uint8_t* p, uint16_t linebytes, uint16_t lines) {
for(uint16_t l=0; l < lines; ++l) {
std::cout << reinterpret_cast<uint16_t*>(p) << " ";
for(uint16_t u=0; u < linebytes; ++u) {
[](uint16_t val) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << val;
}(*p);
++p;
std::cout << " ";
}
std::cout << "\n";
}
}
struct pair {
uint16_t x, y;
pair(uint16_t x, uint16_t y) : x(x), y(y){
std::cout << "Creating pair(" << x << " " << y << ") at (" << this << ", " << sizeof*this << " )\n";
}
~pair() {
std::cout << "Destroying pair\n";
}
};
int main(int argc, char const *argv[])
{
pair *p = new pair(3,4);
pair *p2 = new pair(2,7);
uint8_t* ptr;
ptr = reinterpret_cast<uint8_t*>(p) - 16;
printRawMem(ptr, 16, 4);
std::cout << "--------------------------------------\n";
delete p;
delete p2;
printRawMem(ptr, 16, 4);
return 0;
}
Y tengo esta salida:
Creating pair(3 4) at (0x2582490, 4 )
Creating pair(2 7) at (0x25824b0, 4 )
0x2582480 00 00 00 00 00 00 00 00 15 70 f9 42 a2 b6 02 1c
0x2582490 03 00 04 00 00 00 00 00 50 01 dc 00 00 00 00 00
0x25824a0 00 00 00 00 00 00 00 00 15 70 f9 42 e1 a4 02 1c
0x25824b0 02 00 07 00 00 00 00 00 50 01 dc 00 00 00 00 00
--------------------------------------
Destroying pair
Destroying pair
0x2582480 00 00 00 00 00 00 00 00 a3 71 f8 f4 a2 b6 02 00
0x2582490 a0 63 dc 00 00 00 00 00 50 01 dc 00 00 00 00 00
0x25824a0 00 00 00 00 00 00 00 00 02 00 01 03 e1 a4 02 1c
0x25824b0 02 00 07 00 00 00 00 00 50 01 dc 00 00 00 00 00