4 ms·
Don't want to write a real post but my most memorable simple story was with a C optimizing compiler. I was using a something like struct Item { char name[12];
by tfigment 11y ago
Don't want to write a real post but my most memorable simple story was with a C optimizing compiler. I was using a something like struct Item { char name[12]; int flag; } with code that would would use this in a btree and use memcmp(x,y,sizeof(Item)) for ordering. The name could be non-unique but name+flag would be and give specific ordering. To fill Item I used something like (much simplified):
x = (Item*)malloc(sizeof(Item));
memset(x->name, 0, 12);
strcpy(x->name, name);
x->flag = flag;
So the bits after name are now significant in compares but expected to be zero because of memset. Well the optimizer would effectively remove the memset in release builds due to the strcpy so the code worked most of the time in release but would occasionally break because the bytes after the null terminated name could be anything and would only come into play when name was the same for 2 items.