So I have a function that amongst other arguments takes in an argument double* X.
The function behaves so that X is not mutated, its values are simply used for calculations.
However I have observed this weird behaviour where when I print I get different values of the same index of X, example:
So I know this probably is a bit hard with only a snippet of code, but is there any way the values of a pointer can be changed inside a function like this? I guess I am just asking for suggestions of possibilities.
I have even tried making X a const double* in the function, but this still happens...
Yeah, you need to show how it's called, what r is (where it's defined, what is it's size), and the value of nInd4. I suspect it isn't looping and writing out of bounds since that would overwrite the X pointer itself which should give pretty wacky results, most likely a segfault.
"r" was created as an array of doubles, and then the pointer to this array was passed on to the function, so that it had the type double* in the function arguments.
I do not think "r" had the right number of values... (this is some code I have adopted)
Do you have an explanation of what went wrong?
And thanks a lot for the feedback, much appreciated!
r would have to have been allocated with "new" if you wanted to pass it out of the function, otherwise r will be deallocated when the function returns, leaving your out pointer dangling.
If both r and x were created as arrays on the stack in the caller and then passed into the function, then writing past the end of r could write into x and change its values.