Posts Tagged ‘c

atoi(getenv(“MYVAR”))

Posted in Technicalon May 28, 2008

These are mostly personal notes. I’m not sure if they will make sense to others.

I had a problem with a line of code like this:
int x = atoi(getenv("MYVAR"));

If this line was in daemon-ized code started at init level 2, it would segfault. (I’m not sure if these conditions are necessary, but those were my conditions).

getenv("UNKNOWN_VAR") == NULL
atoi(NULL) should == 0

But for some reason, things were segfaulting. I corrected the problem with:
if (getenv("MYVAR") == NULL) int x = 0;
else int x = atoi(getenv("MYVAR"));

I came across this line of code today, and while I don’t normally hate other people’s coding style, this one has something in it that really bothers me:

i = ((unsigned int)(crcAccum >> 24) ^ *dataBlkPtr++ ) & 0xff;

Categories