I was just writing a small program today converting csv files to xml, and I noticed something weird was happening. I had an array of doubles that was being iniatialized near the end of the program using an array of strings, atof(), and a for loop. When I commented out the initialization and left only the declaration, I got some runtime errors. Then when I commented out the declaration, the program ran fine again. I've never had this happen to me before, so I made a short test program. All it was was an integer declaration, and it ran fine. Any idea what's causing this?
C++ -- Undinitialized Variables
Started by Captacha, Oct 06 2012 06:10 PM
9 replies to this topic
Sponsor:
#4 Members - Reputation: 141
Posted 06 October 2012 - 07:48 PM
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int CELLS_IN_GRID = 81;
int main()
{
FILE *file; /* Pointer to File */
double cellValue[CELLS_IN_GRID]; /* Array of cellValues for each Cell in Grid */
string csvValue[CELLS_IN_GRID]; /* Array of cellValues as Strings */
char currentChar; /* Current Character in File Stream */
int cellPosition = 0;
string filename;
cin >> filename;
file = fopen(filename.c_str(), "r");
while(currentChar != EOF)
{
currentChar = fgetc(file);
if(currentChar != ',' && currentChar != '%')
{
csvValue[cellPosition] += currentChar;
}
if(currentChar == ',')
{
cellPosition++;
}
}
fclose(file);
/*for(int i=0;i<CELLS_IN_GRID;i++)
{
//cellValue[i] = atof(csvValue[i].c_str());
//cout << csvValue[i] << '%' << " " << i << endl;
}*/
return 0;
}
Here's the code. The line that causes it to break(if commented out) is the first line in the for loop.
Edited by Captacha, 06 October 2012 - 08:14 PM.
#7 Members - Reputation: 2142
Posted 06 October 2012 - 11:45 PM
Also, initialise currentChar to a value != EOF. That could potentially skip your reading loop.
Check if cellPosition gets >= CELLS_IN_GRID. If it does you're writing in memory that is not yours. Once that happens all kind of weird things can happen (hence undefined).
The lines inside the for loop look ok.
Check if cellPosition gets >= CELLS_IN_GRID. If it does you're writing in memory that is not yours. Once that happens all kind of weird things can happen (hence undefined).
The lines inside the for loop look ok.
Fruny: Ftagn! Ia! Ia! std::time_put_byname<wchar_t>! Mglui naflftagn std::codecvt<char,char,mbstate_t> eY'ha-nthlei!
#8 Members - Reputation: 6184
Posted 07 October 2012 - 06:52 AM
This is how I typically make a loop for reading from a file:
In the case of reading character by character (which perhaps you should reconsider), it looks like this:
while (reading statement succeeds) {
// ...
}In the case of reading character by character (which perhaps you should reconsider), it looks like this:
while ( (currentChar = fgetc(file)) != EOF) {
// ...
}
#10 Members - Reputation: 6184
Posted 07 October 2012 - 12:16 PM
Well, you also had fragile code that couldn't detect that mistake in the file. Proper error handling is tedious and hard to learn, but it's very important, so you should take this opportunity to practice by making your code detect anything that could be wrong with the file.






