linked list problem

Discussion in 'C Programming' started by bofh1234@hotmail.com, May 2, 2006.

  1. Guest

    Hello,

    I am having a problem with linked lists. My program is based on a
    client server model. The client sends some packets of data to the
    server. The server reads those packets and is supposed to store the
    data in a linked list. It looks like everything works except for the
    fact that the linked list only stores the last value sent and the
    number of nodes in the linked list is way to high. For example the
    client sends 4 create requests to the server, there should be 4 nodes
    in the server but there turns out to be 12. All 12 contain the values
    of the last request. I am sure the problem is with the linked list
    code. If I put in an array it works, but I need a varible size so I
    have to use a linked list. Here is my server code.

    When create is called the correct varname and varval are printed.
    However when findvar is called things go wrong. I am including the
    output from a run of the server. From the output there appear to be
    two problems, 1) the table does not get created correctly 2) the
    findvar function is going crazy most likely due to the table not being
    created correctly. What am I doing wrong with the linked lists?

    The client code is 4 creates followed by reading those 4 values from
    the table.

    Thanks,
    Mike

    /* server code */
    #include <sys/types.h>
    #include <sys/signal.h>
    #include <sys/wait.h>
    #include <netinet/in.h>

    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <messages.h>

    #define QLEN 5 /* maximum connection queue length */

    #define or ||
    #define and &&

    struct Table1 {
    int vartype;
    char *varname;
    char *ptr;
    struct Table1 *next;
    };
    struct Table1 *Table1ptr=NULL, *temp;

    extern int errno;

    int serverA(int);
    void create(int, struct Table1 **);
    void findvar(int, struct Table1 *);
    void updatevar(int);
    void deletevar(int);
    void reaper(int);

    int errexit(const char *format, ...);

    int passiveTCP(const char *service, int qlen);

    int i=0;

    /*-------------------------------------------------------------------
    * main
    *-----------------------------------------------------------------*/
    int main(int argc, char *argv[])
    {
    char *service = "3445"; /* service name or port number */
    struct sockaddr_in fsin; /* the address of a client */
    int alen; /* length of client's address */
    int msock; /* master server socket */
    int ssock; /* slave server socket */

    switch (argc) {
    case 1:
    break;
    case 2:
    service = argv[1];
    break;
    default:
    errexit("usage: serverA [port]\n");
    }

    msock = passiveTCP(service, QLEN);

    (void) signal(SIGCHLD, reaper);
    alen = sizeof(fsin);
    ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
    exit(dsmd(ssock));
    }

    /*-------------------------------------------------------------------
    * serverA
    *-----------------------------------------------------------------*/
    int serverA(int fd)
    {
    int size, n, msgtype=0;
    char *bufptr;

    for ( ; ; ) {
    bufptr=(char*)(&msgtype);
    size=sizeof(int);

    while ((n=read(fd, bufptr, size)) >0) {
    size-=n;
    bufptr+=n;
    }
    if (msgtype == 0)
    break;

    switch(msgtype) {
    case 1:
    create(fd, &Table1ptr);
    break;
    case 2:
    findvar(fd, Table1ptr);
    break;
    case 3:
    updatevar(fd);
    break;
    case 4:
    deletevar(fd);
    break;
    default:
    errexit("The server is shutting down due to a problem\n");
    }
    msgtype=0;
    }
    return 0;
    }

    /*-------------------------------------------------------------------
    * create procedure for creating variables
    *------------------------------------------------------------------*/
    void create(int fd, struct Table1 **q)
    {
    int n, size;
    char *createpack;
    struct Table1 *temp;

    createpacket.requesttype=1;
    createpack=(char*)(&createpacket)+4;
    size=sizeof(createpacket)-sizeof(int);

    while ((n = read(fd, (void*)createpack, size)) >0 ){
    size-=n;
    createpack+=n;
    }

    temp = *q;
    if(*q==NULL) {
    *q=malloc(sizeof(struct Table1));
    temp = *q;
    }
    else {
    while((temp->next)!=NULL) {
    temp=temp->next;
    }
    temp->next = malloc(sizeof(struct Table1));
    temp=temp->next;
    }

    temp->varname=createpacket.varname;
    temp->vartype=createpacket.vartype;
    temp->ptr=createpacket.varval;
    temp->next = NULL;

    rpacket.rescode=0;
    strcpy(rpacket.rvarval,createpacket.varval);
    strcpy(rpacket.rdescrip,"");
    printf("createvar temp-vartype created = %s \n", temp->varname);
    printf("createvar pt->ptr = %s \n", temp->ptr);

    write(fd, &rpacket, sizeof(rpacket));
    }

    /*-------------------------------------------------------------------
    * findvar
    *------------------------------------------------------------------*/
    void findvar(int fd, struct Table1 *pt)
    {
    int n, size;
    char *findpack;

    findpacket.requesttype=2;
    findpack=(char*)(&findpacket)+4;
    size=sizeof(findpacket)-sizeof(int);

    while ((n = read(fd, (void*)findpack, size)) >0){
    size-=n;
    findpack+=n;
    }

    while (pt!=NULL) {
    printf("findpacke varname = %s \n", findpacket.varname);
    printf("findvar pt->varname = %s \n", pt->varname);
    printf("findvar pt->ptr = %s \n", pt->ptr);
    printf("size = %d \n", i); i++;

    if (strcmp(findpacket.varname,pt->varname) == 0) {
    rpacket.rescode=0;
    strcpy(rpacket.rvarval,pt->ptr);
    break;
    }
    else {
    rpacket.rescode=-1;
    strcpy(rpacket.rdescrip,"varible not found");
    }
    pt=pt->next;
    }
    write(fd, &rpacket, sizeof(rpacket));
    }


    /* output from server */
    createvar temp-vartype created = a
    createvar pt->ptr = 140
    createvar temp-vartype created = b
    creatvar pt->ptr = 240
    createvar temp-vartype created = c
    createvar pt->ptr = 340
    createvar temp-vartype created = d
    createvar pt->ptr = 440
    findpacke varname = a
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 0
    findpacke varname = a
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 1
    findpacke varname = a
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 2
    findpacke varname = a
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 3
    findpacke varname = b
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 4
    findpacke varname = b
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 5
    findpacke varname = b
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 6
    findpacke varname = b
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 7
    findpacke varname = c
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 8
    findpacke varname = c
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 9
    findpacke varname = c
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 10
    findpacke varname = c
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 11
    findpacke varname = d
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 12


    /* this should be the output from the server */
    createvar temp-vartype created = a
    createvar pt->ptr = 140
    createvar temp-vartype created = b
    creatvar pt->ptr = 240
    createvar temp-vartype created = c
    createvar pt->ptr = 340
    createvar temp-vartype created = d
    createvar pt->ptr = 440
    findpacke varname = a
    findvar pt->varname = a
    findvar pt->ptr = 140
    size = 0
    findpacke varname = b
    findvar pt->varname = a
    findvar pt->ptr = 140
    size = 1
    findpacke varname = b
    findvar pt->varname = b
    findvar pt->ptr = 240
    size = 2
    findpacke varname = c
    findvar pt->varname = a
    findvar pt->ptr = 140
    size = 3
    findpacke varname = c
    findvar pt->varname = b
    findvar pt->ptr = 240
    size = 4
    findpacke varname = c
    findvar pt->varname = c
    findvar pt->ptr = 340
    size = 5
    findpacke varname = d
    findvar pt->varname = a
    findvar pt->ptr = 140
    size = 6
    findpacke varname = d
    findvar pt->varname = b
    findvar pt->ptr = 240
    size = 7
    findpacke varname = d
    findvar pt->varname = c
    findvar pt->ptr = 340
    size = 8
    findpacke varname = d
    findvar pt->varname = d
    findvar pt->ptr = 440
    size = 9
     
    , May 2, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hello,
    >
    > I am having a problem with linked lists. My program is based on a
    > client server model. The client sends some packets of data to the
    > server. The server reads those packets and is supposed to store the
    > data in a linked list. It looks like everything works except for the
    > fact that the linked list only stores the last value sent and the
    > number of nodes in the linked list is way to high. For example the
    > client sends 4 create requests to the server, there should be 4 nodes
    > in the server but there turns out to be 12. All 12 contain the values
    > of the last request. I am sure the problem is with the linked list
    > code. If I put in an array it works, but I need a varible size so I
    > have to use a linked list. Here is my server code.
    >
    > When create is called the correct varname and varval are printed.
    > However when findvar is called things go wrong. I am including the
    > output from a run of the server. From the output there appear to be
    > two problems, 1) the table does not get created correctly 2) the
    > findvar function is going crazy most likely due to the table not being
    > created correctly. What am I doing wrong with the linked lists?
    >
    > The client code is 4 creates followed by reading those 4 values from
    > the table.
    >
    > Thanks,
    > Mike
    >
    > /* server code */
    > #include <sys/types.h>
    > #include <sys/signal.h>
    > #include <sys/wait.h>
    > #include <netinet/in.h>
    >
    > #include <unistd.h>
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <string.h>
    > #include <messages.h>
    >
    > #define QLEN 5 /* maximum connection queue length */
    >
    > #define or ||
    > #define and &&
    >
    > struct Table1 {
    > int vartype;
    > char *varname;
    > char *ptr;
    > struct Table1 *next;
    > };
    > struct Table1 *Table1ptr=NULL, *temp;
    >
    > extern int errno;
    >
    > int serverA(int);
    > void create(int, struct Table1 **);
    > void findvar(int, struct Table1 *);
    > void updatevar(int);
    > void deletevar(int);
    > void reaper(int);
    >
    > int errexit(const char *format, ...);
    >
    > int passiveTCP(const char *service, int qlen);
    >
    > int i=0;
    >
    > /*-------------------------------------------------------------------
    > * main
    > *-----------------------------------------------------------------*/
    > int main(int argc, char *argv[])
    > {
    > char *service = "3445"; /* service name or port number */
    > struct sockaddr_in fsin; /* the address of a client */
    > int alen; /* length of client's address */
    > int msock; /* master server socket */
    > int ssock; /* slave server socket */
    >
    > switch (argc) {
    > case 1:
    > break;
    > case 2:
    > service = argv[1];
    > break;
    > default:
    > errexit("usage: serverA [port]\n");
    > }
    >
    > msock = passiveTCP(service, QLEN);
    >
    > (void) signal(SIGCHLD, reaper);
    > alen = sizeof(fsin);
    > ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
    > exit(dsmd(ssock));
    > }
    >
    > /*-------------------------------------------------------------------
    > * serverA
    > *-----------------------------------------------------------------*/
    > int serverA(int fd)
    > {
    > int size, n, msgtype=0;
    > char *bufptr;
    >
    > for ( ; ; ) {
    > bufptr=(char*)(&msgtype);
    > size=sizeof(int);
    >
    > while ((n=read(fd, bufptr, size)) >0) {
    > size-=n;
    > bufptr+=n;
    > }
    > if (msgtype == 0)
    > break;
    >
    > switch(msgtype) {
    > case 1:
    > create(fd, &Table1ptr);
    > break;
    > case 2:
    > findvar(fd, Table1ptr);
    > break;
    > case 3:
    > updatevar(fd);
    > break;
    > case 4:
    > deletevar(fd);
    > break;
    > default:
    > errexit("The server is shutting down due to a problem\n");
    > }
    > msgtype=0;
    > }
    > return 0;
    > }
    >
    > /*-------------------------------------------------------------------
    > * create procedure for creating variables
    > *------------------------------------------------------------------*/
    > void create(int fd, struct Table1 **q)
    > {
    > int n, size;
    > char *createpack;
    > struct Table1 *temp;
    >
    > createpacket.requesttype=1;
    > createpack=(char*)(&createpacket)+4;
    > size=sizeof(createpacket)-sizeof(int);
    >
    > while ((n = read(fd, (void*)createpack, size)) >0 ){
    > size-=n;
    > createpack+=n;
    > }
    >
    > temp = *q;
    > if(*q==NULL) {
    > *q=malloc(sizeof(struct Table1));
    > temp = *q;
    > }
    > else {
    > while((temp->next)!=NULL) {
    > temp=temp->next;
    > }
    > temp->next = malloc(sizeof(struct Table1));
    > temp=temp->next;
    > }
    >
    > temp->varname=createpacket.varname;
    > temp->vartype=createpacket.vartype;
    > temp->ptr=createpacket.varval;
    > temp->next = NULL;
    >
    > rpacket.rescode=0;
    > strcpy(rpacket.rvarval,createpacket.varval);
    > strcpy(rpacket.rdescrip,"");
    > printf("createvar temp-vartype created = %s \n", temp->varname);
    > printf("createvar pt->ptr = %s \n", temp->ptr);
    >
    > write(fd, &rpacket, sizeof(rpacket));
    > }
    >
    > /*-------------------------------------------------------------------
    > * findvar
    > *------------------------------------------------------------------*/
    > void findvar(int fd, struct Table1 *pt)
    > {
    > int n, size;
    > char *findpack;
    >
    > findpacket.requesttype=2;
    > findpack=(char*)(&findpacket)+4;
    > size=sizeof(findpacket)-sizeof(int);
    >
    > while ((n = read(fd, (void*)findpack, size)) >0){
    > size-=n;
    > findpack+=n;
    > }
    >
    > while (pt!=NULL) {
    > printf("findpacke varname = %s \n", findpacket.varname);
    > printf("findvar pt->varname = %s \n", pt->varname);
    > printf("findvar pt->ptr = %s \n", pt->ptr);
    > printf("size = %d \n", i); i++;
    >
    > if (strcmp(findpacket.varname,pt->varname) == 0) {
    > rpacket.rescode=0;
    > strcpy(rpacket.rvarval,pt->ptr);
    > break;
    > }
    > else {
    > rpacket.rescode=-1;
    > strcpy(rpacket.rdescrip,"varible not found");
    > }
    > pt=pt->next;
    > }
    > write(fd, &rpacket, sizeof(rpacket));
    > }
    >


    [snip output]

    In create() even though you allocate memory for each Table1, you never
    allocate space for the varname and ptr fields. You just set them to
    values in createpacket, wherever that comes from. Unless you allocate
    new storage for those fields in createpacket somewhere else not listed
    here, it looks to me like all the Table1 entries would point to the
    same storage which would contain the last values set in them.

    Also it looks like some of your pointer arithmatic may be off and you
    aren't checking to make sure malloc doesn't fail.

    Dave.
     
    , May 2, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > Hello,
    >
    > I am having a problem with linked lists. My program is based on a
    > client server model. The client sends some packets of data to the
    > server. The server reads those packets and is supposed to store the
    > data in a linked list. It looks like everything works except for the
    > fact that the linked list only stores the last value sent and the
    > number of nodes in the linked list is way to high. For example the
    > client sends 4 create requests to the server, there should be 4 nodes
    > in the server but there turns out to be 12. All 12 contain the values
    > of the last request. I am sure the problem is with the linked list
    > code. If I put in an array it works, but I need a varible size so I
    > have to use a linked list. Here is my server code.
    >
    > When create is called the correct varname and varval are printed.
    > However when findvar is called things go wrong. I am including the
    > output from a run of the server. From the output there appear to be
    > two problems, 1) the table does not get created correctly 2) the
    > findvar function is going crazy most likely due to the table not being
    > created correctly. What am I doing wrong with the linked lists?
    >
    > The client code is 4 creates followed by reading those 4 values from
    > the table.
    >
    > Thanks,
    > Mike
    >
    > /* server code */
    > #include <sys/types.h>
    > #include <sys/signal.h>
    > #include <sys/wait.h>
    > #include <netinet/in.h>
    >
    > #include <unistd.h>
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <string.h>
    > #include <messages.h>
    >
    > #define QLEN 5 /* maximum connection queue length */
    >
    > #define or ||
    > #define and &&
    >
    > struct Table1 {
    > int vartype;
    > char *varname;
    > char *ptr;
    > struct Table1 *next;
    > };
    > struct Table1 *Table1ptr=NULL, *temp;
    >
    > extern int errno;
    >
    > int serverA(int);
    > void create(int, struct Table1 **);
    > void findvar(int, struct Table1 *);
    > void updatevar(int);
    > void deletevar(int);
    > void reaper(int);
    >
    > int errexit(const char *format, ...);
    >
    > int passiveTCP(const char *service, int qlen);
    >
    > int i=0;
    >
    > /*-------------------------------------------------------------------
    > * main
    > *-----------------------------------------------------------------*/
    > int main(int argc, char *argv[])
    > {
    > char *service = "3445"; /* service name or port number */
    > struct sockaddr_in fsin; /* the address of a client */
    > int alen; /* length of client's address */
    > int msock; /* master server socket */
    > int ssock; /* slave server socket */
    >
    > switch (argc) {
    > case 1:
    > break;
    > case 2:
    > service = argv[1];
    > break;
    > default:
    > errexit("usage: serverA [port]\n");
    > }
    >
    > msock = passiveTCP(service, QLEN);
    >
    > (void) signal(SIGCHLD, reaper);
    > alen = sizeof(fsin);
    > ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
    > exit(dsmd(ssock));
    > }
    >
    > /*-------------------------------------------------------------------
    > * serverA
    > *-----------------------------------------------------------------*/
    > int serverA(int fd)
    > {
    > int size, n, msgtype=0;
    > char *bufptr;
    >
    > for ( ; ; ) {
    > bufptr=(char*)(&msgtype);
    > size=sizeof(int);
    >
    > while ((n=read(fd, bufptr, size)) >0) {
    > size-=n;
    > bufptr+=n;
    > }
    > if (msgtype == 0)
    > break;
    >
    > switch(msgtype) {
    > case 1:
    > create(fd, &Table1ptr);
    > break;
    > case 2:
    > findvar(fd, Table1ptr);
    > break;
    > case 3:
    > updatevar(fd);
    > break;
    > case 4:
    > deletevar(fd);
    > break;
    > default:
    > errexit("The server is shutting down due to a problem\n");
    > }
    > msgtype=0;
    > }
    > return 0;
    > }
    >
    > /*-------------------------------------------------------------------
    > * create procedure for creating variables
    > *------------------------------------------------------------------*/
    > void create(int fd, struct Table1 **q)
    > {
    > int n, size;
    > char *createpack;
    > struct Table1 *temp;
    >
    > createpacket.requesttype=1;
    > createpack=(char*)(&createpacket)+4;
    > size=sizeof(createpacket)-sizeof(int);
    >
    > while ((n = read(fd, (void*)createpack, size)) >0 ){
    > size-=n;
    > createpack+=n;
    > }
    >
    > temp = *q;
    > if(*q==NULL) {
    > *q=malloc(sizeof(struct Table1));
    > temp = *q;
    > }
    > else {
    > while((temp->next)!=NULL) {
    > temp=temp->next;
    > }
    > temp->next = malloc(sizeof(struct Table1));
    > temp=temp->next;
    > }
    >
    > temp->varname=createpacket.varname;
    > temp->vartype=createpacket.vartype;
    > temp->ptr=createpacket.varval;
    > temp->next = NULL;
    >
    > rpacket.rescode=0;
    > strcpy(rpacket.rvarval,createpacket.varval);
    > strcpy(rpacket.rdescrip,"");
    > printf("createvar temp-vartype created = %s \n", temp->varname);
    > printf("createvar pt->ptr = %s \n", temp->ptr);
    >
    > write(fd, &rpacket, sizeof(rpacket));
    > }
    >
    > /*-------------------------------------------------------------------
    > * findvar
    > *------------------------------------------------------------------*/
    > void findvar(int fd, struct Table1 *pt)
    > {
    > int n, size;
    > char *findpack;
    >
    > findpacket.requesttype=2;
    > findpack=(char*)(&findpacket)+4;
    > size=sizeof(findpacket)-sizeof(int);
    >
    > while ((n = read(fd, (void*)findpack, size)) >0){
    > size-=n;
    > findpack+=n;
    > }
    >
    > while (pt!=NULL) {
    > printf("findpacke varname = %s \n", findpacket.varname);
    > printf("findvar pt->varname = %s \n", pt->varname);
    > printf("findvar pt->ptr = %s \n", pt->ptr);
    > printf("size = %d \n", i); i++;
    >
    > if (strcmp(findpacket.varname,pt->varname) == 0) {
    > rpacket.rescode=0;
    > strcpy(rpacket.rvarval,pt->ptr);
    > break;
    > }
    > else {
    > rpacket.rescode=-1;
    > strcpy(rpacket.rdescrip,"varible not found");
    > }
    > pt=pt->next;
    > }
    > write(fd, &rpacket, sizeof(rpacket));
    > }
    >


    [snip output]

    In create() even though you allocate memory for each Table1, you never
    allocate space for the varname and ptr fields. You just set them to
    values in createpacket, wherever that comes from. Unless you allocate
    new storage for those fields in createpacket somewhere else not listed
    here, it looks to me like all the Table1 entries would point to the
    same storage which would contain the last values set in them.

    Also it looks like some of your pointer arithmatic may be off and you
    aren't checking to make sure malloc doesn't fail.

    Dave.
     
    , May 2, 2006
    #3
  4. Guest

    Yeah I have to check if malloc returned NULL. I took it out to see if
    that was causing the problem. So I allocated memory for the struct
    Table1. I thought that when I did the malloc(sizeof (struct Table1))
    it allocated the memory for the struct and setup the memory inside the
    struct so I could start assigning things to it. I guess I was wrong.
    So what you are telling I have to do something like this (not real
    code)

    char *ptrtostructvarname;
    char *ptrtostructptr;
    int *ptrtostructvartype;

    *q = malloc(sizeof struct Table1)
    check if q is null
    *ptrtostructvarname=malloc(sizeof(createpacket.varname));
    *ptrtostructptr=malloc(sizeof(createpacket.varval));
    *ptrtostructvartype=malloc(sizeof(createpacket.vartype));

    Then how do I associate these new pointers to the Table?

    Thanks,
    Mike
     
    , May 2, 2006
    #4
  5. TJW Guest

    writes:

    > Yeah I have to check if malloc returned NULL. I took it out to see if
    > that was causing the problem.

    This is good practice, but is not your problem in this case.

    > So I allocated memory for the struct
    > Table1. I thought that when I did the malloc(sizeof (struct Table1))
    > it allocated the memory for the struct and setup the memory inside the
    > struct so I could start assigning things to it. I guess I was wrong.

    malloc allocates enough memory to store your structure. The
    elements of your structure are:
    > struct Table1 {
    > int vartype;
    > char *varname;
    > char *ptr;
    > struct Table1 *next;
    > };

    so you've allocated enough memory to store an int and 3
    pointers. The next step is to point the pointers to something
    meaningful. You've already done it once with the statement:
    > temp->next = malloc(sizeof(struct Table1));

    Now temp->next points to a place in memory allocated to store a
    structure. varname and ptr still have no meaning. You need the
    assignment
    temp->varname = malloc(sizeof(char));
    or better yet:
    tmp->varname = malloc(sizeof(*(tmp->varname)));
    and the equivalent for ptr. You should also check these values to
    assure that malloc succeeded in allocating the memory.

    Good Luck,
    -TJW
     
    TJW, May 2, 2006
    #5
  6. CBFalconer Guest

    wrote:
    >
    > I am having a problem with linked lists. My program is based on a
    > client server model. The client sends some packets of data to the
    > server. The server reads those packets and is supposed to store the
    > data in a linked list. It looks like everything works except for the
    > fact that the linked list only stores the last value sent and the
    > number of nodes in the linked list is way to high. For example the
    > client sends 4 create requests to the server, there should be 4 nodes
    > in the server but there turns out to be 12. All 12 contain the values
    > of the last request. I am sure the problem is with the linked list
    > code. If I put in an array it works, but I need a varible size so I
    > have to use a linked list. Here is my server code.
    >
    > When create is called the correct varname and varval are printed.
    > However when findvar is called things go wrong. I am including the
    > output from a run of the server. From the output there appear to be
    > two problems, 1) the table does not get created correctly 2) the
    > findvar function is going crazy most likely due to the table not being
    > created correctly. What am I doing wrong with the linked lists?
    >
    > The client code is 4 creates followed by reading those 4 values from
    > the table.
    >
    > /* server code */
    > #include <sys/types.h>
    > #include <sys/signal.h>
    > #include <sys/wait.h>
    > #include <netinet/in.h>
    >
    > #include <unistd.h>
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <string.h>
    > #include <messages.h>


    None of those include files, apart from stdlib.h, stdio.h, string.h
    have anything to do with the standard C language. Reduce your
    problem to something expressible (and compilable) in standard C, in
    about 100 lines or less, and then ask again. Otherwise take it all
    to some newsgroup that deals with your particular system. This is
    not it.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, May 2, 2006
    #6
  7. Guest

    wrote:
    > Yeah I have to check if malloc returned NULL. I took it out to see if
    > that was causing the problem. So I allocated memory for the struct
    > Table1. I thought that when I did the malloc(sizeof (struct Table1))
    > it allocated the memory for the struct and setup the memory inside the
    > struct so I could start assigning things to it. I guess I was wrong.
    > So what you are telling I have to do something like this (not real
    > code)
    >
    > char *ptrtostructvarname;
    > char *ptrtostructptr;
    > int *ptrtostructvartype;
    >
    > *q = malloc(sizeof struct Table1)
    > check if q is null
    > *ptrtostructvarname=malloc(sizeof(createpacket.varname));
    > *ptrtostructptr=malloc(sizeof(createpacket.varval));
    > *ptrtostructvartype=malloc(sizeof(createpacket.vartype));
    >
    > Then how do I associate these new pointers to the Table?
    >
    > Thanks,
    > Mike


    Sortof...

    // conditional to find end of list and allocate temp
    ...
    // temp is allocated and ready to be populated

    temp->varname = malloc( number of bytes to hold the name );
    temp->ptr = malloc( number of bytes to hold whatever ptr points at
    );

    strncpy (temp->varname, createpacket.varname, number of bytes
    allocated );

    // this should be ok as it is
    temp->vartype=createpacket.vartype;

    memcpy (temp->ptr, createpacket.varval, number of bytes allocated );

    // this is also ok as is
    temp->next = NULL;

    // rest of create()
    ...

    You have to remember now when free-ing each Table1 element in the list,
    first free the memory allocated for varname and ptr.
     
    , May 2, 2006
    #7
  8. Joe Smith Guest

    "CBFalconer" <> wrote:
    [OP]> I am having a problem with linked lists. My program is based on a
    >> client server model. The client sends some packets of data to


    [non Standard stuff snipped]
    > None of those include files, apart from stdlib.h, stdio.h, string.h
    > have anything to do with the standard C language. Reduce your
    > problem to something expressible (and compilable) in standard C, in
    > about 100 lines or less, and then ask again. Otherwise take it all
    > to some newsgroup that deals with your particular system. This is
    > not it.


    Every time I've seen a post that endeavors to do something with the
    client/server model, it goes OT but quickly. Is this inevitable? Joe
     
    Joe Smith, May 2, 2006
    #8
  9. CBFalconer Guest

    Joe Smith wrote:
    > "CBFalconer" <> wrote:
    >
    >>> I am having a problem with linked lists. My program is based on a
    >>> client server model. The client sends some packets of data to

    >
    > [non Standard stuff snipped]
    >> None of those include files, apart from stdlib.h, stdio.h, string.h
    >> have anything to do with the standard C language. Reduce your
    >> problem to something expressible (and compilable) in standard C, in
    >> about 100 lines or less, and then ask again. Otherwise take it all
    >> to some newsgroup that deals with your particular system. This is
    >> not it.

    >
    > Every time I've seen a post that endeavors to do something with the
    > client/server model, it goes OT but quickly. Is this inevitable?


    There are neither clients nor servers mentioned in the C standard.
    So yes, barring writing everything in standard C. I do not
    consider this practical for such an application.

    Of course you could define client and server in such a way as to
    make it feasible.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, May 3, 2006
    #9
  10. TJW Guest

    CBFalconer <> writes:

    >> When create is called the correct varname and varval are printed.
    >> However when findvar is called things go wrong. I am including the
    >> output from a run of the server. From the output there appear to be
    >> two problems, 1) the table does not get created correctly 2) the
    >> findvar function is going crazy most likely due to the table not being
    >> created correctly. What am I doing wrong with the linked lists?
    >>

    <headers snipped>
    > None of those include files, apart from stdlib.h, stdio.h, string.h
    > have anything to do with the standard C language. Reduce your
    > problem to something expressible (and compilable) in standard C, in
    > about 100 lines or less, and then ask again. Otherwise take it all
    > to some newsgroup that deals with your particular system. This is
    > not it.


    But in this particular case his question is essentially "I am
    having problems putting data in a linked list. Here is my code
    (that may or may not have something to do with clients and
    servers):"

    I agree that ideally the system specific stuff should have been
    pruned and the code compilable, but his question at heart was a
    relatively basic C question so I didn't mind answering it too
    much. Is this the wrong attitude for this group?

    --
    -TJW
     
    TJW, May 3, 2006
    #10
  11. Vladimir Oka Guest

    TJW opined:

    > CBFalconer <> writes:
    >
    >>> When create is called the correct varname and varval are printed.
    >>> However when findvar is called things go wrong. I am including the
    >>> output from a run of the server. From the output there appear to
    >>> be two problems, 1) the table does not get created correctly 2) the
    >>> findvar function is going crazy most likely due to the table not
    >>> being
    >>> created correctly. What am I doing wrong with the linked lists?
    >>>

    > <headers snipped>
    >> None of those include files, apart from stdlib.h, stdio.h, string.h
    >> have anything to do with the standard C language. Reduce your
    >> problem to something expressible (and compilable) in standard C, in
    >> about 100 lines or less, and then ask again. Otherwise take it all
    >> to some newsgroup that deals with your particular system. This is
    >> not it.

    >
    > But in this particular case his question is essentially "I am
    > having problems putting data in a linked list. Here is my code
    > (that may or may not have something to do with clients and
    > servers):"


    I think the problem in these cases really boils down to:

    a) there was too much code, i.e. OP should pare down to minimal
    compilable example that still exhibits the problem
    b) existence of non-standard features makes it difficult to say with
    confidence that the problem is *not* in those same features (about
    which c.l.c crowd may not know much or anything at all), but in
    the rest of the, possibly standard, code

    > I agree that ideally the system specific stuff should have been
    > pruned and the code compilable, but his question at heart was a
    > relatively basic C question so I didn't mind answering it too
    > much. Is this the wrong attitude for this group?


    No, but removing obstacles as above, should still be encouraged, if not
    enforced.

    --
    "Open Standards, Open Documents, and Open Source"

    -- Scott Bradner (Open Sources, 1999 O'Reilly and Associates)

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
     
    Vladimir Oka, May 3, 2006
    #11
  12. Joe Smith Guest

    "TJW" <> wrote in message
    news:...
    > CBFalconer <> writes:

    [non Standard stuff can hit the road]

    > But in this particular case his question is essentially "I am
    > having problems putting data in a linked list. Here is my code
    > (that may or may not have something to do with clients and
    > servers):"
    >
    > I agree that ideally the system specific stuff should have been
    > pruned and the code compilable, but his question at heart was a
    > relatively basic C question so I didn't mind answering it too
    > much. Is this the wrong attitude for this group?
    >
    > --
    > -TJW


    [freshly snipped elsewhere]
    This is always a temptation:

    <NOTHING-TO-DO-WITH-STANDARD-C-HERE>

    int opt = 1;

    setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
    sizeof(opt));

    </NOTHING-TO-DO-WITH-STANDARD-C-HERE>

    I don't know why this couldn't have been posted at alt.alt.test with an
    appropriate link for the curious. My guess is that if you _struggle_ with
    topicality, then you do not have the wrong attitude for this group. Joe
     
    Joe Smith, May 3, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    514
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    503
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    548
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    705
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    810
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page