// This is a first example of how you should NOT create arrays. // In particular, look at function make_array, // and how it creates array x. // Compare to good_example1.cpp. // // Note: this code produces a warning when compiled with Visual Studio, // but runs without crashing. When I run it on Linux, it crashes. // Either way, this code does not comply with C standards. #include #include int * make_array(int length) { // bad way int x[2]; // good way // int * x = (int *) malloc(sizeof(int) * length); x[0] = 10; x[1] = 20; return x; } int main() { int * x = make_array(2); printf("x[0] = %d\n", x[0]); free(x); return 0; }