Skip to content

Instantly share code, notes, and snippets.

@wimleers
Created May 23, 2011 18:29
Show Gist options
  • Save wimleers/987233 to your computer and use it in GitHub Desktop.
Save wimleers/987233 to your computer and use it in GitHub Desktop.
Note that the array index that is used goes beyond the array's capacity. And thus it modifies the next variable in memory…
#include <stdio.h>
#define uint unsigned int
#define TTW_NUM_GRANULARITIES 5
class Weird {
public:
Weird() {
this->lastUpdate = 0;
for (int g = 0; g < TTW_NUM_GRANULARITIES; g++)
this->capacityUsed[g] = 0;
}
void increment() { this->lastUpdate++; }
void test() {
uint granularity = TTW_NUM_GRANULARITIES;
printf("before: %d\n", this->lastUpdate);
// Update this granularity's used capacity..
this->capacityUsed[granularity] = 0;
printf("before: %d\n", this->lastUpdate);
}
protected:
uint capacityUsed[TTW_NUM_GRANULARITIES];
uint lastUpdate;
};
int main() {
Weird w;
w.increment();
w.test();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment