|
|
|
|
|
by nneonneo
1185 days ago
|
|
Hey, it actually works! int main() {
int tobias[24] = {0,0,33};
delete(tobias+4);
}
Compile and run on Ubuntu 20.04 (may work on other Linuxes), no errors, no warnings, runs perfectly fine.Bonus fun: try printing out the address of `tobias` and `new int[6]` afterwards :) |
|
Default new and delete just use malloc/free.
First 4 ints are interpreted as prev_size and size. prev_size is 0. 33 is 0b10001, size is 32 (bytes, so 8 ints), AMP is 0b001, so not in arena (default sbrk heap, I assume), not mmap'd, prev is used.
I didn't follow how the internal bookkeeping will be updated, but I assume 8 size chunk will be immediately reused on a following `new int[6]`.
Obviously don't write code like this.
[1] https://sourceware.org/glibc/wiki/MallocInternals