C scanf arbitrarily deletes contents of another variable???
Ok, I am in the process of writing a program that plays the game mancala. I wanted to create a function that requires the player to choose a number between one and six, should be simple right? I kept having problems, so I started testing stuff out. This chunk of code is giving me a real headache:
Code:
#include <stdio.h> Code:
adam@tiamat:~/Mancala$ gcc scanf_test.c I am using gcc version 4.3.2 (Debian 4.3.2-1.1). On a whim, I tried something else out; when I change the 'short' variables to 'int' , the problem seems to dissapear.... I tried changing the format string in the scanf statement from "%d" to "%u" which is listed as the appropriate string for the 'short' type, still no luck. Any insight is gratefully appreciated. |
M is not big enough so you are overwriting memory erasing i
try changing short M to long M |
Quote:
You know, you might be under some obligation to use C-era functions, but C++ has much more robust methods to deal with these issues -- methods that don't get confused like this, and that are easier to understand as well. |
Quote:
|
%u indicates unsigned, not short. Try %hd
And use gcc -Wall as it would warn you Code:
wim@btd-techweb01:~/progs/lq$ gcc -Wall main.c -o mytest |
Thank you guys! So lets say I declare three variables; int X int Y and int Z inside some function. I am telling the compiler to allocate three blocks of memory that are the size of 'int' four bytes I think? and these are located immediately one after the other? I haven't had a chance yet to test stuff out. I am basically doing the 'teach yourself C ' thing, C++ with all of its classes and member functions seems both incredibly powerful but extremely intimidating!!! The -Wall flag stands for 'all warnings' or something similar? Thanks guys for all of your insight on this.
|
Quote:
Quote:
If you compile without optimization, the compiler is likely to place them that way. If you use all their addresses (as with scanf) then even with optimization, the compiler might place them that way. But there is no rule about that, so don't try to depend on the placement. |
I think I have encountered precisely what johnsfine just said. When I add a printf line, to look at the address of each variable, The problem dissappears:
Code:
short move() Incidentally, the addresses of each variable look like they are adjacent to each other Code:
Address of M: 0xbfabcee6, Address of i: 0xbfabcee4 |
JUST USE INT INSTEAD OF SHORT.
why not? |
You don't want to care about that; that's what high level languages are for. Just use the proper types or the %hd as per my previous post. I you want to care about those things, use assembly ;)
The only time that you need to be aware of it is if you want to dump data from e.g. a file or serial port into a struct. |
Quote:
To find the address of any variable X, take &X, which explicitly means "return the address of X". This is the core mechanism of pointers, which are very important in both C and C++. In the context of your game, you shouldn't need a lot of this, but in almost all circumstances you can make your program more efficient by using pointers. |
Quote:
Code:
short M; Any change that makes the "problem disappear" just means there is no symptom to clobbering those two bytes. Quote:
Quote:
If you knew assembler language, you could dump a disassembly of the compiler binary and see the relative addresses of the variables. In this case, that is what you actually care about, not their absolute addresses. From what you have posted so far, the compiler you are using seems to allocate M first and then i when you don't use &i in that printf, but it allocates i first and then M when you do. When you don't have that printf, an optimizing compile would probably only allocate M. i wouldn't even have an address, because the function is so simple i could be in a register. |
Quote:
|
Why :scratch: My reply was related to How would I find out the addressing of these variables without printing them? Are the actual addresses of variables located somewhere in the compiled binary?
|
All times are GMT -5. The time now is 07:11 AM. |