⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alloca.glb.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
字号:
<html><!-- Mirrored from c-faq.com/malloc/alloca.glb.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:00:17 GMT --><head><title></title></head><body>From: Gordon Burditt<br>Newsgroups: comp.lang.c<br>Subject: Re: <TT>alloca()</TT><br>Date: 25 Jan 1999 09:34:09 GMT<br>Message-ID: &lt;16167775DFB6601E.39220BF5766349A7.B20FE25E4B4173B2@library-proxy.airnews.net&gt;<p>&gt;I'm curious to hear a little about a function called <TT>alloca()</TT>.<br>&gt;The (Solaris) man page I have here tells me that it allocates <br>&gt;memory on the stack, which is then automatically freed when you <br>&gt;return. Now, I'm well aware that <TT>alloca()</TT> is Not Standard And<br>&gt;thus System Dependant and Beyond The Scope Of This Group,<br>&gt;so please don't feel the need to reply just to tell me that. On<br>&gt;the other hand, I'm interested to know how portable this is <em>in</em><br>&gt;<em>practice</em>, and what advantages and disadvantages it has w.r.t <br>&gt;<TT>malloc()</TT> et al. I've seen it used a fair bit, and presumably the<br>&gt;authors of the code must have had <em>some</em> reason for using it.<br><p>This function is one of the most horribly unportable abominationsI have ever tried to implement.  There are ways to try toimplement it with <TT>malloc()</TT>, but if the memory is actuallyallocated on the stack, it causes lots of problems.  You'repretty much stuck with needing to implement this in the compileras a built-in.  <p>You can write a function that knows about linkage conventions that adjusts the stack, but on one architecture (which didn't use a stackframe pointer), I ended up having to allocate something like 128 bytes extra on each call, due to possible register saving.  That 128 byte figure was a <strong>guess</strong> as to what the worst case was, and I think it involved some (unreasonable) assumptions about the maximum number of arguments to functions that call <TT>alloca()</TT>.  You did getthose bytes back when the function exited.  If it had beenpractical to modify the compiler-generated function exit sequences, life would have been much simpler.<p>What's the problem?  Well, if you start allocating stuff on thestack when there's already stuff on the stack, you have<TT>alloca()</TT> allocating stuff in the middle of it, which reallymesses up argument passing, where stuff pushed on the stackis supposed to be contiguous.  Horrible example:<p><pre>    printf("%p %p %p %p %p\n", alloca(1), alloca(2), alloca(10), 	alloca(8), alloca(20));</pre><p>Now, where can you use <TT>alloca</TT>?  I think this is reasonably safe:<p>You may use alloca() in the form of:<pre>	pointer_variable = alloca(expression);</pre>as an expression statement in the outermost block of a function.  <p>You are asking for trouble if you use it anywhere that doesn't fit this description.  You are likely to run into trouble if you use <TT>alloca()</TT> in any of these places, because there might be something on the stack at the point alloca() is called:<p><ol><li>Inside a loop.<li>Inside any block that begins with local variables, exceptthe outermost block of a function, especially if theallocated memory is used after exiting this block.<li>Using any expression more complicated than a pointer variableon the left hand side of an assignment, including oneelement of an array of pointers.  <li>Where the return value of alloca() is used as a function argument.<li>In any context where the value of the = operator is used,such as <pre>	if ((pointer_variable = 		alloca(sizeof(struct something))) == NULL) { .... }</pre></ol><p>And I expect that someone will call me on even THAT highly restrictivelimitation not being conservative enough for the code generated bysome compilers.  Now, if it's done as a compiler builtin, you mightmanage to get around the problems.  <p>Once I finally got that <TT>alloca()</TT> function figured out, it workedreasonably well - as I recall, the primary use for it was in aBison parser.  That 128 bytes wasted per invocation combinedwith a fixed stack size could be a nuisance.  Why didn't I justuse GCC?  Because this was an attempt to <strong>port</strong> GCC, initiallyusing cross-compilers, to a machine that turned out to barelyhave enough memory to compile GCC (1.35 or so) natively.  When GCC 2 came out, it turned out to be enough of a memory pigthat natively compiling itself was out of the question.<p>					Gordon L. Burditt</body><!-- Mirrored from c-faq.com/malloc/alloca.glb.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:00:17 GMT --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -