Values changing without reason?

This is a discussion on Values changing without reason? within the C Programming forums, part of the General Programming Boards category; I'm having problems with the below code. Basically, every time the function ADPCMEncoder is called, prevsample and previndex get reset ...

Values changing without reason?

I'm having problems with the below code. Basically, every time the function ADPCMEncoder is called, prevsample and previndex get reset to 0, for no reason that I can see... I've set a breakpoint on the last line of ADPCMEncoder after they're assigned values and they're fine, then when I set a breakpoint in main on the line after the function call, they've become 0, but I can't see what's making them do it?!

> if ((infile = fopen(argv[i],"r")) == NULL)
If this is a binary file, then you should use "rb" (and "wb" for writing), otherwise end of line character translations can make a real mess of your data.

Likewise, such translations can make your simple attempt to work out the length of the file meaningless. Always use the result of fread() to decide on success/fail, not some 'guess' as to how many times around the loop you should go.

I'm having problems with the below code. Basically, every time the function ADPCMEncoder is called, prevsample and previndex get reset to 0, for no reason that I can see... I've set a breakpoint on the last line of ADPCMEncoder after they're assigned values and they're fine, then when I set a breakpoint in main on the line after the function call, they've become 0, but I can't see what's making them do it?!

Throw it in a debugger and put a watch point on one of those variables. It'll break at the moment it changes, and you can see what's doing it. None of the code you posted appears to be capable of causing this.

There are usually two possibilities why a global variable is improperly overwritten. One, you may be overrunning some global buffer somewhere, and stepping on the other variables. Two, you may have made a change to a header file which changed the size of some structure (and therefore the arrangement of global variables in memory), but failed to correctly recompile a module which uses that structure.