deleting a dynamic array of structs

Started by
1 comment, last by IImmortal 20 years ago
Hi all i have a small problem. I am using a struct to handel some data. the struct looks like this: struct STAD { int Niveau; LPSTR Naam; int X; int Y; }; now i make a dynamic array of structs like this: STAD* StedenNogTeDoen; and after that i load the data from a file into the struct''s in the file is how manny structs i should make and create it like this: StedenNogTeDoen = new STAD[AantalS-1]; after that i go through a loop that loads the data and sets every string to 50chars like this: StedenNogTeDoen[x].Naam = new char[50]; now that all works. but if i want to delete the array of structs i get memory check errors or access violations. delete []StedenNogTeDoen; does anyone know what i am doing wrong?
Advertisement
If you det all names in the structs to the length of 50 you don''t need to dynamically allocate them. Just put char naam[50];
into the struct. And it should work.

But if you wan''t to dynamically allocate the naam , you also have to free the memory.
for (i < AantalS-1)
delete[] StedenNogTeDoen.naam;



EasyGL - easy to use graphics library.
EasyGL - easy to use graphics library.
You''re probably doing something with an element that''s outside the array''s bounds. What does the loop look like?

Anyway, meet the C++ programmers'' best friends: std::string and std::vector ;-)

This topic is closed to new replies.

Advertisement