is there a way to keep initialized structs outside loop?

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

New contributor

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật