Creating A List - issues

Hi all, new to here, so I hope I post things right.

I am currently going through some work, in which I have to manually create a list, rather than using the in-built function. This list takes the form of DataStream, in which each node holds a DataPacket.

I have an overloaded constructor in DataStream, which takes a .txt file as a parameter etc. The code is below.

My issue is, is that when I do a toString() to see the statistical info on the DataStream I've created, I'm getting different values. The first time I call it, it is from within the overloaded constructor itself, and the second time is from main().

Can anyone shed some light on the situation?

Here is the overloaded constructor for DataStream:
DataStream::DataStream(string inputFileName)
{
    // LOCAL VARIABLES
    char filename[255] ;
    char line[255] ;
    int i ;

    // CHANGE FILENAME (STRING TO CHARACTER ARRAY)
    strcpy (filename, inputFileName.c_str()) ;

    // OPEN FILENAME FOR READING
    ifstream ifile(filename);

    // CREATE A NEW DATASTREAM
    DataStream newDS ;

    // WHILE FILE IS OPEN AND READABLE
    if (ifile.is_open())
    {
        while (!ifile.eof())
        {
            // CREATE A POINTER TO A DATAPACKET
            DataPacket * newPacket = new DataPacket ;

            // TAKE 5 ELEMENTS
            for( i = 0 ; i < 5 ; i++)
            {
                switch(i)
                {
                    // FIRST ELEMENT IS Source, ADD TO PACKET
                    case 0: ifile.getline(line, 255, ':') ;
                            newPacket->setSorc(atoi(line)) ;
                            break ;
                    // SECOND ELEMENT IS Destination, ADD TO PACKET
                    case 1: ifile.getline(line, 255, ':') ;
                            newPacket->setDest(atoi(line)) ;
                            break ;
                    // THIRD ELEMENT IS Type, ADD TO PACKET
                    case 2: ifile.getline(line, 255, ':') ;
                            newPacket->setType(atoi(line)) ;
                            break ;
                    // FOURTH ELEMENT IS Port, ADD TO PACKET
                    case 3: ifile.getline(line, 255, ':') ;
                            newPacket->setPort(atoi(line)) ;
                            break ;
                    // FINAL ELEMENT IS Data, ADD TO PACKET
                    case 4: ifile.getline(line, 255) ;
                            newPacket->setData(line);
                            break;
                }
            }

            // PUSH PACKET ONTO LIST
            newDS.push(newPacket) ;
            newPacket->toString() ;
        }

        cout << "\n" ;
        newDS.toString() ;
    }
    else
    {
        cout << "File cannot be opened." << endl ;
    }

}


My main() is:
int main() {
    DataStream * forwardStream1 = new DataStream("data.txt");
    forwardStream1->toString() ;
}


And, in case your curious as to the output from the console, its:
DataStream :: Top [0x671348] Btm [0x69e1a0] Size [30]
DataStream :: Top [0x671088] Btm [0x5c3a433b] Size [1735357008]


I should finally note that the layout of main() cannot change, in line with the work I'm doing. I appreciate I've just thrown code at you, but any thoughts/solutions are highly welcome!

IE

EDIT: Through some more fiddling, I've found that this does actually access the same list, but starts at the 5th node, rather than the 1st. Why the size is such an obscure number I do not know.
Last edited on
[code] "Please use code tags" [/code]
1
2
//DataStream * forwardStream1 = new DataStream("data.txt"); //why dynamic allocation?
DataStream forwardStream1("data.txt");


1
2
3
4
5
6
DataStream::DataStream(string inputFileName){ //It could be const string &
  ifstream ifile(inputFilename.c_str());
  DataStream newDS ; //local variable
  //code that affect the local variable
  newDS.toString() ; //display the content of the local variable
} //local variable dies 
newDS should not exists. You need to modify the object that make the call (*this)
so you need to call this->push(newPacket); //this could be omitted push(newPacket);

Check out for memory leaks.
Much appreciated. I wasn't aware I was essentially making a local list. As for the dynamic allocation, as I say, I was given a main() and not allowed to edit it.

The issue surrounding the list beginning at the 5th node, I'm still unsure about, and am currently trawling through my push() and pop() routines to hopefully locate the memory leak.

EDIT: This problem has now been solved. Help much appreciated ^^
Last edited on
Topic archived. No new replies allowed.