// file: my first program // #include #include #include // function: myalloc // void mymalloc(float** arg, long N) { *arg = new float[N]; fprintf(stdout, "mymalloc: %u\n", *arg); } // function: myprint // // arguments: // FILE* fp: open file pointer // float* x: a floating point array // long N: the total number of elements // // return: a logical value indicating status // bool myprint(FILE* fp_a, float* x_a, long N_a) { // print // for (long i = 0; i < N_a; i++) { fprintf(stdout, "myprint: (%d) = %f\n", i, x_a[i]); } // exit gracefully // return true; } // function: mychange // // arguments: // float* x: a floating point array // long N: the total number of elements // // return: a logical value indicating status // bool mychange(float* x_a, long N_a) { // loop over the matrix // for (long i = 0; i < N_a; i++) { *x_a++ = (float)27.0; } // exit gracefully // return true; } // function: mycopy // // arguments: // float* x: a floating point array // long N: the total number of elements // // return: a float* containing the new array // float* mycopy(float* x_a, long N_a) { // allocate memory // // float* rval = malloc(N_a * sizeof(float)); float* rval = new float[N_a]; // create a temp pointer // float* tmp_val = rval; // copy the data: // we could use memcpy() // // memcpy(rval, x_a, N_a * sizeof(float)); for (long i = 0; i < N_a; i++) { *tmp_val++ = *x_a++; } // exit gracefully // return rval; } // my main program starts here // int main(int argc, char** argv) { // void pointers // // float x[] = {0, 1, 2, 3, 4}; // fprintf(stdout, "x[3] = %f (%u %u %f) = %f\n", x[3], x, &x[3], x[3], x[3]); /* float* ptr = (float*)malloc(5 * sizeof(float)); for (long i = 0; i < 5; i++) { ptr[i] = x[i]; } fprintf(stdout, "ptr[3] = %f\n", ptr[3]); */ // create memory in a function // float* y = (float*)NULL; fprintf(stdout, "main BEFORE: y = %u\n", y); mymalloc(&y, 5); fprintf(stdout, "main AFTER: %u\n", y); // grab the dimensions from the command line // long Mrows = atoi(argv[1]); long Ncols = atoi(argv[2]); long Pdepth = atoi(argv[3]); // declare an array of floats // float x[Mrows][Ncols][Pdepth]; //float x[Mrows][Ncols]; // set the values // for (long m = 0; m < Mrows; m++) { for (long n = 0; n < Ncols; n++) { for (long p = 0; p < Pdepth; p++) { x[m][n][p] = (float)(m + n + p); fprintf(stdout, "[%d][%d][%d] = %f\n", m, n, p, x[m][n][p]); } } } // test how the data is stored in memory // /* long i = 0; float* ptr = (float*)x; for (long m = 0; m < Mrows; m++) { for (long n = 0; n < Ncols; n++) { for (long p = 0; p < Pdepth; p++) { fprintf(stdout, "%d: x[%d][%d][%d] (mem: %u) = %f\n", i, m, n, p, &x[m][n][p], x[m][n][p]); i++; ptr++; } } } fprintf(stdout, "size of x = %u (%u)\n", sizeof(&x), sizeof(ptr)); fprintf(stdout, "size of chr = %u (%u)\n", sizeof(char*), sizeof(int*)); fprintf(stdout, "the starting memory location is %u (%p)\n", x, x); fprintf(stdout, "x[][][] (%u) = %f (%f)\n", (x+1), *(x+1), *(x+1)); fprintf(stdout, "x[][][] (%u) = %f (%f)\n", (x+2), *(x+2), *(x+2)); fprintf(stdout, "x[][][] (%u) = %f (%f)\n", (x+3), *(x+3), *(x+3)); fprintf(stdout, "x[][][] (%u) = %f (%f)\n", (x+4), *(x+4), *(x+4)); */ /* long i = 0; for (long m = 0; m < Mrows; m++) { for (long n = 0; n < Ncols; n++) { x[m][n] = (float)(i); // fprintf(stdout, "%d: [%d][%d] = %f\n", i, m, n, x[m][n]); i++; } } // fprintf(stdout, "data: \n"); for (long i = 0; i < Mrows * Ncols * Pdepth; i++) { fprintf(stdout, "(%d: %u): %f\n", i, ptr, *ptr); ptr++; } // for (long i = 0; i < Mrows * Ncols; i++) { // fprintf(stdout, "(%d: %u): %f\n", i, ptr, *ptr); // ptr++; // } */ // print x // myprint(stdout, (float*)x, Mrows * Ncols * Pdepth); // change x // mychange((float*)x, Mrows * Ncols * Pdepth); fprintf(stdout, "New values:\n"); myprint(stdout, (float*)x, Mrows * Ncols * Pdepth); /* /* // create a new array // //float* y = (float*)x; float* y = new float[Mrows * Ncols]; long k = 0; for (long i = 0; i < Mrows; i++) { for (long j = 0; j < Ncols; j++) { y[k++] = x[i][j]; } } y[3] = 15.0; //float* y = mycopy((float*)x, Mrows * Ncols * Pdepth); fprintf(stdout, "X:\n"); myprint(stdout, (float*)x, Mrows * Ncols); v fprintf(stdout, "Y:\n"); myprint(stdout, (float*)y, Mrows * Ncols); // delete [] y; long M = 10; float* y = new float[M*M]; memset(y, 0, M*M*sizeof(float)); y[M*1 + 1] = 27.0; y[M*2 + 2] = 99.0; float*z = y; long N = 3; for (long i = 0; i < N; i++) { for (long j = 0; j < N; j++) { fprintf(stdout, "y[%d][%d] = (%u) %f <> %u %f\n", i, j, &y[N*i+j], y[N*i+j], z, *z); z++; } } short int buf[10]; FILE* fp = fopen(argv[1], "r"); fprintf(stdout, "position = %u\n", ftell(fp)); fread(buf, sizeof(short int), 1, fp); fprintf(stdout, "position = %u\n", ftell(fp)); fclose(fp); char* mystr = new char[10]; char* ptr = (char*)malloc(10 * sizeof(char)); char* aptr = new char[10]; aptr[3] = 'a'; *aptr = 'b'; aptr[0] = *aptr; printf("%c>", aptr[3]); void* pstr = (void*)aptr; float* joe = (float*)pstr; char Joe[] = "JoePicone"; char* str = Joe; fprintf(stdout, "str = %c\n", str[0]); str++; fprintf(stdout, "str = %c\n", *str); double Mary[10]; Mary[0] = 27.0; Mary[1] = 35.0; Mary[2] = -27.0; double* psum = (double*)Mary; fprintf(stdout, "psum = %f\n", *psum); psum++; fprintf(stdout, "psum = %f\n", *psum); */ // exit gracefully // return 0; }