Click to See Complete Forum and Search --> : why crash?


amatorc++
August 2nd, 2008, 09:11 AM
1: string hist = getenv("HISTFILE");
2: if (hist != "/dev/null")
3: return -1;

the program crashes at 1
# Caught: basic_string::_S_construct NULL not valid
# Type: St11logic_error

it makes no sense since HISTFILE is not null
# echo $HISTFILE
/root/.bash_history


anyway i tried
1: char * hist = getenv("HISTFILE");
and
1: const char * hist = getenv("HISTFILE");
2: if (strcmp(hist,"/dev/null") != 0)
3: return -1;
and i get seg fault

it guess i get seg fault because the value of the variable is again null and i can't compare null with something... but why is it null instead or /root/.bash_history ? o.O

Lindley
August 2nd, 2008, 10:07 AM
Environment variables are tricky things. Have you closed and re-opened your IDE since setting that value? If not, it may not have propagated.

Also, on Windows at least, there can be difficulty if you try to use one environment variable in the definition of another. Are you doing that at all?

amatorc++
August 2nd, 2008, 10:51 AM
no idea what you're talking about
i don't think you understood the issue :(

Lindley
August 2nd, 2008, 10:57 AM
I understood it just fine. That environment variable isn't being found.

Oh, and std::strings can't take NULL in their constructors. You should check for that explicitly.

amatorc++
August 2nd, 2008, 11:44 AM
yep the variables isn't being found :-S no idea why

i knew about the null thingy

Lindley
August 2nd, 2008, 12:51 PM
Without knowing more about how you set its value and how you're trying to run the program, I can't really help. However, it is important to realize that environment variables are not system-wide. They only exist for *children* of the process where they're set. If you're using XP and you set this variable via the System control panel, try restarting your machine.

amatorc++
August 2nd, 2008, 02:09 PM
no need to set that variable
every *nix system has it in the env

pm_kirkham
August 2nd, 2008, 02:26 PM
Echo will print the variable whether or not it is exported to the child process; by default, HISTFILE is not exported - "export -p | grep HISTFILE" outputs nothing.

But you should fix your code so it can handle getenv returning NULL and exit gracefully.

amatorc++
August 2nd, 2008, 02:52 PM
Echo will print the variable whether or not it is exported to the child process; by default, HISTFILE is not exported - "export -p | grep HISTFILE" outputs nothing.

But you should fix your code so it can handle getenv returning NULL and exit gracefully.

wow thx for the answer dude
it makes sense