4 ms·
Haha. I was amused/bewildered by: void *p = realloc(...) // ... cast every other occurrence of p to (int *) ... The concept may be interesting, but th
by tom_mellior 6y ago
Haha. I was amused/bewildered by:
void *p = realloc(...)
// ... cast every other occurrence of p to (int *) ...
The concept may be interesting, but this particular implementation is needlessly horrible.
- Aanok 6y agoThat's actually correct C pointer arithmetic. When you increment a pointer, it moves ahead by the sizeof the underlying concrete type. If the pointer is to void, there is no underlying type and in fact void pointer arithmetic is disallowed by the standard. GCC lets you do it with an extention: https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Pointer-Arith.html https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Pointer-Arith.h... So in this case, since the headers for the arrays are made up of two int's it's the right thing to do to move around by sizeof(int) memory address units. You do this with a cast before the arithmetic.
- tom_mellior 6y agoMy point was, why not declare the pointer as int * directly since that the only type it's used as?