次の関数で浮動小数点数の配列を返そうとしています:
static GLfloat *LoadFile(const char *filePath) {
std::ifstream f;
f.open(filePath, std::ios::in | std::ios::binary);
if(!f.is_open()){
throw std::runtime_error(std::string("Failed to open file: ") + filePath);
}
std::stringstream buffer;
buffer << f.rdbuf();
std::string s = buffer.str();
std::string delimiter = ",";
size_t pos = 0;
std::string token;
pos = s.find(delimiter);
token = s.substr(0, pos);
int numberOfItems = ::atof(token.c_str());
s.erase(0, pos + delimiter.length());
pos = s.find(delimiter);
token = s.substr(0, pos);
int componentsPerItem = ::atof(token.c_str());
s.erase(0, pos + delimiter.length());
int dataLength = numberOfItems * componentsPerItem;
GLfloat *data = new GLfloat[dataLength + 2];
data[0] = numberOfItems;
data[1] = componentsPerItem;
int counter = 2;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
GLfloat num = ::atof(token.c_str());
if (counter > dataLength + 1)
throw std::runtime_error(std::string("Error in vertex data file: ") + filePath);
data[counter] = num;
s.erase(0, pos + delimiter.length());
counter++;
}
return data;
}
しかし、戻り値と等しい変数を設定すると:
GLfloat *data = LoadData();
data は NULL に等しいです。new 演算子を使用したため、関数のスコープを離れた後でも値が保持されるという印象を受けました。私は何を間違っていますか?