As VLAs are assigned memory on the stack, will they cause any problem if we inlined the function containing them? I think, because of the same behavior of alloca i.e. storing objects on the stack, compilers avoids inlining such functions(?).
Asked
Active
Viewed 186 times
4
-
What difference does it make if a function is inlined and needs to allocate on the stack? – A.S.H Feb 10 '17 at 22:11
-
@A.S.H http://stackoverflow.com/a/3410689/1669844 – user1669844 Feb 10 '17 at 22:16
-
1@lax that should have been considered as a compiler bug IMO. A compiler that inlines a function in a way that changes the *semantics* is not conforming. By the semantics each *alloca* should be released when function returns, and correct inlining must respect this semantic. – A.S.H Feb 10 '17 at 22:28
-
2@lax: Semantics of VLA is very different from semantics of `alloca`, so that example does not apply. Moreover, what you read about at that link is most likely a user error (like using `__forceinline` or such). – AnT stands with Russia Feb 10 '17 at 22:30
-
1If your concern is that the 'inline' function won't be inlined, but the alternative implementation using `alloca()` won't be inlined either, you're no better or worse off than you were before. If your compiler doesn't have bugs, and if you don't allocate such large VLAs that your stack is blown — and if it is still sensible to inline the function — then there's no obvious reason that it won't be inlined. People are very cautious about VLAs because there's no way of reporting 'out of memory'; your program crashes if there isn't enough space. The notational convenience is considerable, though. – Jonathan Leffler Feb 10 '17 at 23:04
2 Answers
4
Whereas (the nonstandard) alloca
function yields an object whose lifetime is the calling function, a VLA's lifetime is the block in which it is declared. However inlining is not relevant to either of them. A function call whose body happens to get inlined is still a function call, and objects it obtains by alloca
cease to exist when it semantically returns, not when the cpu executes a ret instruction or equivalent.

R.. GitHub STOP HELPING ICE
- 208,859
- 35
- 376
- 711
-3
I would have thought that any memory assigned to a stack for a VLA (struct you are using) would somehow be a different memory area to that allocated for an inline function. Besides, when the inline code is compiled or interpreted, that would be a job for the processor/parser and not for the stack. So in short, I would say, no.
-
2Your comments are puzzling — I'm not sure I understand why you think that the memory for VLAs would be anywhere other than the stack. Do you know of an implementation that allocates VLAs somewhere else? And I have difficulty 'parsing' or 'compiling' your sentence that starts 'Besides'.. – Jonathan Leffler Feb 10 '17 at 23:08
-
Sorry perhap I wasn't very clear and I could well be very wrong, but I was merely trying to suggest that any stack space used for a VLA would logically be different to that used for inline statement, in a separate entity kind of way? – David Cropley Feb 21 '17 at 00:07
-
I will logically deallocate myself now to prevent overflow Jon!! – David Cropley Feb 21 '17 at 00:17