// String overflow example // Written by Lee Devlin 1-24-2011 #include using namespace std; void print_bytes(const void *object, size_t size); int main() { char helloWorld[15] = "hello world"; char inputString[] = "test"; // this will set up a char array with a length of 5 bytes cout << "Input a char string of 4 characters or less : " ; //try putting in more characters until you see a collision with helloWorld cin >> inputString; cout << "helloWorld is a string that is set to \"" << helloWorld << "\" at address " << &helloWorld << endl; cout << "inputString is a string that is set to \"" << inputString << "\" at address " << &inputString << endl; cout << "\nHere are the hex dumps of the contents these variables:" << endl; cout << "helloWorld is "; print_bytes(&helloWorld, sizeof helloWorld); cout << "inputString is "; print_bytes(&inputString, sizeof inputString); system ("pause"); } void print_bytes(const void *object, size_t size) { size_t i; printf("[ "); for(i = 0; i < size; i++) { printf("%02x ", ((const unsigned char *) object)[i] & 0xff); } printf("]\n"); }