What is the wrong with this code?

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

    #1

    What is the wrong with this code?

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <time.h>


    struct nlpPkt
    {
    int source:4; int destin:4;
    unsigned int control:1;
    unsigned int contype:5;
    int length:10;
    int checksum:16;
    union
    {
    char data[1500]; //info holds message/packet if the event type is
    msg/pck arrival.
    struct TLPPKT *tlp;
    } nlpData;

    } NLPPKT; //same size

    struct tlpPkt
    {
    int sequence:7;
    int ack:8;
    int length:10;
    int blankbit:5;
    int End:1;
    char tlpData[1469];

    } TLPPKT;


    int main(){

    NLPPKT *nlppkt;
    TLPPKT *tlppkt;
    char str[8888] = "asbcdefghijklm nopqrstuvwxyz";

    tlppkt=(TLPPKT *)malloc(sizeof (TLPPKT));
    nlppkt=(NLPPKT *)malloc(sizeof (NLPPKT));

    tlppkt->sequence =1;
    tlppkt->ack =2;
    tlppkt->length =1000;
    tlppkt->blankbit = 0;
    tlppkt->End=1;
    tlppkt->tlpData=str;

    memcpy( nlppkt->nlpData.tlp, tlppkt, sizeof(TLPPKT)) ;
    printf("nlppkt. nlpdata.data is %s\n",nlppkt->nlpData.tlp) ;

    return 0;
    }

  • Chris McDonald

    #2
    Re: What is the wrong with this code?

    [code snipped]

    Ummmm, not enough comments?

    --
    Chris.

    Comment

    • pete

      #3
      Re: What is the wrong with this code?

      yezi wrote:
      [color=blue]
      > #include <unistd.h>[/color]
      [color=blue]
      > #include <sys/types.h>
      > #include <sys/socket.h>
      > #include <netinet/in.h>
      > #include <arpa/inet.h>
      > #include <netdb.h>[/color]

      That code has too many headers that I don't have.



      --
      pete

      Comment

      • Keith Thompson

        #4
        Re: What is the wrong with this code?

        "yezi" <ye_line@hotmai l.com> writes:[color=blue]
        > #include <stdio.h>
        > #include <stdlib.h>
        > #include <unistd.h>
        > #include <errno.h>
        > #include <string.h>
        > #include <sys/types.h>
        > #include <sys/socket.h>
        > #include <netinet/in.h>
        > #include <arpa/inet.h>
        > #include <netdb.h>
        > #include <time.h>[/color]
        [snip][color=blue]
        > tlppkt=(TLPPKT *)malloc(sizeof (TLPPKT));
        > nlppkt=(NLPPKT *)malloc(sizeof (NLPPKT));[/color]
        [snip]

        Please put the question in the body of the article as well as in the
        subject. Not all newsreaders display the subject properly.

        The code is poorly indented, making it difficult to read.

        It casts the result of malloc(). This isn't strictly incorrect, but
        it's unnecessary and can mask certain errors. The quoted lines should
        be:

        tlppkt = malloc(sizeof *tlppkt);
        nlppkt = malloc(sizeof *nlppkt);

        It uses several headers that are not defined by the C standard. It
        should be discussed in a system-specific newsgroup, possibly
        comp.unix.progr ammer.

        When you do post in an appropriate newsgroup, please provide more
        information than just asking what's wrong with the code. You need to
        specify what the code actually does, what you wanted it to do, and how
        those differ.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • Barry Schwarz

          #5
          Re: What is the wrong with this code?

          On 10 Nov 2005 16:15:02 -0800, "yezi" <ye_line@hotmai l.com> wrote:

          There is no question in your message. Some readers cannot see the
          question in your title. Ask your questions in the body of the
          message.

          The answer to your question is just about everything.

          You don't tell us what the code is supposed to do or how what it
          actually does differs from the desired result. Only a few here claim
          clairvoyance.
          [color=blue]
          >#include <stdio.h>
          >#include <stdlib.h>
          >#include <unistd.h>[/color]

          None standard headers. How do non-unix types know what this is?
          [color=blue]
          >#include <errno.h>
          >#include <string.h>
          >#include <sys/types.h>
          >#include <sys/socket.h>
          >#include <netinet/in.h>
          >#include <arpa/inet.h>
          >#include <netdb.h>
          >#include <time.h>
          >
          >
          >struct nlpPkt
          >{
          > int source:4; int destin:4;
          > unsigned int control:1;
          > unsigned int contype:5;
          > int length:10;
          > int checksum:16;
          > union
          > {
          > char data[1500]; //info holds message/packet if the event type is
          >msg/pck arrival.[/color]

          // type comments frequently wrap rendering your code uncompilable.
          [color=blue]
          > struct TLPPKT *tlp;
          > } nlpData;
          >
          >} NLPPKT; //same size[/color]

          I give up. Same size as what?
          [color=blue]
          >
          >struct tlpPkt
          >{
          > int sequence:7;
          > int ack:8;
          > int length:10;
          > int blankbit:5;
          > int End:1;
          > char tlpData[1469];
          >
          >} TLPPKT;
          >
          >
          >int main(){
          >
          >NLPPKT *nlppkt;
          >TLPPKT *tlppkt;
          >char str[8888] = "asbcdefghijklm nopqrstuvwxyz";
          >
          >tlppkt=(TLPP KT *)malloc(sizeof (TLPPKT));[/color]

          You should not cast the return from malloc.
          [color=blue]
          >nlppkt=(NLPP KT *)malloc(sizeof (NLPPKT));
          >
          >tlppkt->sequence =1;
          >tlppkt->ack =2;
          >tlppkt->length =1000;[/color]

          length is signed and 10 bits wide. The max value it can hold is 511.
          [color=blue]
          >tlppkt->blankbit = 0;
          >tlppkt->End=1;[/color]

          End is signed and 1 bit wide. The max value it can hold is 0.
          [color=blue]
          >tlppkt->tlpData=str;[/color]

          tlpData is an array. It cannot appear on the left of an assignment.
          [color=blue]
          >
          >memcpy( nlppkt->nlpData.tlp, tlppkt, sizeof(TLPPKT)) ;[/color]

          tlp is an uninitialized pointer. You cannot pass an uninitialized
          value to a function.
          [color=blue]
          >printf("nlppkt .nlpdata.data is %s\n",nlppkt->nlpData.tlp) ;[/color]

          When initialized properly, tlp will point to a struct. The first
          member of the struct is not a string.
          [color=blue]
          >
          >return 0;
          >}[/color]


          <<Remove the del for email>>

          Comment

          • Jack Klein

            #6
            Re: What is the wrong with this code?

            On Thu, 10 Nov 2005 18:11:40 -0800, Barry Schwarz <schwarzb@deloz .net>
            wrote in comp.lang.c:
            [color=blue]
            > On 10 Nov 2005 16:15:02 -0800, "yezi" <ye_line@hotmai l.com> wrote:[/color]

            [snip]
            [color=blue][color=green]
            > >struct nlpPkt
            > >{
            > > int source:4; int destin:4;
            > > unsigned int control:1;
            > > unsigned int contype:5;
            > > int length:10;
            > > int checksum:16;
            > > union
            > > {
            > > char data[1500]; //info holds message/packet if the event type is
            > >msg/pck arrival.[/color]
            >
            > // type comments frequently wrap rendering your code uncompilable.
            >[color=green]
            > > struct TLPPKT *tlp;
            > > } nlpData;
            > >
            > >} NLPPKT; //same size[/color][/color]

            [snip]
            [color=blue][color=green]
            > >tlppkt->length =1000;[/color]
            >
            > length is signed and 10 bits wide. The max value it can hold is 511.[/color]

            It is implementation-defined whether 'length' is signed or unsigned.
            If the OP's implementation makes it unsigned, the maximum value it can
            hold is 1023.
            [color=blue][color=green]
            > >tlppkt->blankbit = 0;
            > >tlppkt->End=1;[/color]
            >
            > End is signed and 1 bit wide. The max value it can hold is 0.[/color]

            Ditto, except of course that the maximum value if unsigned is 1.

            6.7.2 Type specifiers P. 5: "Each of the comma-separated sets
            designates the same type, except that for bit-fields, it is
            implementation-defined whether the specifier int designates the same
            type as signed int or the same type as unsigned int."

            --
            Jack Klein
            Home: http://JK-Technology.Com
            FAQs for
            comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
            comp.lang.c++ http://www.parashift.com/c++-faq-lite/
            alt.comp.lang.l earn.c-c++

            Comment

            • yezi

              #7
              Re: What is the wrong with this code?

              I am really new with bit manipulation .Through the comments, I think it
              is the basi bit problem .Would you provide some good websit to read?

              Thanks

              Comment

              Working...