Archived

This topic is now archived and is closed to further replies.

deleting a dynamic array of structs

This topic is 5007 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 ;-)

Share this post


Link to post
Share on other sites