views:

167

answers:

6

Hi -

I haven't written C in quite some time and am writing an app using the MySQL C API, compiling in g++ on redhat.

So i start outputting some fields with printfs... using the oracle api, with PRO*C, which i used to use (on suse, years ago), i could select an int and output it as:

int some_int;
printf("%i",some_int);

I tried to do that with mysql ints and i got 8 random numbers displayed... i thought this was a mysql api issue and some config issue with my server, and i wasted a few hours trying to fix it, but couldn't, and found that i could do:

int some_int;
printf("%s",some_int);

and it would print out the integer properly. Because i'm not doing computations on the values i am extracting, i thought this an okay solution.

UNTIL I TRIED TO COUNT SOME THINGS....

I did a simple:

int rowcount;
for([stmt]){
rowcount++;
}
printf("%i",rowcount);

i am getting an 8 digit random number again... i couldn't figure out what the deal is with ints on this machine.

then i realized that if i initialize the int to zero, then i get a proper number.

can someone please explain to me under what conditions you need to initialize int variables to zero? i don't recall doing this every time in my old codebase, and i didn't see it in the example that i was modeling my mysql_stmt code from...

is there something i'm missing? also, it's entirely possible i've forgotten this is required each time

thanks...

+1  A: 

If you don't initialize your variables, there's no guarantee of a default 0/NULL/whatever value. Some compilers MIGHT initialize it to 0 for you (IIRC, MSVC++ 6.0 would be kind enough to do so), and others might not. So don't rely on it. Never use a variable without first giving it some sort of sane value.

FreeMemory
A: 

Variable are not automatically initialized in c.

MarkusQ
+1  A: 

You have indeed forgotten. In C and C++, you don't get any automatic initialization; the contents of c after int c; are whatever happens to be at the address referred to by c at the time.

Best practice: initialize at the definition: int c = 0;.

Oh, PS, and take some care that the MySQL int type matches the C int type; I think it does but I'm not positive. It will be, however, both architecture and compiler sensitive, since sizeof(int) isn't the same in all C environments.

Charlie Martin
+1  A: 

Only global and static values will be initialized to zero. The variables on the stack will always contain garbage value if not initialized.

int g_var; //This is a global varibale. So, initialized to zero
int main()
{

int s_var = 0; //This is on stack. So, you need to explicitly initialize
static int stat_var;  //This is a static variable,  So, initialized to zero
}
chappar
perfect! this is the difference that i was looking for... thanks so much.
to clarify, i use all globals for the bind variable at the top of the program, which lead to my fogginess on this issue... thanks
Are global variables initialized to zero on Windows? I had a vague recollection that it was a UNIX behavior only, but I may be wrong.
ephemient
A: 

Uninitialized variable.

int some_int = 0;
Sanjaya R
+1  A: 

You always neet to initialize your variables. To catch this sort of error, you should probably compile with -Wall to give you all warnings that g++ can provide. I also prefer to use -Werror to make all warnings errors, since it's almost always the case that a warning indicates an error or a potential error and that cleaning up the code is better than leaving it as is.

Also, in your second printf, you used %s which is for printing strings, not integers.

int i = 0;
printf("%d\n", i);
// or
printf("%i\n", i);

Is what you want.

Brian Campbell