Error in assigning a value to an element in an array of struct

0

I get this error in visual studio when running the code: Unhandled exception at 0x0028457b in fileIO_experiment.exe: 0xC0000005: Access violation reading location 0x00000018

I am trying to allocate an array of structs inside one of my functions. The pointer to this array is declared in main, but passed on and used inside a function called getRow. The problem occurs when I try to assign a value to an element in the array of structs.

Here is my code :

In the main function:

int main()
{
.
.
.
DataRow* dataRowPointer = 0;
dataRowIndex = 0;

while ( myfile.good() )
{
        getRow( myfile, &loc, tcsPointer, dataRowPointer );
        globalLineCounter++;        
}
.
.

In the function getRow :

void getRow( std::istream& myfile, HeaderLocations* loc, TestCaseSet* tcsPointer, DataRow* dataRowPointer )
{
    std::string line;
    std::string word;    
    int currentLoc = 0;
    bool startStop = true;

    std::getline( myfile, line);        
    std::stringstream sline(line);

    while ( sline >> word )
    {
        if( word == "start" )
        {   
            dataRowIndex = -1;
            dataRowPointer = new DataRow[MAX_NUMBER_DATA_ROWS_PER_TEST_CASE_SET];              //Declaration of array of DataRow structs
            initializeDataRowPointer( dataRowPointer );          // Setting all values of previous array to default values

            testCaseSetID = captureTestCaseSetNumber( line, tcsPointer );
            getHeaderRow( myfile, loc );

            startStop = true;
            break;
        }
        else if ( word == "stop" )
        {   tcsNumber++;

            dataRowPointer = 0;
            startStop = true;
            break;
        }
        else
        {   startStop = false;  }
    }

    if( !startStop )
    {   dataRowIndex++;
        dataRowPointer[dataRowIndex].tcSetNum = testCaseSetID;      // <-- PROBLEM OCCCURS HERE
        getDataRow( line, loc, tcsPointer, dataRowPointer );    }
}

dataRowIndex is a global variable. This is only temporary until I fix this code. I apologize in advance if my code is more C than C++, I am still a newbie.

Help would be much appreciated. Thank You!

c++
asked on Stack Overflow Jun 10, 2013 by Splaty • edited Jun 10, 2013 by Splaty

2 Answers

0

This is a very fragile piece of code. If you have move than one "start", you leak and throw away memory. You hit stop after start, you leak your pointer - heck you always leak your pointer. You need to rethink how your create your memory for sure.

Your pointer is 0'd in stop, but you set a sentinel to catch that so it might be okay.

Here's the probable issue: If all the words coming in are neither stop nor start (or your compares to strings don't work correctly) and while(sline >> word) eventually returns false as it is out of data or something, you never allocate memory and then attempt to read from it in your !startStop if statement. This will cause a NULL exception.

answered on Stack Overflow Jun 10, 2013 by Michael Dorgan
0

dataRowPointer is a NULL pointer, the if statement is only entered if startStop is false. the only point in your code you initialize this pointer sets startStop to true and leaves the loop (break). it is not possible that you access a valid pointer inside your if.

answered on Stack Overflow Jun 10, 2013 by WoJo

User contributions licensed under CC BY-SA 3.0