I am having a tough time understanding the following piece of code:
int stride = 512;
int max_len = 1024 * stride;
char *addr = (char *)malloc(max_len);
for (int i=stride; i<max_len; i += stride)
*(char **)&addr[i-stride] = (char*)&addr[i];
*(char **)&addr[i-stride] = (char*)&addr[0];
Looking at the code it seems this is trying to create some kind of circular link list. But I have no clue what those casts are actually doing.
IMHO the code you present is poor taste.
*(char **)&addr[i-stride] = (char*)&addr[0];is taking the pointeraddrto some dynamically allocated zone (right part(char*)&addr[0]could have been writtenaddrwhich is simpler), then copying that address to the memory located at positionaddr[i-stride].This actually could be wrong (undefined behavior), e.g. when
&addr[i-stride](which could be writtenaddr+i-stride) is not word aligned on some processors. In your caseihappens to bemax_lenandstridehappens to be 512, so that address is word aligned (sincemallocgives a well aligned zone).The original author of the code should perhaps have declared a
struct(or maybe aunion) like e.g.