Here is another snippet with mysterious output ;-)
#include <stdio.h>
#include <malloc.h>
int main(void) {
printf("%s\n\n", "References are cute!");
int* MyArray;
MyArray = (int*) malloc (100*sizeof(int));
int& ElementFive = MyArray[5];
MyArray[5]=1005;
printf("MyArray[5]=%i, ElementFive=%i\n", MyArray[5], ElementFive);
MyArray[5]=2*MyArray[5];
printf("MyArray[5]=%i, ElementFive=%i\n", MyArray[5], ElementFive);
MyArray = (int*) realloc (MyArray,100*sizeof(int));
MyArray[5]=2*MyArray[5];
printf("MyArray[5]=%i, ElementFive=%i\n", MyArray[5], ElementFive);
MyArray = (int*) realloc (MyArray,1000*sizeof(int));
MyArray[5]=2*MyArray[5];
ElementFive=2*ElementFive;
printf("MyArray[5]=%i, ElementFive=%i <<<<<<< great, isn't it?\n", MyArray[5], ElementFive);
printf("%s", "\nHit Enter to end this disaster");
getchar();
return (0);
}
Output:
References are cute!
MyArray[5]=1005, ElementFive=1005
MyArray[5]=2010, ElementFive=2010
MyArray[5]=4020, ElementFive=4020
MyArray[5]=8040, ElementFive=-35783204 <<<<<<< great, isn't it?
Of course, seasoned assembler programmers might have an idea what happens under the hood ;)