I am learning c/c++ by myself, when making a program to read a file format from a game, I was able to get the data needed, but the problem is, it is only accessible while inside the loop that reads it. I am not able to use it anywhere else in the function much less outside it.
i know variables and etc “disappear” after the loop, but im using dynamic memory allocation using “new” method, its an array of structs. i cant initialize it beforehand cause the amount of elements is unknown without reading the file first.
i need this information for later use, so if it just lives while inside the loop, thats useless really…
heres my code
typedef struct {
uint32_t FbxHash;
string ShapeName;
float ShapeTransforms[18];
bool converted;
} ShapeEntity;
and the function that reads the file:
void CompositeMeshReader(FILE* MeshCompPtr) {
uint8_t NumberOfLods;
uint8_t NumberOfMeshes;
uint8_t ShadersPerMesh;
float GlobalTransforms[6];
int NumberOfShapes;
int CurrentLOD;
int w;
int y;
int i;
int j;
int k;
string fname;
string* fnameptr = &fname;
uint8_t z;
uint8_t NullProperty;
uint8_t UnknownByte;
bool stay = true;
fseek(MeshCompPtr, 5, SEEK_SET);
StringSkipper(fnameptr);
cout << "Lods Settings File: " << fname << endl;
fread(&NumberOfLods, 1, 1, MeshCompPtr);
fseek(MeshCompPtr, 3, SEEK_CUR);
cout << "Lods detected: " << int(NumberOfLods) << ", which one to extract?" << endl;
cout << "for lod 0, type 0, lod 1, type 1, etc" << endl;
cin >> i;
fname = "";
CurrentLOD = 0;
while ((CurrentLOD < NumberOfLods) && (stay == true)) {
fread(&NullProperty, 1, 1, MeshCompPtr);
fread(&ShadersPerMesh, 1, 1, MeshCompPtr);
fread(&NumberOfMeshes, 1, 1, MeshCompPtr);
for (j = 0; j < NumberOfMeshes; j++) {
StringSkipper(fnameptr);
if (i == CurrentLOD) {
cout << "Mesh " << j << " " << fname << endl << endl;
}
for (k = 0; k < ShadersPerMesh; k++) {
StringSkipper(fnameptr);
if (i == CurrentLOD) {
cout << "Shader " << k << " " << fname << endl << endl;
}
}
}
fread(GlobalTransforms, sizeof GlobalTransforms[0], 6, MeshCompPtr);
fseek(MeshCompPtr, 8, SEEK_CUR);
fread(&UnknownByte, 1, 1, MeshCompPtr);
fread(&NumberOfShapes, 4, 1, MeshCompPtr);
ShapeEntity* Shapes = new ShapeEntity[NumberOfShapes]; // this is what I need to keep alive
for (w = 0; w < NumberOfShapes; w++) {
fread(&Shapes[w].FbxHash, 4, 1, MeshCompPtr);
StringSkipper(fnameptr);
Shapes[w].ShapeName = fname;
fread(Shapes[w].ShapeTransforms, 4, 18, MeshCompPtr);
Shapes[w].converted = false;
}
fseek(MeshCompPtr, ShadersPerMesh * 24, SEEK_CUR);
if (i != CurrentLOD) {
cout << "Shapes destroyed" << endl;
delete[] Shapes;
}
else {
print(Shapes, ShadersPerMesh, NumberOfMeshes, NumberOfShapes, GlobalTransforms);
stay = false;
}
CurrentLOD++;
}
return;
}
im trying to keep Shapes alive even after returning from this function, but after leaving the while loop they just disappear, im just going nuts this seems to be impossible? without rewritting the whole thing from scratch
looked pretty much everywhere but still cant find a proper solution to this
V12Style is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
Either return shapes
(or pass it in as a reference) or use a global variable for it (which may hurt you later if you need multithreading or run into some issues with dynamically linked libraries). As your function is written, it just leaks the memory (allocates the memory but then never deletes it as you don’t pass the pointer to anywhere else).
Also, it’s probably better to use something like an std::vector
for this, as then you won’t run into the memory leaks. Your code is basically C code as is, not really C++.
1