Segmentation fault...

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

    #1

    Segmentation fault...

    I don't know what I'm doing wrong ??

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <string.h>


    struct irc_server
    {
    //glowne dane
    char *hostname;
    int port;
    char *username;
    //kanaly
    int number_of_chann els;
    char *channels[IRC_MAX_CHANNEL S];
    //socket
    int sockfd;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    };

    int irc_init_server (struct irc_server *server, char *username, char
    *hostname, int port)
    {
    //alokowanie pamieci glownej struktory
    server = (struct irc_server*)mal loc(sizeof(stru ct irc_server));
    //wpisujemy podstawowe informacje
    server->hostname = (char*)malloc(s izeof(char)*str len(hostname));
    strcpy(server->hostname, hostname);

    server->username = (char*)malloc(s izeof(char)*str len(username));
    strcpy(server->username, username);

    server->port=port;
    //socket
    if((server->sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
    {
    return 1;
    }
    //reszte informacji o serververze
    server->server = gethostbyname(s erver->hostname);
    if(server->server == NULL)
    {
    return 2;
    }
    server->serv_addr.sin_ family = AF_INET;
    server->serv_addr.sin_ port = htons(server->port);
    server->serv_addr.sin_ addr = *((struct in_addr
    *)server->server->h_addr);
    memset(&(server->serv_addr.sin_ zero), '\0', 8);
    // printf("hostnam e %s\n", server->hostname);
    return 0;
    }

    main()
    {
    struct irc_server* server;
    printf("start\n ");
    printf("initcod e: %d\n", irc_init_server (server, "bak",
    "www.wp.pl" , 7666));
    if(server == NULL)
    {
    printf("null\n" );
    }
    else
    {
    printf("not null> %d\n", server->hostname); //and I get
    Segmentation fault.
    }
    }

    what is wrong??

    PS. THX
  • Rob van der Leek

    #2
    Re: Segmentation fault...

    In article <2a8066d6.04062 20726.dddea21@p osting.google.c om>, Michal J wrote:
    ....SNIP...
    [color=blue]
    > int irc_init_server (struct irc_server *server, char *username, char
    > *hostname, int port)
    > {[/color]

    ....SNIP...
    [color=blue]
    > main()
    > {
    > struct irc_server* server;
    > printf("start\n ");
    > printf("initcod e: %d\n", irc_init_server (server, "bak",
    > "www.wp.pl" , 7666));
    > if(server == NULL)
    > {
    > printf("null\n" );
    > }
    > else
    > {
    > printf("not null> %d\n", server->hostname); //and I get
    > Segmentation fault.
    > }
    > }
    >
    > what is wrong??[/color]

    Hint: initialize 'server' as NULL before calling irc_init_server () and
    look what happens. Also remember that C passes parameters by value.

    Regards,
    --
    Rob van der Leek | rob(at)ricardis (dot)tudelft(do t)nl
    Ricardishof 73-A | http://www.ricardis.tudelft.nl/~rob
    2614 JE Delft, The Netherlands
    +31 (0)6 155 244 60

    Comment

    • Alex Fraser

      #3
      Re: Segmentation fault...

      "Michal J" <baniak_spam@el ektromet.pl> wrote in message
      news:2a8066d6.0 406220726.dddea 21@posting.goog le.com...[color=blue]
      > I don't know what I'm doing wrong ??
      >
      > #include <stdio.h>
      > #include <stdlib.h>
      > #include <sys/types.h>
      > #include <sys/socket.h>
      > #include <netinet/in.h>
      > #include <netdb.h>
      > #include <string.h>[/color]

      Various non-standard headers here, although that is not the source of your
      problem.
      [color=blue]
      > struct irc_server
      > {
      > //glowne dane[/color]

      "//" comments are not part of C, except for C99. They are also generally a
      bad idea in code posted to Usenet.
      [color=blue]
      > char *hostname;
      > int port;
      > char *username;
      > //kanaly
      > int number_of_chann els;
      > char *channels[IRC_MAX_CHANNEL S];
      > //socket
      > int sockfd;
      > struct sockaddr_in serv_addr;
      > struct hostent *server;
      >
      > };
      >
      > int irc_init_server (struct irc_server *server, char *username, char
      > *hostname, int port)
      > {
      > //alokowanie pamieci glownej struktory
      > server = (struct irc_server*)mal loc(sizeof(stru ct irc_server));[/color]

      The above cast is not necessary and, had you inadvertently done so, would
      have hidden the fact that you forgot to include <stdlib.h> causing undefined
      behaviour. I prefer:

      server = malloc(sizeof *server);

      This, BTW, modifies the local variable server, which is initialised with the
      value specified as the caller's argument.
      [color=blue]
      > //wpisujemy podstawowe informacje
      > server->hostname = (char*)malloc(s izeof(char)*str len(hostname));[/color]

      sizeof(char) is 1 by definition, so it is a useless factor here. You need to
      allocate one more byte than strlen() returns, to accomodate the terminating
      0:

      server->hostname = malloc(strlen(h ostname) + 1);
      [color=blue]
      > strcpy(server->hostname, hostname);
      >
      > server->username = (char*)malloc(s izeof(char)*str len(username));[/color]

      Same as above.
      [color=blue]
      > strcpy(server->username, username);
      >
      > server->port=port;
      > //socket
      > if((server->sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
      > {
      > return 1;
      > }
      > //reszte informacji o serververze
      > server->server = gethostbyname(s erver->hostname);[/color]

      <OT> Better to check if the string specifies an IP address first. </OT>
      [color=blue]
      > if(server->server == NULL)
      > {
      > return 2;
      > }
      > server->serv_addr.sin_ family = AF_INET;
      > server->serv_addr.sin_ port = htons(server->port);
      > server->serv_addr.sin_ addr = *((struct in_addr
      > *)server->server->h_addr);
      > memset(&(server->serv_addr.sin_ zero), '\0', 8);[/color]

      <OT> memset() here is a waste of time </OT>
      [color=blue]
      > // printf("hostnam e %s\n", server->hostname);
      > return 0;
      > }[/color]

      Now, the local variable server has gone out of scope, causing a memory leak.
      [color=blue]
      >
      > main()[/color]

      Yuck.

      int main(void)
      [color=blue]
      > {
      > struct irc_server* server;
      > printf("start\n ");
      > printf("initcod e: %d\n", irc_init_server (server, "bak",
      > "www.wp.pl" , 7666));[/color]

      The call to irc_init_server () is passed the _value_ of the uninitialised
      pointer server. Nothing that irc_init_server () does will change the value
      seen here.
      [color=blue]
      > if(server == NULL)[/color]

      Undefined behaviour, since server is uninitialised.
      [color=blue]
      > {
      > printf("null\n" );
      > }
      > else
      > {
      > printf("not null> %d\n", server->hostname); //and I get[/color]


      The format specifier and type of the supplied argument do not agree.
      [color=blue]
      > Segmentation fault.[/color]

      Syntax error. Now do you see why it's a bad idea to post code with "//"
      comments?
      [color=blue]
      > }[/color]

      return 0;
      [color=blue]
      > }
      >
      > what is wrong??[/color]

      Quite a bit :).

      Alex


      Comment

      • Alex Vinokur

        #4
        Re: Segmentation fault...


        "Michal J" <baniak_spam@el ektromet.pl> wrote in message news:2a8066d6.0 406220726.dddea 21@posting.goog le.com...[color=blue]
        > I don't know what I'm doing wrong ??[/color]
        [snip][color=blue]
        > struct irc_server
        > {
        > //glowne dane
        > char *hostname;
        > int port;
        > char *username;
        > //kanaly
        > int number_of_chann els;
        > char *channels[IRC_MAX_CHANNEL S];
        > //socket
        > int sockfd;
        > struct sockaddr_in serv_addr;
        > struct hostent *server;
        >
        > };[/color]
        [snip]
        [color=blue]
        > main()
        > {
        > struct irc_server* server;[/color]
        server = malloc(sizeof(s truct irc_server));
        if (!server)
        {
        printf ("malloc failed\n");
        return 1;
        }
        [color=blue]
        > printf("start\n ");
        > printf("initcod e: %d\n", irc_init_server (server, "bak", "www.wp.pl" , 7666));
        > if(server == NULL)
        > {
        > printf("null\n" );
        > }
        > else
        > {[/color]
        /* Attention! server->hostname not initialized */[color=blue]
        > printf("not null> %d\n", server->hostname); //and I get Segmentation fault.[/color]
        /*
        Perhaps
        printf("not null> %s\n", server->hostname);
        */[color=blue]
        > }
        > }
        >[/color]
        [snip]


        --
        Alex Vinokur

        http://sourceforge.net/users/alexvn



        Comment

        • Dan Pop

          #5
          Re: Segmentation fault...

          In <2a8066d6.04062 20726.dddea21@p osting.google.c om> baniak_spam@ele ktromet.pl (Michal J) writes:
          [color=blue]
          >I don't know what I'm doing wrong ??[/color]

          You're posting off topic code to comp.lang.c.
          [color=blue]
          >#include <sys/types.h>
          >#include <sys/socket.h>
          >#include <netinet/in.h>
          >#include <netdb.h>[/color]

          Dan
          --
          Dan Pop
          DESY Zeuthen, RZ group
          Email: Dan.Pop@ifh.de

          Comment

          • xarax

            #6
            Re: Segmentation fault...

            "Dan Pop" <Dan.Pop@cern.c h> wrote in message
            news:cb9nb7$6se $3@sunnews.cern .ch...[color=blue]
            > In <2a8066d6.04062 20726.dddea21@p osting.google.c om> baniak_spam@ele ktromet.pl[/color]
            (Michal J) writes:[color=blue]
            >[color=green]
            > >I don't know what I'm doing wrong ??[/color]
            >
            > You're posting off topic code to comp.lang.c.
            >[color=green]
            > >#include <sys/types.h>
            > >#include <sys/socket.h>
            > >#include <netinet/in.h>
            > >#include <netdb.h>[/color][/color]

            The off-topic pieces are irrelevant to the OP's problem.

            He is passing the parameter "server" by value, rather
            than by address, so the pointer to the allocated memory
            is lost upon return to main(). He needs to change the
            function to accept a pointer to a pointer to the struct,
            and to store the malloc() pointer through a dereference.
            He also needs to initialize the "server" variable, and
            test for NULL from malloc(), change '//' comments to
            the '/* */' style, fix the main() signature, and so on.

            --
            ----------------------------
            Jeffrey D. Smith
            Farsight Systems Corporation
            24 BURLINGTON DRIVE
            LONGMONT, CO 80501-6906

            z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!



            Comment

            • Dan Pop

              #7
              Re: Segmentation fault...

              In <Ur4Cc.10709$w0 7.8377@newsread 2.news.pas.eart hlink.net> "xarax" <xarax@email.co m> writes:
              [color=blue]
              >"Dan Pop" <Dan.Pop@cern.c h> wrote in message
              >news:cb9nb7$6s e$3@sunnews.cer n.ch...[color=green]
              >> In <2a8066d6.04062 20726.dddea21@p osting.google.c om> baniak_spam@ele ktromet.pl[/color]
              >(Michal J) writes:[color=green]
              >>[color=darkred]
              >> >I don't know what I'm doing wrong ??[/color]
              >>
              >> You're posting off topic code to comp.lang.c.
              >>[color=darkred]
              >> >#include <sys/types.h>
              >> >#include <sys/socket.h>
              >> >#include <netinet/in.h>
              >> >#include <netdb.h>[/color][/color]
              >
              >The off-topic pieces are irrelevant to the OP's problem.[/color]

              Then, they should have been removed *before* posting the code, which
              was littered with items coming from them.

              Dan
              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              • Allin Cottrell

                #8
                Re: Segmentation fault...

                Dan Pop wrote:[color=blue]
                > In <Ur4Cc.10709$w0 7.8377@newsread 2.news.pas.eart hlink.net> "xarax" <xarax@email.co m> writes:
                >
                >[color=green]
                >>"Dan Pop" <Dan.Pop@cern.c h> wrote in message
                >>news:cb9nb7$6 se$3@sunnews.ce rn.ch...
                >>[color=darkred]
                >>>In <2a8066d6.04062 20726.dddea21@p osting.google.c om> baniak_spam@ele ktromet.pl[/color]
                >>
                >>(Michal J) writes:
                >>[color=darkred]
                >>>>I don't know what I'm doing wrong ??
                >>>
                >>>You're posting off topic code to comp.lang.c.
                >>>
                >>>
                >>>>#include <sys/types.h>
                >>>>#include <sys/socket.h>
                >>>>#include <netinet/in.h>
                >>>>#include <netdb.h>[/color]
                >>
                >>The off-topic pieces are irrelevant to the OP's problem.[/color]
                >
                > Then, they should have been removed *before* posting the code, which
                > was littered with items coming from them.[/color]

                However, you have prided yourself on several past occasions on making
                just the move xarax made, namely noticing that the *actual* issue in
                an apparently off-topic post was in fact an issue addressable within
                standard C.

                Allin Cottrell

                Comment

                • Dan Pop

                  #9
                  Re: Segmentation fault...

                  In <cbd4q2$1dum$1@ f1n1.spenet.wfu .edu> Allin Cottrell <cottrell@wfu.e du> writes:
                  [color=blue]
                  >Dan Pop wrote:[color=green]
                  >> In <Ur4Cc.10709$w0 7.8377@newsread 2.news.pas.eart hlink.net> "xarax" <xarax@email.co m> writes:
                  >>
                  >>[color=darkred]
                  >>>"Dan Pop" <Dan.Pop@cern.c h> wrote in message
                  >>>news:cb9nb7$ 6se$3@sunnews.c ern.ch...
                  >>>
                  >>>>In <2a8066d6.04062 20726.dddea21@p osting.google.c om> baniak_spam@ele ktromet.pl
                  >>>
                  >>>(Michal J) writes:
                  >>>
                  >>>>>I don't know what I'm doing wrong ??
                  >>>>
                  >>>>You're posting off topic code to comp.lang.c.
                  >>>>
                  >>>>
                  >>>>>#include <sys/types.h>
                  >>>>>#include <sys/socket.h>
                  >>>>>#include <netinet/in.h>
                  >>>>>#include <netdb.h>
                  >>>
                  >>>The off-topic pieces are irrelevant to the OP's problem.[/color]
                  >>
                  >> Then, they should have been removed *before* posting the code, which
                  >> was littered with items coming from them.[/color]
                  >
                  >However, you have prided yourself on several past occasions on making
                  >just the move xarax made, namely noticing that the *actual* issue in
                  >an apparently off-topic post was in fact an issue addressable within
                  >standard C.[/color]

                  Indeed. But the code did make sense in the absence of the platform
                  specific stuff.

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de

                  Comment

                  Working...