simple problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J Wang

    simple problem


    the c code as follows:

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    printf("%s\n", getenv("NAME")) ;
    return 0;
    }

    Segmentation Fault

    why?

    cheers,

  • Leor Zolman

    #2
    Re: simple problem

    On Fri, 9 Apr 2004 23:51:26 GMT, J Wang <csmjbw@bath.ac .uk> wrote:
    [color=blue]
    >
    >the c code as follows:
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >int main()
    >{
    > printf("%s\n", getenv("NAME")) ;
    > return 0;
    >}
    >
    >Segmentation Fault
    >
    >why?[/color]

    NULL.
    -leor
    [color=blue]
    >
    >cheers,[/color]


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Mike Wahler

      #3
      Re: simple problem

      "J Wang" <csmjbw@bath.ac .uk> wrote in message
      news:Pine.GSO.4 .44.04041000381 90.13163-100000@amos.bat h.ac.uk...[color=blue]
      >
      > the c code as follows:
      >
      > #include <stdio.h>
      > #include <stdlib.h>
      >
      > int main()
      > {[/color]

      char *name = getenv("NAME");
      [color=blue]
      > printf("%s\n", getenv("NAME")) ;[/color]

      if(name)
      printf("%s\n", name);
      else
      printf("%s\n", "getenv() returned NULL");
      [color=blue]
      > return 0;
      > }
      >
      > Segmentation Fault
      >
      > why?[/color]

      Most likely you tried to dereference a NULL pointer.

      *Always* check the return value of a function
      which can fail.

      -Mike


      Comment

      • Martin Ambuhl

        #4
        Re: simple problem

        J Wang wrote:
        [color=blue]
        > the c code as follows:
        >
        > #include <stdio.h>
        > #include <stdlib.h>
        >
        > int main()
        > {
        > printf("%s\n", getenv("NAME")) ;
        > return 0;
        > }
        >
        > Segmentation Fault
        >
        > why?
        >[/color]

        Perhaps "NAME" is not defined in the environment, so getenv() returns
        NULL and your attempt to dereference it is illegal. We just had this a
        couple of days ago. That you did not see it shows that you violated
        usenet etiqutte by posting without following the newsgroup first. You
        probably didn't check the FAQ either.

        Comment

        • Barry Schwarz

          #5
          Re: simple problem

          On Fri, 9 Apr 2004 23:51:26 GMT, J Wang <csmjbw@bath.ac .uk> wrote:
          [color=blue]
          >
          >the c code as follows:
          >
          >#include <stdio.h>
          >#include <stdlib.h>
          >
          >int main()
          >{
          > printf("%s\n", getenv("NAME")) ;
          > return 0;
          >}
          >
          >Segmentation Fault
          >
          >why?
          >[/color]

          getenv is allowed to return NULL. You need to check for that
          situation before dereferencing the return value.


          <<Remove the del for email>>

          Comment

          Working...