// file: $(ECE_1111)/labs/01/l01_03/myprog.cc // // local include files // #include "myprog.h" // declare a function // void foo(float* x) { *x = 99.0; x = new float[10]; fprintf(stdout, "x is stored at %u\n", x); } // main: myprog // // This is a driver program that does some very simple things. // int main(int argc, const char** argv) { // declare a string array // char* str = (char*)"joe.txtxyxyxyxy"; fprintf(stdout, "how big is str (%u)\n", sizeof(char*)); fprintf(stdout, "the memory location is (%u)\n", str); // declare a numeric array // long N = 3; float x[N] = {1, 2, 3}; float y[6][9][12]; float* z = (float*)y; fprintf(stdout, "y begins at (%u)\n", (unsigned long)y); fprintf(stdout, "z begins at (%u)\n", (unsigned long)z); // dynamic memory allocation // float* alex = new float[99]; fprintf(stdout, "alex is located at %u\n", alex); char* joe = (char*)new float[99]; fprintf(stdout, "joe is located at %u\n", joe); memcpy(joe, alex, 4); /* fprintf(stdout, "alex[1] is located at %u\n", &alex[1]); fprintf(stdout, "joe[4] is located at %u\n", &joe[4]); delete [] alex; float joe = (unsigned long)(void*)alex; float* jordan = (float*)(unsigned long)joe; if (jordan != alex) { fprintf(stdout, "%u != %u\n", alex, jordan); } */ // demonstrate a function // float sum = 27.0; foo(&sum); fprintf(stdout, "sum is stored at %u\n", &sum); // fprintf(stdout, "sum = %f\n", sum); /* // declare a variable for status // FILE* fp = fopen(argv[1], "w"); fprintf(fp, "x[0] = %10.4f\n", x[0]); fprintf(stdout, "number of elements written = %d\n", fwrite(x, sizeof(float), N, fp)); long i = N; while (i > 0) { i--; fprintf(stdout, "x[%d] = %f\n", i, x[i]); } fclose(fp); // copy x into y so that y is a *duplicate* of x // fprintf(stdout, "---> memory copy\n"); float* y = new float[N]; memcpy(y, x, sizeof(float)*N); x[2] = 27.0; fprintf(stdout, "x[%d] = %f\n", 2, x[2]); fprintf(stdout, "y[%d] = %f\n", 2, y[2]); // produce a copy of argv[]: // char* b[argc]; char** c = new char*[argc]; for (long i = 0; i< argc; i++) { // b[i] = (char*)argv[i]; b[i] = new char[strlen(argv[i]) + 1]; strcpy(b[i], argv[i]); } fprintf(stdout, "note that b[0] != argv[0] (%u %u)\n", b[0], argv[0]); */ // exit gracefully // return(0); }