简体   繁体   中英

Return the pointer to array by C function

Imagine I have the following C function :

double * cross_product( double vec1[3], double vec2[3] )
   {
    double *outvec ;

     *(outvec + 0)= vec1[1]*vec2[2] - vec1[2]*vec2[1];
     *(outvec + 1)= vec1[2]*vec2[0] - vec1[0]*vec2[2];
     *(outvec + 2)= vec1[0]*vec2[1] - vec1[1]*vec2[0];
     return outvec ;
 }

why the program return an error in the execution , not in the compilation phase ??

this one also does not work

double * cross_product_2( double vec1[3], double vec2[3] )
   {
    double var ;
    double *outvec = &var;

     *(outvec + 0)= vec1[1]*vec2[2] - vec1[2]*vec2[1];
     *(outvec + 1)= vec1[2]*vec2[0] - vec1[0]*vec2[2];
     *(outvec + 2)= vec1[0]*vec2[1] - vec1[1]*vec2[0];
     return outvec ;
// }

You just created the pointer "outvec", but you dont know where it is pointing. I mean, you didnt alloc the memory where the pointer outvec will write. It is just trying to write data on a random memory space.
You need to "tell" the pointer where it should start writing the data, and reserve that space to you work in.
As Joel said, try doing that by using the code:

double *outvec = malloc(3 * sizeof(double)); 

Sorry about my bad english though... Good Luck!

You have not initialized your outvec pointer, so you are trying to write data to a null (or garbage) address. Try allocating some memory for it to point at first or declare a static array to return from your function.

eg

double *outvec = malloc(3 * sizeof(double));

You get a runtime error because the compiler doesn't evaluate outvec at compile time. So it is not aware of what address you will be trying to access at runtime.

double *outvec contains garbage, since you have not initialised or assigned any value
to it. Any operation on 'outvec' is an operation on garbage, thus the result also contains garbage.

Correction in second code :

     REVISED CODE 
double * cross_product_2( double vec1[3], double vec2[3] )
       {
        double *outvec = NULL; // new change
        outvec = (double *)malloc(sizeof(double)*3); //since you need space for 3 
                                                    //doubles         


     /* 
           *(outvec + i) means, that the calculated value is to be stored at the ith 
             index of the address pointed to by the outvec pointer. That is how arrays
              are indexed using pointers 
     */
         *(outvec + 0)= vec1[1]*vec2[2] - vec1[2]*vec2[1];
         *(outvec + 1)= vec1[2]*vec2[0] - vec1[0]*vec2[2];
         *(outvec + 2)= vec1[0]*vec2[1] - vec1[1]*vec2[0];
         return outvec ;
    // }

Explanation :

You have declared the following variable -
double *outvec;
"outvec" is a pointer that is used to store the address where result will be stored. Being a pointer the variable "outvec" will point to an address where the result will be stored. But since you have not specified what that address that would be, thus "outvec" points
to some random address which is garbage. Thats why we need to specify at what address we want to store the result, and for that
we need to initialise or assign the "outvec" variable with some valid address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM