3 ms·
auto x = new int[8]; auto y = new int[8]; However, given how low-level a language C++ is, we can actually break this assumption by setting i to y-x. S
by miket94 8y ago
auto x = new int[8];
auto y = new int[8];
However, given how low-level a language C++ is, we can
actually break this assumption by setting i to y-x.
Since
&x[i] is the same as x+i, this means we are actually
writing 23 to &y[0].
This assumes that the address space of y starts at the end of the address space of x? In other words x+8==y? Could these two allocated blocks of memory ever not be continuous?
- vardump 8y ago> However, given how low-level a language C++ is, we can actually break this assumption by setting i to y-x. Since &x[i] is the same as x+i, this means we are actually writing 23 to &y[0]. > This assumes that the address space of y starts at the end of the address space of x? No it does not. Any value of x or y should "work", because y-x computes a difference between pointers. > Could these two allocated blocks of memory ever not be continuous? Probability they're not continuous is high and only gets higher as the heap gets more fragmented over time.