[sf-lug] K & R errata

jim jim at systemateka.com
Sun Jul 3 15:13:57 PDT 2011



I think declaring a variable is 
type identifier, e.g. 
int i; 

and defining a variable is 
type identifier assign value, e.g. 
int j = 3; 

declaring a function (not in K&R) is prototyping 
type identifier argtypes, e.g. 
int doit(int, char *); 

defining a function is 
type identifier arguments block, e.g. 
int doit(int k, char * s) { while (k--) puts(s); } 


    As to when memory is actually reserved is puzzling 
(to keep it simple, stick to variables): 

    when it encounters a variable declaration, the c 
compiler updates its symbol table to record the 
identifier (name of the variable) and its type. 
    when it encounters a variable definition, the c 
compiler updates its symbol table and is forced to 
enter the assigned value somewhere in the region that 
will ultimately be (in) the data segment. 

    (assuming the above is more or less correct) the 
puzzling part has to do with the optimizing pass of the 
compiler and the linking pass. any c compiler must 
correctly implement the specification of the c language 
but may do so however its designers choose. there is no 
requirement that the compiler "allocates" memory at any 
particular point in the process. 
    the compiler may set up images of segments for 
machine code (the text segment), data, and the stack(s) 
during its compile phase or defer that work until it's 
in the optimizing phase. 
    regardless, it may "allocate" memory for a declared 
variable or not, depending on the variable's status as 
global or as local within some function. it will likely 
defer such allocation until it has determined that the 
variable is used at some point in the program's flow (or 
issue a warning of an unused identifier). 
    the optimizing stage may do the allocation or change 
the compile-pass allocation (possibly determining that 
the variable needs no allocation in the data segment). 
    the linker (or link phase) may revise any and all 
uses of memory, likely reorganizing the data segment 
into sub-segments (e.g. constants all together, ints all 
together, strings all together...). 

    put succinctly, who could ever say? 
    the language designers themselves were careful to 
keep I/O and platform and compiler designs out of the 
specification. 
    i present the above respectfully as a question: what's 
the actual answer? 

with thanks 




On Sun, 2011-07-03 at 14:30 -0700, Ken Shaffer wrote:
> Jeff,
> My memory of the item in the K &R C Programming book which was wrong
> was jogged by scanning the errata:  I think the problem involved the
> definition vs. declaration of a variable, and when memory actually was
> reserved.  I did pass my copy of the book along, so I can't check my
> margin notes.
> Ken
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug
> Information about SF-LUG is at http://www.sf-lug.org/






More information about the sf-lug mailing list