GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2004, 17:08
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

Problem with the function creating updating a linked list


Hi all,
I have this function,which builds dynamic linked list from the data received in connections. It complies fine. But when I build the first node in the list, the next pointer is somehow now getting assigned to NULL, which causes my while loops where in I check for NULL value, go on indefinitely. Following my function. I guess it must be something simple that i am missing but just cant figure out where. Pls help. I have sprinkled lots of printf statment in function for debugging purpose.

CPP / C++ / C Code:
boolean push_to_conn_queue(_conn_q_ptr **conn_q_ptr, _ppacket qpacket, _conn_q **queue)
{
				/*
                                                   typedef struct connq  _conn_q;
				struct connq
				{	
					short conn_id;
					_command command;
					long src_session_id;
					long dst_session_id;
					_conn_q *next;
				};

				struct connqptr
				{
					_conn_q *head;
					_conn_q *tail;
				};

								                                      typedef struct connqptr  _conn_q_ptr;
				*/
	
	static short conn_id=1;
	_conn_q *temp=NULL;

	printf("\nqueue: %d\n",conn_id);

	if ((*queue)==NULL)
	{	
		temp=(_conn_q *)malloc(sizeof (_conn_q *));
		if (temp==NULL)
		{
			fprintf(stderr,"%s(): Malloc Failed\n",__FUNCTION__);
			return FALSE;
		}
		(*conn_q_ptr)=(_conn_q_ptr *)malloc(sizeof (_conn_q_ptr *));
		if (*conn_q_ptr==NULL)
		{
			fprintf(stderr,"%s(): Malloc Failed\n",__FUNCTION__);
			return FALSE;
		}
		fprintf(stderr,"%s(): In If making temp node\n",__FUNCTION__);
		temp->conn_id=conn_id;
		temp->command=qpacket.packet.command;
		temp->src_session_id=qpacket.packet.src_session_id;
		temp->dst_session_id=qpacket.packet.dst_session_id;
		temp->next=NULL;
		fprintf(stderr,"%s(): Finished making temp node\n",__FUNCTION__);
		(*queue)=temp;
		(*conn_q_ptr)->head=temp;
		(*conn_q_ptr)->tail=temp;
		fprintf(stderr,"%s(): Created head and tail of queue\n",__FUNCTION__);
		printf("\nConnection %d queued\n",conn_id);
		conn_id++;
		return TRUE;
	} else {
		temp=(_conn_q *)malloc(sizeof (_conn_q *));
		if (temp==NULL)
		{
			printf("\n%s(): Malloc Failed\n",__FUNCTION__);
			return FALSE;
		}
		fprintf(stderr,"%s(): In else creating temp node\n",__FUNCTION__);
		temp->conn_id=conn_id;
		temp->command=qpacket.packet.command;
		temp->src_session_id=qpacket.packet.src_session_id;
		temp->dst_session_id=qpacket.packet.dst_session_id;
		temp->next=NULL;
		fprintf(stderr,"\n%s(): In else finished creating temp node\n",__FUNCTION__);
		int i=0;
		while ((*queue)->next!=NULL)  //looping starts here. 
		{
			(*queue)=(*queue)->next;
			printf("i= %d\n",++i);
			continue;
		}
		(*queue)->next=temp;
		(*conn_q_ptr)->tail=temp;
		fprintf(stderr,"%s(): In else changed tail\n",__FUNCTION__);
		printf("\nConnection %d queued\n",conn_id);
		conn_id++;
		return TRUE;
	}
	return TRUE;
}

this how I am defining queue and queue pointer variables in the parent function and calling this function.

CPP / C++ / C Code:
boolean create_open_command_channel(int *tx_qid,int *rx_qid,int *cmd_ch_proc_id,struct process_stack **proc_stack)
{
_conn_q *conn_q=NULL;
_conn_q_ptr *conn_q_ptr=NULL;

*cmd_ch_proc_id=fork();
		if (*cmd_ch_proc_id==0)
		{
		//printf("\nchild proc_id: %d\n",getpid());
		

		for (; ; )
		{
			if ((bytes_rcvd=msgrcv(*rx_qid,&rpacket,sizeof(_packet),1,IPC_NOWAIT))==-1)
			{
				msgctl((*rx_qid, IPC_RMID, NULL);
			} else {
				
					if (bytes_rcvd>0)
					{
							printf("\nbytes received: %d\n",bytes_rcvd);
							printf("\nConnection received.\n");
							if ((result=push_to_conn_queue (&conn_q_ptr,rpacket,&conn_q))==TRUE)
							{
								printf("\nConnection pushed onto queue\n");
							} else {
								printf("\nFailed push connection on to thequeue\n");
							} 

....rest of the code for function

Any suggestions ?

Thanks,
  #2  
Old 28-Oct-2004, 18:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nkhambal
Hi all,
I

Any suggestions ?

Thanks,

I haven't looked at everything, but I don't think you are malloc'ing what you need.

For example, you are getting storage for the struct, but you have
CPP / C++ / C Code:
 temp=(_conn_q *)malloc(sizeof (_conn_q *));

Shouldn't this be

CPP / C++ / C Code:
 temp=(_conn_q *)malloc(sizeof (_conn_q));

???

Look at your other malloc() stuff also.

Regards,

Dave
  #3  
Old 28-Oct-2004, 20:26
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Thanks dave,

That was the problem, I guess? I got little confused there with pointer to pointer convension that I didn't realize it. However I still wonder how it worked for first and second call to the function. I tried printing queue in the parent function and it kept printing the first connection data cause NULL was never hit. I guess, i was just lucky not to see a crash.

Thanks,
  #4  
Old 28-Oct-2004, 20:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nkhambal
Thanks dave,

That was the problem, I guess? I got little confused there with pointer to pointer convension that I didn't realize it. However I still wonder how it worked for first and second call to the function. I tried printing queue in the parent function and it kept printing the first connection data cause NULL was never hit. I guess, i was just lucky not to see a crash.

Thanks,

When a pointer points outside of its allocated area, it points somewhere. If it happens to point to some other allocated area, things may seem to be OK --- until the rightful owner of that area uses it (or tries to use it). Then all heck breaks loose --- sometimes.

Sometimes everything seems to be OK, but then at some later date it falls apart. Sometimes everything seems to work OK with a given set of test data, but then blows up when actual user data is encountered.

In your case you were lucky to have it misbehave with a fairly small test.

The bottom line: test, test, test, but remember that you can't prove a program is correct just by testing.

Good Luck, and

Best regards,

Dave
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 20:33
Insert problem in linked list with two function code Kay Chan C++ Forum 1 03-Sep-2004 09:52
help on linked lists any1????? nick4 C Programming Language 1 17-May-2004 09:32
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.