C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 21st, 2003, 12:05 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
[c] sizeof: calculated at compile, or during run?

in the k&r book it says :

"c provides a compile-time unary operator called sizeof that can be used to compute the size of any object."

but then in some notes on the net about this part of the book it says :

"The sentence ``But the expression in the #define is not evaluated by the preprocessor'' means that, as far as the preprocessor is concerned, the ``value'' of the macro NKEYS (like the value of any macro) is just a string of characters..."

the line of code in the k&r book is:

Code:
#define NKEYS (sizeof keytab / sizeof keytab[0])


i thought basically #define replaced the text. nothing more. so in some code that has the above #define does NKEYS get replaced with this actual exact code:

Code:
(sizeof keytab / sizeof keytab[0])


or does NKEYS get calculated, so therefore replaced with the value?

this could be useful to answer this, but maybe not. keytab is defined like:
Code:
struct key {
	char *word;
	int count;
} keytab[] = {
	"auto", 0,
	"break", 0,
	"case", 0,
	...
	...
	"volatile", 0,
	"while", 0
};

Reply With Quote
  #2  
Old May 21st, 2003, 12:41 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,507 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 9 m 24 sec
Reputation Power: 865
The process goes like this:
Source Files --> Preprocessor --> Compiler --> Linker --> Executable file.

The job of the preprocessor is to resolve all the #include and #define lines in the source code. What it does is, take the source file(s), perform search and replace operations, and produce an intermediate file for the compiler to compile.

So, to answer your question, the preprocessor merely replaces all occurences of NKEYS with the string, "(sizeof keytab / sizeof keytab[0])". After the preprocessor is done, the intermediate file is passed on to the compiler.

The process of actually determining sizeof keytab and sizeof keytab[0] is done by the compiler, which is the next stage in the process. If your compiler is smart enough (and also, depending on your optimization settings), it may even compute the result of sizeof keytab / sizeof keytab[0] at compile time, and replace the expression, with that computed value.

Reply With Quote
  #3  
Old May 21st, 2003, 01:18 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
i see. thanks.
Quote:
If your compiler is smart enough (and also, depending on your optimization settings), it may even compute the result of sizeof keytab / sizeof keytab[0] at compile time, and replace the expression, with that computed value.

right, that's the part i was particularly interested in. so the #define part is irrelevent. it's a question of whether the compiler calculates, when it compiles, the size of keytab, and places that value in the end code, or places the calculation to get that value into the end code.

what it's hopefully sizing up at compile time, keytab, is constant, so i'd hope, expect even, it is smart enough. how can i find out if the compiler is doing that, or make it do so? (i'm using apple's gcc 3.1 which is pretty similar to the usual gnu gcc, apart from it's got a few extra bits, i think)

Reply With Quote
  #4  
Old May 21st, 2003, 01:28 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,507 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 9 m 24 sec
Reputation Power: 865
To make things clearer, let's say sizeof keytab array is 80 bytes and sizeof keytab[0] is 4 bytes. If your expression is something like this:
foo = sizeof keytab / sizeof keytab[0];

The compiler will know the sizeof keytab and sizeof keytab[0] at compile time. However, if the compiler is not smart enough, it may compile as though you typed this:
foo = 80 / 4;
If it is smart enough to optimize the expression, the code may end up as though you typed this:
foo = 20;

To determine what your compiler is doing, try compiling with the -S option. This will generate the intermediate assembly language file, so you can see how that statement was optimized. You might also consider tossing in different optimization option levels too (i.e. -O0, -O1, -O2 etc.) and see how it optimizes differently.

Last edited by Scorpions4ever : May 21st, 2003 at 01:31 PM.

Reply With Quote
  #5  
Old May 21st, 2003, 01:43 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
i see. it's the calculation bit, not the sizeof bit that may or may not get done at compile time. sizeof gets done at compile time regardless of compiler smartness.

thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > [c] sizeof: calculated at compile, or during run?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway