Lists...

closed account (jvqpDjzh)
//sorry i am trying to create a list using a pointer to the next struct
//PROBLEMS:
//with dev-cpp:
//1. when i try to print the list the output has no sense
//2. if i use the malloc to allocate memory for the struct film it crashes
returning an high number
//il = (film*) malloc(sizeof(film));//that's what i had written with malloc notation...
//of course i had included the <cstdlib>


//with code-blocks
//1. when i try to print the list there's a third element of the list that shouldn't be there...maybe it's a problem with the print function -__-

//have a look to my code

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
creating lists...
*/

#include <iostream>
#include <string>
using namespace std;

struct film
{
    string title;
    string director;
    string producer;
    int duration;
    int year;

    film* next;
};

//another version for a film struct without the pointer as strings...
void insertInTheHead(film* &il, string title, string director, string producer, int duration, int year)
{
	film* structer = NULL;
	//structer = (film*) malloc( sizeof(film) );
	structer = new film;
	//INFO part
	structer->title = title;
	structer->director = director;
	structer->producer = producer;
	structer->duration = duration;
	structer->year = year;

	structer->next = il;//il has NULL
	il = structer;//il is a pointer to defined struct

	cout << endl;
}

void printList(film* il)
{
	while(il != NULL)
	{
		cout << "Title: " << il->title << endl;
		cout << "Director: " << il->director << endl;
		cout << "Producer: " << il->producer << endl;
		cout << "Duration (min): " <<il->duration << endl;
		cout << "Year: " << il->year << endl;

		cout << endl;

		il = il->next;
	}
}


int main()
{
	film* il = NULL;
	string title;
	string director;
	string producer;
	int duration;
	int year;

	il = new film;

	for (int i=0; i<2; i++)
	{
		cout << "Enter the title: ";
		cin >> title;
		cout << "Enter the director: ";
		cin >> director;
		cout << "Enter the producer: ";
		cin >> producer;
		cout << "Enter the duration: ";
		cin >> duration;
		cout << "Enter the year: ";
		cin >> year;

		insertInTheHead(il, title, director, producer, duration, year);
	}

	cout << endl;

	printList(il);

    cin.get();

    return 0;
}
Last edited on
Learn classes, then approach this topic again. Your code is really bad, because you'll get massive memory leaks - you don't have any convenient way to delete memory you've assigned, and you don't do that. Classes have constructors and destructors helping you with manual memory management.

Anyway, the bug lies on line 33 and 34. Read about assigning pointers. You probably think you're copying value of pt1 to pt2, but you don't do that.

Cheers!
closed account (jvqpDjzh)
I didn't do it with classes because i didn't want to do it with classes, and i didn't free nothing because it wasn't necessary... and i don't know what you mean with memory leaks with such a little program... i know classes have constructors and what?
closed account (jvqpDjzh)
ah? are you serious? read about assigning pointers? yes, cause couldn't I a assign a pointer to another pointer that are pointing to variables of the same type?lol, yes, i am copying the address of structer, which is pointing to the first element of struct, to il, and i can do it! if not, tell me a reason
Last edited on
closed account (D80DSL3A)
Your code all looks fine. I think that line 65 is the reason for the extra item. Just comment it out. Your insertInTheHead function takes care of all allocations.

You do have a memory leak in that the new-ed films are never deleted, but that shouldn't cause the program to not work right.

EDIT: A function for deleting a film lists nodes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void destroyList(film*& il)// post condition: il = NULL
{
        int N = 0;// film counter (a frill)

	while(il != NULL)
	{
                film* temp = il;
                il = il->next;
		delete temp;
                ++N;	
	}
        
        cout << "deleted " << N << " films from list." << endl;
}
Last edited on
The problem is line 65:
il = new film;

You create a (basically empty) film node, and everything else adds on to that.
So delete that line and you should be good to go.
closed account (jvqpDjzh)
long double main (764)

Yes, it works, but shouldn't we first allocate memory to the pointer il (of type film) in the main source?

//and if I use malloc to allocate the pointer il the program crashes, why?
il = (film*)malloc( sizeof(film));
//with this notation i am just allocatin memory for a struct
//shouldn't be there any problems, but I can't understand the reason of my crash...
Last edited on
closed account (jvqpDjzh)
Thank you to y'all! It s of course of the line 65, because I am allocating memory to another struct and when I try to create my "new" list (from zero), calling the insertInTheHead function, there is already an existing node, that I have just created with (il=new film;). And because I am creating my list inserting nodes in the head, when I print it, the first node I created (with "il = new film;") is printed after the second, because it becomes the last node of the list!! :)
Last edited on
Topic archived. No new replies allowed.