/* This is dyn1.c with a subtle bug: the bytes variable is unsigned int instead of float. Do you see why this is a problem? Try typing 65536 65536 for dyn1, then for dyn2. Can you explain why dyn2 doesn't stop? */ #include #include void die_read(void) { printf("scanf failed\n"); exit(1); } void die_nomem(void) { printf("out of memory\n"); exit(1); } int main(void) { unsigned int m; unsigned int n; unsigned int i; unsigned int j; unsigned int bytes; double *x; if (scanf("%u",&m) < 1) die_read(); if (scanf("%u",&n) < 1) die_read(); bytes = m; bytes *= n; bytes *= sizeof(double); if (bytes >= 1000000) die_nomem(); x = (double *) malloc(bytes); if (!x) die_nomem(); for (i = 0;i < m;++i) for (j = 0;j < n;++j) if (scanf("%lf",x + i * n + j) < 1) die_read(); printf("%u %u\n",n,m); for (j = 0;j < n;++j) for (i = 0;i < m;++i) printf("%f%c",x[i * n + j]," \n"[i == m - 1]); return 0; }