473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Instrusive List Container using the Base class as the node

9
Hi,

I am trying to write an intrusive List template class that accepts any type that is derived from a class called Link. I've derived a class called Student from Link to experiment. When I use the Link and Student class without List they work very well but when I used them through the List class the new operator doesn't throw but seems to be return 0.

I've pasted the full code here and the program output. The commented part of main works very well. The issue I think is specifically in the List<T>::add(T t) function where new is being called. Does anyone know the reason?

Many Thanks,
pnayak

I am using gcc 3.4.6 on FreeBSD .

Output from the Program
p->next was not allocated
p->next was not allocated
Id:10 Name:Jack

Program Source Code
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. struct Link
  4. {
  5.     Link():next(0) {};
  6.     Link(const Link& l):next(l.next) {};
  7.     virtual void print() const {std::cout<<"parent\n";} ;
  8.     Link* next;
  9. };
  10.  
  11. class Student: public Link
  12. {
  13.     public:
  14.         Student(int id, char * name): Link(), id_(id), name_(name) {}
  15.         Student(const Student& s): Link(s), id_(s.id_), name_(s.name_) {}
  16.         virtual void print() const { std::cout<<"Id:"<<id_<<" Name:"<<name_<<'\n'; }
  17.  
  18.     private:
  19.         int id_;
  20.         char* name_;
  21. };
  22.  
  23. template<class T> class List
  24. {
  25.     public:
  26.         [indent]List():head(0) {};
  27.         void print() { for (Link* p = head; p; p = p->next) p->print(); }
  28.         void add(const T& t);
  29.  
  30.     private:
  31.         Link* head;
  32. };
  33.  
  34.  
  35. template<class T> void List<T>::add(const T& t)
  36. {
  37.     Link* p;
  38.  
  39.     if (!head)
  40.     {
  41.         head = new T(t);
  42.         if (!head)
  43.             std::cout<<"head was not allocated\n";
  44.         return;
  45.     }
  46.  
  47.     for ( p = head; p->next; p = p->next)
  48.  
  49.     p->next = new T(t);
  50.  
  51.     if (!p->next)
  52.         std::cout<<"p->next was not allocated\n";
  53. }
  54.  
  55. int main()
  56. {
  57.     List<Student> students;
  58.  
  59.     students.add(Student(10, "Jack"));
  60.     students.add(Student(12, "Jill"));
  61.     students.add(Student(14, "Bill"));
  62.     students.print();
  63.  
  64.     //Link* head = NULL;
  65.     //Link* t = NULL;
  66.  
  67.     //Student s(25, "George");
  68.  
  69.     //head = new Student(10, "Purush");
  70.     //head->next = new Student(12, "Jack");
  71.     //
  72.     //for (t = head; t->next; t = t->next);
  73.  
  74.     //t->next = new Student(20, "Bill");
  75.     //t = t->next;
  76.     //t->next = new Student(s);
  77.  
  78.     //for (t = head; t; t = t->next)
  79.     //    t->print();
  80.  
  81. }
Apr 22 '08 #1
9 2519
Savage
1,764 Expert 1GB
I believe you are missing a semicolon here ;)

Expand|Select|Wrap|Line Numbers
  1. template<class T> void List<T>::add(const T& t)
  2. {
  3.  
  4.     Link* p;
  5.  
  6.     if (!head)
  7.     {
  8.  
  9.         head = new T(t);
  10.         if (!head)
  11.         std::cout<<"head was not allocated\n";
  12.         return;
  13.  
  14.     }
  15.  
  16.     for ( p = head; p->next; p = p->next)//<-----
  17.  
  18.     p->next = new T(t);
  19.  
  20.     if (!p->next)
  21.     std::cout<<"p->next was not allocated\n";
  22.  
  23. }
Apr 22 '08 #2
pnayak
9
OH MY HOLY GOD. You are right.

I wasted an entire evening on this. I pretty much tore apart the c++ template vocabulary to understand this "obscure" behavior. Turned out to be a semi colon !!!!.

Thank you so much.
Purush
Apr 23 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
I don't think you object model is correct. That is, a Student is not a Link. A Student can be a member of a Link.

So there should be a pointer to a Student in the Link.

Also, the Link is not a list. It's just a link. I expect there is a missing class LinkedList that has Link pointers to the beginning, end and current position in the list.

Doing it this way, you create a LinkedList, have a member function, like AddToEnd, take a Student* argument and it is there you create the Link and install the Student* in the Link and add the Link as the new end of the LinkedList.

Once you get this working, you convert your code to a template so the Student* becomes a T*. That's how you get multiple types in your Link rather than incorrectly using inheritance.
Apr 23 '08 #4
pnayak
9
That's a nice point, however, what I am trying to do here is to develop an intrusive list and not a non-intrusive list, which is what you are describing. One way of developing an intrusive list it to use the Link object as a member of the application class. The other option is to make the Link a common base class.

I am trying to ascertain whether intrusive lists have better performance as opposed to non-intrusive lists since the Link class does not need to have an additional pointer to an application object (the Student in this case) since the application object has the link embedded in it. However the drawback being that there is no non-obscure way of using a list of built-in types. On the other hand, a non-intrusive list is really somewhere down below an intrusive list. Because to implement any list structure there needs to be something in common among the list items based on which we can traverse the list.

So now, I create a new list based on the previous list. And see how much performance slow down is observed when traversing a large list. And see if it worth forcing users (of the class) to force to derive from a common base class or just let them use a non-intrusive list.

If you know anything more on the topic please do let me know.

Purush
Apr 24 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
One way of developing an intrusive list it to use the Link object as a member of the application class. The other option is to make the Link a common base class.
The List object as a member of the Student is identical to private inheritance. That is, you inherit the List but not the List interface.

Your real question is whether the Student can vary indendently from List. Like if the Student changes do you have to traverse your entire list to find the thing to change or does the change to Student occur independently.

In the first case you can use a member List but in the second you can't. Instead you will need a List with nodes that contain pointers (or handles) to Student.

That means this is larger than a simple question of performance. You are really comparing two different object models and they are not equivalent so your comparison results won't make sense.
Apr 25 '08 #6
pnayak
9
I completely disagree.

The choice of intrusive and non-intrusive lists with regards to performance and flexibility is very important in Container design and form a fairly large part of any literature that deals with the topic. Their comparison is very important and makes a lot of sense.
Apr 25 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
Maybe you should read this.

http://www.codefarms.com/publication.../Intrusive.pdf.
Apr 25 '08 #8
pnayak
9
That's the same paper from which I was testing the conclusion. The entire paper is about comparing the the two types on performance and flexibility. But according to you they are not equivalent and comparing them makes no sense!

You should also read The C++ Programming Language by Stroustrup [16.2]. That will give you a better understanding of the concepts involved.

Here are a few of lines from the papers abstract.

This article compares two styles of building data structures and data structure libraries in C++: (a) Intrusive data structures formed by pointers stored inside the application objects, (b) Containers where auxiliary objects form the required data structure, and only point to the application objects without adding any pointers or other data to them.

Each style works better in different situations, and the first half of the article discusses the impact of this choice on program performance, code maintainability, ease of use and data integrity.
Apr 25 '08 #9
weaknessforcats
9,208 Expert Mod 8TB
Each style works better in different situations, and the first half of the article discusses the impact of this choice on program performance, code maintainability, ease of use and data integrity.
This is what I meant by saying there are two object models. Intrusive containers work poorly where the objects need to change independently of the container.

Maybe I just wasn't communicating clearly.
Apr 26 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: kazio | last post by:
Hello, So, I need to have double linked, circular list (last element point to the first one and first one points to the last one). I thought maybe I could use list container from STL, but...
5
by: Kenneth | last post by:
<list> seems to be a powerful structure to store the related nodes in memory for fast operations, but the examples I found are all related to primitive type storage. I'm doing a project on C++...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: silverburgh.meryl | last post by:
in STL, why vector has an API to return the n-th element, but list does not have such API? http://www.sgi.com/tech/stl/Vector.html http://www.sgi.com/tech/stl/List.html Thank you.
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
9
by: Naren | last post by:
Hi, why cant a list<derived*be implicitly castable to list<base*>? Any alternatives other than global operators? thanks in advance, Naren.
3
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
4
by: Thomas Grund | last post by:
Hi, In the following code I would like that the class Node is only be used by the class Database. So the idea was to make the interface protected and use the friend definition. The code does not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.