0

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 ;
// }
1
  • In your second example, var goes out of scope after the end of the function, and the returned address is no longer a valid memory address for you to use. Also note that somewhere, you should free the memory. Commented Sep 12, 2014 at 19:00

3 Answers 3

3

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!

Sign up to request clarification or add additional context in comments.

Comments

2

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.

e.g.

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.

3 Comments

but must I initialize any pointer before to fill in any other value ??
@user3466199 - Yes..In order to use a pointer in some operation, you must make sure that it contains valid address (since the pointers store address ), otherwise you are just performing operations on some random memory location that : 1.May or not be even in your process context. 2.Contains garbage. In this case if you are lucky you get errors, and if not you get expected answers.
@user3466199 - In the second code the pointer has a valid memory address, but as far as your implementation is concerned you should store the address of a "double" array inside the outvec pointer. Let me help you with some code...
1

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.

15 Comments

ok , if I add this to lines double ss ; double *outvec = &ss;
@user3466199 - if the address to which your pointer points to is valid then rest should fall in place unless you make some other mistake.
but what the difference to point to an doube and to point to an double array, why I need to declare an array not a simple double, because if I do the same thing on the main program that works perfectely
when I debug the code it fill the first and the second components normaly, but it crash at the third components !!!!!
@Blastfurnace - could you reply me about the two previous comments
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.