Bad Pointer Doubly Link List

In the tutoiral im using it has ask for me to create a doubly link list however I am geetting a bad pointer runtime error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <string>
#include <iostream>

using namespace std;

typedef string DataT;

struct link;

struct link{
	struct link *next;
	struct link *prev;
	DataT data;
};

struct list{
	struct link *head;
};

typedef struct link LinkT;
typedef struct list StrLLT;
typedef struct link *Iter;

StrLLT *Cons(void){
	StrLLT *tempList;

	tempList = (StrLLT*)malloc(sizeof(StrLLT));
	tempList->head = (LinkT*)malloc(sizeof(LinkT));
	tempList->head->next = NULL;
	tempList->head->prev = NULL;
	tempList->head->data = "sr"; //Offending Line of Code
	return tempList;
};
The problem is you are allocating LinkT object using malloc. This is wrong as LinkT contains member of type std::string, which will not be properly constructed because malloc only allocates raw memory, it doesn’t create objects. Use 'new' instead.
Topic archived. No new replies allowed.