Skip to main content
gamedev.net
🔒 Locked

Delete an array from a pointer

Started by BattleMetalChris 2 replies 1k views
Original Post
BattleMetalChris
BattleMetalChris
[font="Helv"][font="Helv"]I've got a function which creates and builds a dynamic array, then returns a pointer to it. Obviously this is going to need to be deleted somewhere down the line, but by the time it's no longer needed it's already been passed into and returned from two functions and so am I correct in thinking delete[] won't work, as by this point it's just seen as an ordinary pointer, not an array?



If this is the case, what's the best way of deleting the memory? I know the size of the array, so I could loop through it and just call delete on every item. Is there a smart-pointer that can be used with arrays which will delete it for me?

Holding a smart pointer to a std::vector won't really work due to the way the array is created in the first place. Unless there's a way of creating a vector quickly from a large array without having to loop through the whole array adding each element individually.

[/font][/font]
SiCrane
SiCrane
delete [] can be used on pointers. std::vector objects can be created from existing arrays using the input iterator constructor. Pass the address of the first element in the array and a pointer to one past the end.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.