0

This is my first time asking something on this site, so if I breach any sort of etiquette, let me know.

I am really new to linked lists and thought that implementing them using the list datatype would be pretty straightforward, but it seems I'm trying to do something weird with them.

I have created a list called "eventList" into which I want to put a series of structs called "entry", and then I wish to put several "eventList"s into an array called "processList".

My issue is that the bit of code

processList[pid].push_front(newEntry);

seems to give me an error. Is there something obviously wrong with what I'm doing?

Here is the code:

#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <array>

using namespace std;

struct entry {
    int eTime;
    string eType;
    int pid;
};

int main(){
ifstream infile;
string comm;
int num;

entry newEntry;
list<entry> eventList;
array<list<entry>, 1024> processList;


infile.open("input00.txt");

if(infile.is_open()){
    while(!infile.eof()){
        int pid = -1;
        string eTy;
        int eTi;
        infile >> eTy;
        infile >> eTi;

        if(eTy == "NEW"){
            pid++;
            cout << "DB: Process " << pid << endl;
        }
        else{
            newEntry.pid = pid;
            newEntry.eType = eTy;
            cout << "Type: " << newEntry.eType << " | ";
            newEntry.eTime = eTi;
            cout << "Time: " << newEntry.eTime << endl;
            processList[pid].push_front(newEntry);
            //processList[pid] = eventList;  <- realized that that wouldn't work fairly quickly
        }
    }
}

else {
    cout << "Sorry, that file doesn't work. :[" <<endl;
}

cout << "Original Order:" << endl;

list<entry>::iterator p = eventList.begin();

while(p != eventList.end()){
    cout << p->eType << " For " << p->eTime << "\n";    //This comes from http://www.cplusplus.com/forum/beginner/3396/
    p++;
}

//cout << "Ordered By Time:\n";

//eventList.sort(); <- commented out, because I can't get it to work. :[

system ("PAUSE");
return 0;
}

I really love that this resource is available, and I hope that I can follow the logic of any answers that come this way. Apologies for the noobishness, and thanks for looking!

  • 1
    Hello, and welcome to the site. What is the error that you are getting? I believe you're missing some information in your question - I'm not actually getting what is the question itself, actually. – Natan Streppel Jun 20 '14 at 17:48
  • Sorry, I just edited the question, and more specifically my program is exiting "with return value 3221225477", which doesn't mean much to me, but I think I did see the word segfault somewhere. My assumption is that I am trying to put something into a memory address that doesn't want anything going into it. – BornYesterday Jun 20 '14 at 17:52
  • Time to fire up a debugger and step through your code. – OldProgrammer Jun 20 '14 at 17:54
  • 2
    My guess is that the first time through you are attempting to add an entry to your list with pid = -1, thus attempting to reference array[-1]. But that's a guess. Whichever debugger you use will tell you for sure. – Aumnayan Jun 20 '14 at 17:58
  • OldProgrammer - I have tried that, but I'm terribly experienced at debugging, and when I attempt to go through it, I just end up at the bit where I attempt to add the list to the array. I am fairly confident that this is where the issue is, but I am not experienced enough to know where I am going wrong. – BornYesterday Jun 20 '14 at 17:58
  • 1
    You may as well [fix this: `while(!infile.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) while you're in there. – WhozCraig Jun 20 '14 at 18:02
  • WhozCraig - Thanks, I didn't know that it worked that way. I'll try and find a more suitable condition. – BornYesterday Jun 20 '14 at 18:04
  • Note that you currently have only one `entry` and one `list`, i.e. the ones you declared on the stack. You probably want to be able to create multiple entries, though, and multiple lists as well. Read up on dynamic allocation using `new`. – Caleb Jun 20 '14 at 18:05

3 Answers3

1

You are initializing pid inside the while loop. So no matter what you do, you will be referencing array[-1]. Talk to you're debugger, it will prove me right.

Aumnayan
  • 671
  • 3
  • 12
0

If eTy != "NEW" then pid remains equal to -1.

So,

processList[pid].push_front(newEntry);

will crash because the subscript is out of bounds ( -1 )

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • Okay, seriously, you guys are all awesome. Thanks a lot, I guess I was assuming that the pid++ would increment it to 0 before trying to add to the list. I imagine I'll be on here a lot more, but for now, thanks so much for the help, everyone! – BornYesterday Jun 20 '14 at 18:02
0

Despite how awesome linked lists are, don't use list (or std::list without the "using namespace std" bit). Use vector (std::vector).

std::list provides the same functionality as std::vector except:

  • list cannot be randomly accessed with the [] operator like vector
  • list being a double linked list it uses about 4 times the memory as vector for small structures
  • list is slower to access even in a sequential way than vector. This is due to vector being easier to cache because it's localized.
  • list is generally being slower than vector in every other aspect too.

The only "advantage" of list that inserting a new element does not invalidate the iterators. But vector indexes are more reliable than iterators any day, so you should never use iterators unless you absolutely need to.

and push_front builds a list in reverse order, you should usually use push_back.

Evan Dark
  • 1,311
  • 7
  • 7
  • I am actually more used to vectors, but my professor was very adamant about using linked lists for some reason, so I figured it would be a good idea to at least learn how to use them. Aside from that, though, I definitely agree with you. – BornYesterday Jun 20 '14 at 18:20
  • Oh, and yeah, I just noticed that the output was going backward, I appreciate the heads-up! – BornYesterday Jun 20 '14 at 18:20
  • Well maybe what he meant by using linked list, was writing the linked list on your own. Using the built in linked list isn't that much different from using any other container. – Evan Dark Jun 24 '14 at 11:30