I have a function:
int somefunction(int a, int b, char *c, int d){}
Now this function is called in an other function which is in main().
I call somefunction(a, b, c, d); and I have it print out int a, witch is declared as 50, before somefunction() is called. In the first call 50 is printed, all good and dandy, but the second call of somefunction() prints out 29549.
These function calls are inside some if statements, and even if the second place were somefunction() is called, is called first it’s the same.
For testing all the input values have been set as the same, so its not the input.
All in put values is printed before calling somefunction(), and are as they should be. I have tried renaming all variables, functions, I tried to change all int’s to float, and I tried moving the order of the variables in somefunction(). But noting changed. I have made prototypes as well, it’s not that.
So does anyone have any idea of what could course this? The actual code is not here because I passed 2000 lines, and there would be a lot of irrelevant code in between. I realize it might be hard to help me, so I am only asking if anyone have experienced anything similar.
So, if I’m using a language with pointers and unsafe arrays, and crazy things start happening, then I have to soon suspect that I’m overwriting memory. There are many ways to do this: writing past the end of an array, using an uninitialized pointer, free()ing something that was never malloc()ed, etc.
If that’s what’s happening in your case, it’s very hard to solve it without seeing the precise and complete code (trying to make a simplified example will often just make the problem go away as you either remove the erroneous code or rearrange memory so that something different and perhaps innocuous is being overwritten).
If you can’t post a complete example that exhibits the bug, then you can start looking at likely culprits: a) all use of dynamic memory functions, b) all dereferencing of pointers.
About all I could glean from your other post was a) yup, that’s the kind of code that’s ripe for wild pointers and b) hmmmm, what’s this:
Hard to guess from code fragments, but usually the word “buffer” implies some biggish amount of space that’s going to get reused more than once. You’ve named a character pointer “char_buffer”, but it actually points to a string constant. So, for example, if you later tried to copy 11 characters worth of data to it, then you would overwrite something else (assuming the compiler/linker didn’t store the constant in read-only memory for you).