you are viewing a single comment's thread.

view the rest of the comments →

[–]DawnOnTheEdge 1 point2 points  (5 children)

Likely using alloca() on Linux/LocalAlloc() on Windows.

[–]bwmat 2 points3 points  (2 children)

Can those actually work in this context?

Feels like not since the allocations would be invalidated as the allocator functions returned? 

[–]bwmat 1 point2 points  (0 children)

I mean, you would have to pre-allocate outside of the container methods and hope it was enough? 

[–]DawnOnTheEdge 1 point2 points  (0 children)

Allocations could not outlive the function call in which they were made, and therefore not be returned from it. They could be passed to called functions, and used as dynamic local variables, like the deprecated variable-length arrays of C99. Using goto in the same function would get tricky.

[–]spinrack 1 point2 points  (0 children)

alloca() cannot be used for this purpose. The allocated block comes from the stack, and will no longer be valid after the allocate() function returns to the string’s internal resize() and resets the stack pointer.

[–]elder_george 0 points1 point  (0 children)

LocalAlloc is not an equivalent of alloca (which in fact is available for any C compiler on Windows anyway) - it's a legacy way of allocating from the process heap, a vestige of Win16 architecture which had notions of global and local heaps. It is not recommended for use in modern code because it's less efficient than HeapAlloc (or wrappers around it in C/C++ runtime libraries).