[Solved] Not passing expected value in function [duplicate]


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:

char char_buffer[] = "What ever";

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).

1

solved Not passing expected value in function [duplicate]