struct fifo
{
    int result;
    char *info;
    struct fifo *next;
}*node;

void add(struct fifo **n,int data,char *text)
{
    struct fifo *temp,*node1;
    if (*n==NULL)
    {
        temp=(struct fifo *)malloc(sizeof(struct fifo));
        printf("%d is to be stored\n",data);
        temp->result=data;
        printf("%d is stored\n",temp->result);
        temp->info=(char *)malloc(strlen(text)+1);
        strcpy(temp->info,text);
        temp->next=NULL;
        *n=temp;
    }
    else
    {
        temp=*n;
        while(temp->next!=NULL)
            temp=temp->next;
        node1=(struct fifo *)malloc(sizeof(struct fifo));
        node1->result=data;
        node1->info=(char *)malloc(strlen(text)+1);
        strcpy(node1->info,text);
        node1->next=NULL;
        temp->next=node1;
    }
}

void del(struct fifo **n)
{
    struct fifo *temp;
    if (*n==NULL)
    {
        printf("Linked list is empty\n");
    }
    else
    {
        temp=*n;
        *n=temp->next;
    }
}

//Inside read thread that will add to the linked list
            //If a particular condition is satisfied
               EnterCriticalSection(&critical);
               add(&node,50,location);                //location holds the string read
               LeaveCriticalSection(&critical);       //exit condition after this

//Inside main thread that reads & deletes from the linked list
InitializeCriticalSection(&critical);

while(1)
{    
     if (node==NULL)
    {
        printf("Nothing to read\n");
        Sleep(4000);
    }
    else
    {
        EnterCriticalSection(&critical);
        temp=(struct fifo *)malloc(sizeof(struct fifo));
        printf("%d\n%s\n",node->result,node->info);
        op=node->result;
        printf("op is %d\n",op);
        detail=(char *) malloc (strlen(node->info));
        strcpy(detail,node->info);
        printf("%s\n",detail);
        del(&node);
        LeaveCriticalSection(&critical);
        if (node==NULL)
            printf("node is null\n");
        else
            printf("node is not null\n");
        if (op==50)                                 
        {
            //call function with detail as parameter...call is successful
            free(detail);
            Sleep(2000);
            //Make some other function calls
            op=-1;
            if (node==NULL)
                    printf("%d\n",op);
            else
                    printf("node is not null\n");
            printf("%d\n",op);
        }
        //some other similar conditions 
}

DeleteCriticalSection(&critical);
//Close threads
return (0);
//End of main

Now, there are no errors in compilation. When I read something, main thread reads that & deletes the node. I get a confirmation of the same due to the printf given after the del(&node). Then, i go into my function calls, which give the right result. But at the end of it I get an error & my printf statement shows that on the 2nd check node is not null.
To make things clear this is how the output looks like:

Nothing to read
//after some time
50         //value of int stored in node
25         //string stored in node
op is 50
25
node is null
//results of function calls successfully printed
node is not null
-1

Please tell me what is wrong with the above code.

ohhh man, it is so difficult to go through your code man. It need to really concentrate on how to indent your code. Give some spacing between instruction and it so compack. And your comments, just makes the code really hard to read.

Do some work on it, i will have look at it. Without that it just not worth me wasting time on it.

ssharish

Sorry for the inconvenience. hope this will do:

//Code for Linked List 
struct fifo
{
    int result;                                                       
    char *info;
    struct fifo *next;
}*node;

void add(struct fifo **n,int data,char *text)
{
    struct fifo *temp,*node1;
    if (*n==NULL)
    {
        temp=(struct fifo *)malloc(sizeof(struct fifo));
        printf("%d is to be stored\n",data);
        temp->result=data;
        printf("%d is stored\n",temp->result);
        temp->info=(char *)malloc(strlen(text)+1);
        strcpy(temp->info,text);
        temp->next=NULL;
        *n=temp;
    }
    else
    {
        temp=*n;
        while(temp->next!=NULL)
            temp=temp->next;
        node1=(struct fifo *)malloc(sizeof(struct fifo));
        node1->result=data;
        node1->info=(char *)malloc(strlen(text)+1);
        strcpy(node1->info,text);
        node1->next=NULL;
        temp->next=node1;
    }
}

void del(struct fifo **n)
{
    struct fifo *temp;
    if (*n==NULL)
    {
        printf("Linked list is empty\n");
    }
    else
    {
        temp=*n;
        *n=temp->next;
    }
}
//Code for Linked List ends


//Inside read thread that will add to the linked list
            
      //If a particular condition is satisfied

               EnterCriticalSection(&critical);
               add(&node,50,location);          //location holds string read
               LeaveCriticalSection(&critical);    //exit condition after this

//End of read thread


//Inside main thread that reads & deletes from the linked list

InitializeCriticalSection(&critical);

while(1)
{    
     if (node==NULL)                             //idle case
    {
        printf("Nothing to read\n");
        Sleep(4000);
    }
    else                               //Linked List is not empty
    {
        EnterCriticalSection(&critical);

        temp=(struct fifo *)malloc(sizeof(struct fifo));   //Read node
        printf("%d\n%s\n",node->result,node->info);       
        op=node->result;
        printf("op is %d\n",op);
        detail=(char *) malloc (strlen(node->info));
        strcpy(detail,node->info);
        printf("%s\n",detail);
        del(&node);                               //delete node

        LeaveCriticalSection(&critical);

        if (node==NULL)                        //Check condition to confirm  
            printf("node is null\n");          //that node has been deleted
        else                     
            printf("node is not null\n");

        if (op==50)                  //op has been read from the node
        {
            //call function with detail as parameter...call is successful
            free(detail);

            Sleep(2000);
            //Make some other function calls

            op=-1;

            if (node==NULL)                 //Another check condition to 
                    printf("%d\n",op);      //confirm that node is empty
            else                
                    printf("node is not null\n");
            
        }
        //some other similar conditions 
}

DeleteCriticalSection(&critical);
//Close threads

return (0);
//End of main

Here's the output:

Nothing to read
//after some time
50         //value of int stored in node
25         //string stored in node

op is 50                 
25

node is null

//results of function calls successfully printed

node is not null

Here what I don't understand is why a node that has become null suddenly does not remain null after a few function calls. The function calls have nothing to do with the linked list.

Hey guyz I have managed to narrow down my problem. The read thread is reading input from a COM port. Now when it has to read a very large input, it does so in multiple read operations. The error that I am getting is in such a situation. When the thread had to read only a small string(one that could be read in a single read), I got the confirmation that the node is indeed null. But in case of a multiple read, it kept showing "node is not null".
Can anybody please tell why this is happening & how I should deal with this.

Look for buffer overflow -- writing beyond the end of a buffer. Post the whole read thread because the problems isn't in the code snipped you posted.

I have also been thinking that there must be some problem with the buffer but so far have been unable to find one. Well here's the read thread

int operation=100;
char resultcode[100];
HANDLE hread,mutex;

unsigned _stdcall read(void* HCom)                       
{
    node=NULL;
    OVERLAPPED osRead = {0};
    DWORD dwRead;

    // Create OVERLAPPED structure hEvent.
    osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (osRead.hEvent == NULL)
      // Error creating overlapped event handle.
      return (1);

    DWORD dwCommEvent;

    for ( ; ; )
    {
        WaitCommEvent(HCom, &dwCommEvent, &osRead);
        if ( WaitForSingleObject(osRead.hEvent,INFINITE) == WAIT_OBJECT_0)
        {
            char szBuf[100];
            char *location;
            do
            {
                memset(szBuf,0,sizeof(szBuf));
                ReadFile( HCom,szBuf,sizeof(szBuf),&dwRead,&osRead);    
                if(dwRead!=0)
                {
                     if(!strcmp(szBuf,"\r\nOK\r\n"))   
                    {
                        if (WaitForSingleObject(mutex,INFINITE)==WAIT_OBJECT_0)
                        add(&node,10,resultcode);
                        ReleaseMutex(mutex);
                    }
                    else if(!strncmp(szBuf,"\r\n*ECAV:",8))               
                    {
                        strcpy(resultcode,szBuf);
                    }
                    //Other such if conditions
                }
            }while (dwRead > 0 );
        }

        else
        {
            printf("Waiting failed with error no %d\n",GetLastError());
            operation=2;
        }

    }
    _endthreadex( 0 );
    return 0;
}


//here is the initialisation of the read thread in main

    hread=(HANDLE)_beginthreadex(NULL,0,read,hCom,0,&dwthread);
    if (hread==0)
    {
            printf("Creating thread failed with error no %d\n",GetLastError());
            return (1);
    }
    else
        printf("Thread activated\n");

    mutex=CreateMutex(NULL,FALSE,NULL);
    if (mutex==NULL)
        printf("Creation of mutex failed with error no %d\n",GetLastError());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.