This is my first question, so I hope I do a good job of getting my meaning across.
I've recently returned to C after many years as it is required by my PhD project (aerospace). I'm not really a software developer but I can usually get by. In the last few years I've been using MATLAB so I'm finding the transition back to the wonderful world of C and the stricter rules a little tricky.
I've been given some code that I have to adapt by adding additional functions, and I'm confused by the existing code. Re-writing the whole code-base is unfortunately not an option!
There exist some functions which in their definitions/declarations are shown to accept pointers as arguments. However these functions are then called in main with the variables themselves. There is no deferencing in the function body. But the code compiles and runs successfully, and gives expected outputs when compared with a MATLAB script performing the same functions. How can this be?
Original code:
// "Toy Model" of original code - this works and produces expected outputs
// myFileName.c
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "myFileName.h" //Contains function declarations
#define FACTOR (4.5) //for example
//Original function definition
void originalFunction(const double * const P1, const double * const P2, double * P_res)
{
double P1nu[3];
double aMatrix[3][3];
/*Example of function body - this function calls another, using the input argument P1 which is not dereferenced in this function either*/
matMultVector(aMatrix, P1, P1nu);
P_res = P1nu * FACTOR;
}
int main()
{
double P1[3];
double P2[3];
double P_res[3];
P1[0] = 2;
P1[1] = 1;
P1[2] = 4;
P2[0] = 100;
P2[1] = 240;
P2[2] = 310;
originalFunction(P1, P2, P_res);
printf("Output:\n");
printf("P_resX: %lf\n", P_res[0]);
printf("P_resY: %lf\n", P_res[1]);
printf("P_resZ: %lf\n", P_res[2]);
)
return (0);
}
I then added my new function following the same structure:
New code:
// "Toy Model" of new code - produces cygwin error at compile time
// myFileName.c
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "myFileName.h" //Contains function declarations
#define FACTOR (4.5) //for example
//Original function definition
void originalFunction(const double * const P1, const double * const P2, double * P_res)
{
double P1nu[3];
double aMatrix[3][3];
/*Example of function body - this function calls another, using the input argument P1, which is not dereferenced in this function either*/
matMultVector(aMatrix, P1, P1nu);
P_res = P1nu * FACTOR;
}
// My new function definition
void myFunction(const double * const P1, const double * const P2, double * myP_res)
{
double* P3; //intermediate variable.
originalFunction(P1, P2, P3);
myP_res = P3;
}
int main()
{
double P1[3];
double P2[3];
double P_res[3];
double myP_res[3];
P1[0] = 2;
P1[1] = 1;
P1[2] = 4;
P2[0] = 100;
P2[1] = 240;
P2[2] = 310;
originalFunction(P1, P2, P_res);
myFunction(P1, P2, myP_res);
printf("Output:\n");
printf("P_resX: %lf\n", P_res[0]);
printf("P_resY: %lf\n", P_res[1]);
printf("P_resZ: %lf\n", P_res[2]);
printf("myP_resX: %lf\n", myP_res[0]);
printf("myP_resY: %lf\n", myP_res[1]);
printf("myP_resZ: %lf\n", myP_res[2]);
)
return(0);
}
This compiles, however when I try to run this I get a cygwin error:
0 [main] myFileName 30504 cygwin_exception::open_stackdumpfile: Dumping stack trace to myFileName.exe.stackdump
Researching this tells me that this can happen when a function doesn't receive a pointer when it expects to. However I cannot work out why this doesn't happen with the original functions, and why it does happen with mine.
I've also tried definining P3 above without the pointer but then I get an incompatible type error at compile time.
Any ideas what is going on?
Many thanks!
Edit: I've realised after reviewing comments that this is not a minimum working example, my apologies. I tried to simplify the code in order to ask the question but didn't ensure that this simplified code was actually correct and there are mistakes in it which have rightly been picked up on by other people, but which don't exist in the original code and aren't part of my problem (the FACTOR multiplication for example). I'm not going to the edit the code now as I think I'll cause more confusion.