I am writing a function that reads various properties, including a string, from a csv file and assigns it to a relevant element of a struct which happens to be in an array of similar structs.
Whenever I attempt to assign value to:
materialLookup[v-1].name
the program crashes.
MaterialL
is a struct with a string element called name that looks like this:
struct MaterialL {
string name;
double sigma;
double corLength;
double mu;
double muPrime;
double ep;
double epPrime;
};
I have checked that I am reading the string from the csv file correctly and in this case, it is "Drywall". The program always crashes before I am able to cout<<"hey"; on the next line. My only thought is that because the program doesn't know the size of the string before I assign it, it doesn't leave any memory for it. If so how can I rectify this?
unsigned int getMatLookUp(string filename)
{
int nLines = getNumOfLines(filename);
cout << nLines;
materialLookup = (MaterialL*)alignedMalloc(nLines * sizeof(MaterialL));
ifstream file(filename);
int v = 0;
string value;
if (file.is_open() && fileExists(filename))
{
//flush title line
for (int p = 0; p < 6; p++){ std::getline(file, value, ','); }std::getline(file, value);
v++;
//get all the materials
while (v < nLines -1)
{
std::getline(file, value, ',');
cout << value<<"\n\n";
materialLookup[v - 1].name = value;
cout << "hey";
std::getline(file, value, ',');
cout << value << "\n\n";
materialLookup[v - 1].sigma = stod(value);
std::getline(file, value, ',');
materialLookup[v - 1].corLength = stod(value);
std::getline(file, value, ',');
materialLookup[v - 1].mu = stod(value);
std::getline(file, value, ',');
materialLookup[v - 1].muPrime = stod(value);
std::getline(file, value, ',');
materialLookup[v - 1].ep = stod(value);
std::getline(file, value);
materialLookup[v - 1].epPrime = stod(value);
v++;
}
file.close();
}
else
{
cout << "Unable to open file when loading material lookup in function getMatLookUp press enter to continue\n";
cin.get();
}
return(0);
}
new, or better yetstd::vector?newin C++17 that add an alignment argument.newreturn properly aligned memory?std::vector. If there's a reason to usealignedMalloc(), he should explain why he's using it instead ofstd::vectorin the question.__STDCPP_DEFAULT_NEW_ALIGNMENT__that I don't see in N4527, probably time to update). Perhaps the OP is looking for overaligned memory, though. I really have no clue, and very little alignment experience anyway.