From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6030 invoked by alias); 16 Dec 2004 19:08:47 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 5891 invoked from network); 16 Dec 2004 19:08:40 -0000 Received: from unknown (HELO mail.gmx.net) (213.165.64.20) by sourceware.org with SMTP; 16 Dec 2004 19:08:40 -0000 Received: (qmail invoked by alias); 16 Dec 2004 19:08:39 -0000 Received: from 149.198.62.81.cust.bluewin.ch (EHLO [192.168.123.202]) (81.62.198.149) by mail.gmx.net (mp003) with SMTP; 16 Dec 2004 20:08:39 +0100 X-Authenticated: #14737133 Message-ID: <41C1DD2D.9010108@gmx.ch> Date: Thu, 16 Dec 2004 19:08:00 -0000 From: jlh User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041126 MIME-Version: 1.0 To: matt smith , gcc-help@gcc.gnu.org Subject: Re: stack allocation References: <20041212192105.94719.qmail@web50706.mail.yahoo.com> In-Reply-To: <20041212192105.94719.qmail@web50706.mail.yahoo.com> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig63524831019BB43F1CCF1410" Content-Transfer-Encoding: 8bit X-Y-GMX-Trusted: 0 X-SW-Source: 2004-12/txt/msg00153.txt.bz2 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig63524831019BB43F1CCF1410 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Content-length: 1623 matt smith wrote: > Why the discrepancy? I think I might have found the reason for this; here's what I've been experimenting with today: extern int i; extern void f2(); void f() { f2(); i = 3; } If I compile with "gcc-4.0 -O2" I get this: (on x86) f: pushl %ebp movl %esp, %ebp subl $8, %esp call f2 movl $3, %eax movl %eax, i leave ret The pushed %ebp uses 4 bytes on the stack and GCC reserves another 8 bytes (which are never used) for a total of 12 bytes. Now if I compile the same with the option "-fomit-frame-pointer" added I get this: f: subl $12, %esp call f2 movl $3, %eax movl %eax, i addl $12, %esp ret No more %ebp on the stack, but now GCC reserves 12 bytes. In both cases the function f() uses 12 bytes of stack and together with the 4 bytes of return address being on the stack already, it totals to 16 bytes, which is a nice alignment. And as you know, proper alignment makes code faster. If f() does not call any function, GCC does not reserve any unnecessary space. In your sample code, you didn't use optimization at all, so it probably did the alignment anyway, even if no other function gets called from your function. This might be the reason why it allocates 40 bytes instead of only what it requires for storage. Then I did some measurements and apparently, calling a function with the stack not aligned to 16-bytes is slower. So GCC actually does a good job here. VoilĂ , I hope this wasn't non-sense. :) jlh --------------enig63524831019BB43F1CCF1410 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" Content-length: 252 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFBwd02BPhW1Cp6o/ERAiJeAKD9pbyRY6EmPjSZV7siNnRACDJMiwCgg8KG w/284Uukkt8JCSXm354vPYE= =kLII -----END PGP SIGNATURE----- --------------enig63524831019BB43F1CCF1410--