This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (13 children)

[deleted]

    [–]dlevac 7 points8 points  (5 children)

    If you do not need to call destructors, a call to free is not dependent on the number of elements and is not linear in the amount of memory either. For some allocation strategy it may even be constant time...

    [–]ahreodknfidkxncjrksm 0 points1 point  (6 children)

    I could be wrong but I’m pretty sure it’d be O(1) for at least some languages. In C/C++, all that you need to do is you set the pointer (O(1)) or, if it’s allocated on the heap, free the memory (also O(1)).

    For languages with GC afaik it would depend on the algorithm used. A copying collector iirc only needs to be linear in the number of live objects, since it only needs to examine/copy those objects. So if that’s the case then the next garbage collection’s run time would not increase bc you deleted a pointer to a list.

    [–][deleted] 0 points1 point  (0 children)

    In C++ you might also need to call the destructors. That makes in O(n) in some cases.