Hi All,

I am getting segmentation fault when I am allocating memory for the node second time.

struct host_msgs_list
{
[B]struct host_msgs_struct *message;[/B]
struct host_msgs_list *next;
};

struct host_msgs_list *root, *temp, *temp1;
root = NULL;
temp=root;

For the first message (i.e. OHAD), it’s able to allocate the memory. 
We are able to process single message, but we are facing difficulty in processing multiple messages.

if (strncmp (&in_rec[OM_TRAN_CODE],OHAD, 4)==0)
{
rec_cnt = 1;
got_ohad = TRUE;
if(root == NULL)
{
/* Assign memory to Root */
root = (struct host_msgs_list*) malloc (sizeof(struct host_msgs_list));
/* Assign message */
memcpy(root->message,&in_rec,s_siz);
/* Next link */
root->next = NULL;
}
/* If Root is not NULL, the link list is not empty.
In this case, use Temp and traverse thro' the link list
and reach to the last element which is NULL. */
else
{
/* Traverse through the link list to find NULL element. */
while(temp->next != NULL)
{
temp = temp->next;
}

/* Assign memory to the last link. */

temp->next = (struct host_msgs_list*) malloc (sizeof(struct host_msgs_list));
/* Point to the location to which
you assigned the memory. */
temp = temp->next ;

/* Assign message
to the new memory location. */
memcpy(temp->message,&in_rec,s_siz);
/* Next link */
temp->next = NULL;
}

While allocating memory for the second node, the program is failing with segmentation fault error.
We indentified the spot where the error is occurring, which is colored red in the post.
Please have a look at it and reply to my email id as well.

Thanks,
Thirupathi reddy,
email snipped

Recommended Answers

All 9 Replies

memcpy(root->message,&in_rec,s_siz);
memcpy(temp->message,&in_rec,s_siz);

You need to allocate memory for the message as well.

Otherwise you can make message a non pointer variable.

struct host_msgs_list
{
struct host_msgs_struct message;
struct host_msgs_list *next;
};

First error, message is uninitialized pointer after malloc. Following structure is what you are looking for I think. After changing this you had to change usage of mesage from pointer to structure.

struct host_msgs_list
{
struct host_msgs_struct message;
struct host_msgs_list *next;
};

Do not post help requests as Code Snippets.
Use CODE tags.
Don't post your email address.

Hi,

Thank you for quick reply.
Could you please elaborate more on this.

Regards,
Thirupathi

Hi ,

We have to use message as pointer as it is being referenced elsewhere in the code, if it has to be pointer, how can I avoid this error by allocating memory to message separately.
Please give detailed analysis, I am finding this hard to understand and got stuck up here with segmentation fault.

Thanks,
Thiru

root->message = (host_msgs_struct*) malloc (sizeof(struct host_msgs_struct));
/* Assign message */
memcpy(root->message...

Hi,

We have tried the above option, but still we are getting the same issue. We are getting the same issue even if we try to create a new node after the root as below.

** Here newnode is host_msg_list type.
newnode= (struct host_msgs_list*) malloc (sizeof(struct host_msgs_list));

memcpy(newnode->message,&in_rec,s_siz);
newnode->next = NULL;
root->next = newnode;

Could you please rewrite the required code in the above code snippet?
so that we can use the same code.

Thanks,
Thiru.

The code changes suggested by JuhaW should be maintained as such. Just because, you are still getting SEG fault doesn't make the necessity of allocating memory to a NULL pointer go away.

if (strncmp (&in_rec[OM_TRAN_CODE],OHAD, 4)==0)

Can you explain what is happening at this line. It looks like you are accessing 4 bytes beyond the end of the in_rec array. You should consider changing that to

if (strncmp (in_rec,OHAD, 4)==0)

I hope OHAD is a character array that you have defined?

struct host_msgs_list *root, *temp, *temp1;
root = NULL;
temp=root;

Are all these local variable or are these global variables ?

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.