Note that there are some explanatory texts on larger screens.

plurals
  1. POIncorrect allocation of memory with an array of structures
    primarykey
    data
    text
    <p>I have posted this problem before but I cannot seem to find a solution. So I am posting again with refined description in hopes of solving this problem. Your help is much appreciated.</p> <p>What I would like to do is read in a row from a text file. This text file contains 3 types of rows: </p> <ol> <li>A row containing the word "start" indicating the start of a particular test case result</li> <li>Rows containing the word PASS or FAIL or PWU or FWU and accompanying data</li> <li>A row containing the word "stop" indicating the end of a test case result</li> </ol> <p>The trouble I am having is with <code>dataRowPointer</code>. <code>dataRowPointer</code> is an array of struct. It's pointer was declared in the main function, and it was passed into the following function by reference.</p> <p>When I call <code>*dataRowPointer = new DataRow[MAX_NUMBER_DATA_ROWS_PER_TEST_CASE_SET];</code>, I can see in the debugger that the first array element, dataRowPointer[0] was properly allocated in memory. However the second, third and all other elements were not properly allocated because the debugger displays them as 0xcccccccc in memory.</p> <p>This is affecting my other function call <code>getDataRow( line, loc, tcsPointer, dataRowPointer );</code> which contains lines similar to <code>(*dataRowPointer)[dataRowIndex].tcSetNum = testCaseSetID;</code> .</p> <pre><code>void getRow( std::istream&amp; 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 &gt;&gt; word ) { if ( word == "PASS" || word == "PWU" || word == "FWU" || word == "FAIL" ) { startStop = false; break; } else if( word == "start" ) { dataRowIndex = 0; *dataRowPointer = new DataRow[MAX_NUMBER_DATA_ROWS_PER_TEST_CASE_SET]; initializeDataRowPointer( dataRowPointer ); testCaseSetID = captureTestCaseSetNumber( line, tcsPointer ); getHeaderRow( myfile, loc ); startStop = true; break; } else if ( word == "stop" ) { tcsNumber++; *dataRowPointer = NULL; startStop = true; break; } else { startStop = false; } } if( !startStop ) { (*dataRowPointer)[dataRowIndex].tcSetNum = testCaseSetID; getDataRow( line, loc, tcsPointer, dataRowPointer ); dataRowIndex++; } } </code></pre> <p>Any help would be greatly appreciated.</p> <p>Note: I understand my code is fragile ( what if text document doesn't have start, or stop or etc), and it contains bad code (use of global variables, etc). But please I just want to solve this problem first before moving on. And please try to ignore the <code>*dataRowPointer = NULL;</code> within the <code>else if</code> statement, I will change this to something much more useful once this bug is fixed.</p> <p><strong>EDIT</strong> As requested the code for struct DataRow and the code for function InitializeDataRowPointer:</p> <p>DataRow:</p> <pre><code>struct DataRow { std::string tcSetNum; std::string class_PassFail; std::string rfPort; float temp; float margin; }; </code></pre> <p>InitializeDataRowPointer:</p> <pre><code>void initializeDataRowPointer( DataRow** dataRowPointer ) { DataRow newDataRow = {"a", "a", "a", 0.0, 0.0 }; for ( int i = 0; i &lt; MAX_NUMBER_DATA_ROWS_PER_TEST_CASE_SET; i++ ) { (*dataRowPointer)[i] = newDataRow; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload