Date: | Thu, 13 Apr 2006 15:29:11 +0300 |
From: | Diomidis Spinellis <dds@aueb.gr> |
Organization: | Athens University of Economics and Business |
User-Agent: | Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0 |
MIME-Version: | 1.0 |
Newsgroups: | comp.lang.c++.moderated |
Subject: | Re: const/define -- efficiency for simple integrals |
References: | <1144845365.832751.189250@j33g2000cwa.googlegroups.com> |
In-Reply-To: | <1144845365.832751.189250@j33g2000cwa.googlegroups.com> |
Content-Type: | text/plain; charset=ISO-8859-7; format=flowed |
Content-Transfer-Encoding: | 7bit |
studennett wrote: > Is there any difference in efficiency between (at global scope): > > static const int MAX_CHAR (1024); > > and > > #define MAX_CHAR 1024 > > I'm wondering about both time and space efficiency. I can take the > address of the static const int, so surely it must take up space. The > define is 'just a number' so doesn't have to take up any space. Is > this right? If you don't take the address of MAX_CHAR the compiler is free to optimize its storage away, and this appears to be the case with two different compilers I checked. I compiled the statement volatile int x = MAX_CHAR; and both compilers generated *exactly the same code* for the two alternatives you mentioned. GCC (version 3.4.2 g++ -O3): mov DWORD PTR _x$[esp+4], 1024 Microsoft C (version 11.00.7022; cl -Ox): movl $1024, -4(%ebp) -- Diomidis Spinellis Code Quality: The Open Source Perspective (Addison-Wesley 2006) http://www.spinellis.gr/codequality