CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: why crash?

  1. #1
    Join Date
    Jul 2008
    Posts
    17

    why crash?

    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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: why crash?

    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?

  3. #3
    Join Date
    Jul 2008
    Posts
    17

    Re: why crash?

    no idea what you're talking about
    i don't think you understood the issue

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: why crash?

    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.

  5. #5
    Join Date
    Jul 2008
    Posts
    17

    Re: why crash?

    yep the variables isn't being found :-S no idea why

    i knew about the null thingy

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: why crash?

    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.

  7. #7
    Join Date
    Jul 2008
    Posts
    17

    Re: why crash?

    no need to set that variable
    every *nix system has it in the env

  8. #8

    Re: why crash?

    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.

  9. #9
    Join Date
    Jul 2008
    Posts
    17

    Re: why crash?

    Quote Originally Posted by pm_kirkham
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured