typedef struct tag_linked_list linked_list;

struct tag_linked_list
{
	int value;
	linked_list* next;
};

typedef struct tag_list list;

struct tag_list
{
	int size;
	linked_list* data;
};

void list_insert(list* lst, int x)
{
	printf("lst->data %p\n",lst->data);
	lst->size++;

	// create a new node
	linked_list node;
	node.value = x;
	node.next = lst->data;

	// make it the new head
	printf("node %p\n",&node);
	lst->data = &node;
	printf("lst->data %p\n",lst->data);
}

int list_delete(list* lst)
{
	// get the element at the head
	printf("lst->data %p\n",lst->data);
	int value = lst->data->value;
	printf("value %d\n",value);

	linked_list tmp;
	tmp = *(lst->data->next);
	printf("tmp %d\n",tmp.value);

	// delete the old node
	// todo: check for memory leak
	//lst->data->next = NULL;
	lst->size--;

	// make the next element of head as the new head
	lst->data = &tmp;
	
	// return the deleted element
	return value;	
}

#include <stdio.h>
#include "queue.c"

int main()
{
	list new_list;
	printf("before seg fault\n");
	list_insert(&new_list,3);
	printf("after insert: %d\n", new_list.data->value);
	list_insert(&new_list,4);
	printf("after insert\n");
	list_insert(&new_list,5);
	printf("after insert\n");

	for (int i = 0;i < new_list.size;i++)
	{
		int n = list_delete(&new_list);
		printf("list[%d]: %d\n", i, n);
	}

	return 0;
}
The output printed as:
before seg fault
lst->data 0x400550
node 0x7fff75dbd3c0
lst->data 0x7fff75dbd3c0
after insert: 3
lst->data 0x7fff75dbd3c0
node 0x7fff75dbd3c0
lst->data 0x7fff75dbd3c0
after insert
lst->data 0x7fff75dbd3c0
node 0x7fff75dbd3c0
lst->data 0x7fff75dbd3c0
after insert
lst->data 0x7fff75dbd3c0
value 1977340928
Segmentation fault

I don't understand why every time, the list_insert method is entered and new linked_list node is created the address of the node is shown to be the same.

I need some help please.

Thanks.

Recommended Answers

All 4 Replies

line 23 and 29: You can't add an node that's allocated on the stack to the linked list. Reason: when the function returns the memory address of that node is destroyed and becomes invalid. To fix this you need to call malloc() to allocate memory for the node from the heap so that it doesn't get destroyed then the function returns.

// line 23 -- make node a pointer
linked_list* node = malloc( sizeof(linked_list));
node->next = NULL;
//
// line 29
lst->data = node; // removed the & address operator

You are going to have other problems with that insert function because all the code above does is toss the current value of lst->data into the bit bucket and replaces it with a newly allocated node. You will NOT get a linked list by doing that, but you WILL get lots of memory leaks. At line 29 you need to add the newly allocated node either to the beginning of the linked list chain or to the tail -- your choice.

If you want to add to the beginning of the list, then do this

node->next = lst->data;
lst->data = node;

If you want to add the newly allocated node to the tail of the linked list then you have to first find the tail (last node) and add the newly allocated node to it.

I don't understand why every time, the list_insert method is entered and new linked_list node is created the address of the node is shown to be the same.

I need some help please.

Thanks.

A simple answer is that the node you created is destroyed each time you exit list_insert. It happens because node is local to list_insert, and exists only in the local scope. The local scope variables live on the stack. The stack has in important property: upon a function exit it looks exactly as it was at the function call time; anything local to a function is gone and forgotten. When you call the it again, its local variables are created as if nothing happened before, and since you call it in the same context, another node is (temporarily) allocated at the same place, and destroyed upon exit, and nothing is new under the sun.

To escape this sansara, learn the difference between

void list_insert(list* lst, int x)
{
	linked_list node;

and

void list_insert(list* lst, int x)
{
	linked_list * node = new linked_list();
new

Awww it was a C forum not C++
I shall be dragged in the slime and the mud.

commented: Christ I know you can't hear me / But I only did what you wanted me to / Christ I'd sell out the nation / For I have been saddled with the murder of you / I have been spattered with innocent blood / I shall be dragged through the slime and the mud. +5

Okay. I got it now. Thanks.

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.