Re: When is a static data member defined?

From: Steven T. Hatton (susudata_at_setidava.kushan.aa)
Date: 10/21/04


Date: Thu, 21 Oct 2004 14:28:57 -0400

Victor Bazarov wrote:

> Steven T. Hatton wrote:
>> Victor Bazarov wrote:
>>
>>
>>>Compilation and linking of static members of non-local classes is very
>>>similar to that of any other global object, and, not surprisingly, any
>>>function. If you just declare a function
>>>
>>> void foo();
>>>
>>>and never define it, it will compile and link fine as long as you don't
>>>try to use the function.
>>>
>>>I am not sure how this can be as daunting as you make us believe you see
>>>it.
>>>
>>>Victor
>>
>>
>> We aren't talking about a function. IIRC, a function in C++ is
>> specifically _not_ an object in any sense of the word.
>
> Function is not an object. However, just like any other non-temporary
> object, every [ordinary] function is designated by a name. When
> a non-const object is declared in the namespace scope, its name has
> external linkage, just like a function (unless otherwise specified).
> That means they have some kind of symbol by which they are recognised
> from another module (or translation unit).
>
> If used anywhere, a symbol (name) has to be _defined_ exactly once (the
> One Definition Rule) to make a well-formed program. If _not_ used
> anywhere, a symbol does NOT have to be defined. See 3.2/3.
>
> > I'm not really doubting that the
>> memory is allocated when the definition is compiled. I'm just wondering
>> how how the pieces are put together. In view of the fact that the point
>> of definition of a static member object of class type is where a
>> constructor will be invoked, it makes sense that it will be physically
>> allocated when that definition's object code representation is loaded.
>>
>> After examining the object files emitted by the compiler for various
>> builds, I can see that if the static member is not used, it is not even
>> present in the object file containing the entry point.
>
> Well, you're closer to understanding how this works, I trust. I am not
> sure how we can help you further without going into the implementation
> details. Perhaps if you find a book on compilers and linkers or talk to
> people in a newsgroup for a particular implementation of the language,
> you might learn more about that particular implementation or about how
> to implement a language in general.
>
> An example of something that is very common yet not considered part of the
> language definition: virtual function mechanism implementation. On all
> implementations I know it is done using a so called 'virtual function
> table', a list of addresses of class functions is stored somewhere in
> memory and a pointer to that table get stored in every object of that
> class at the time of construction. Short name for that pointer/pointers
> is 'vtbl'. Yet there is no mention of it in the Standard. Why? Because
> it is considered an implementation detail. See why I am reluctant to go
> into linking and how it works or should work?
>
> V

The virtual function table is discussed extensively in the TC++PL(SE). The
idea that it is not topical here is absurd.

-- 
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell


Relevant Pages

  • Re: OO compilers and efficiency
    ... > Calculating the address of the virtual function can usually be done ... address (4 instructions). ... personally I think C is a beautiful programming language. ... compiler vendor and a smaller base of skilled programmers. ...
    (comp.programming)
  • Re: "STL from the Ground Up"
    ... high-level intermediate language than can interoperate with many other ... If your language lacks expressive features then you cannot write code ... memory management in comparison. ... Mostly because type errors mean that the programmer and compiler disagree ...
    (comp.programming)
  • Re: A note on computing thugs and coding bums
    ... It would handle international characters if the execution character ... method I used in "Build Your Own .Net Language and Compiler". ... work areas and counting on Nul is an illusion. ...
    (comp.programming)
  • Re: access(FULLPATH, xxx);
    ... with "trial& error" to just silence the compiler. ... void *foo); ... given that the language in the specification _was_ abiguous and both ... documentation was paramount. ...
    (freebsd-questions)
  • Writing a Roguelike in D, C# and C++
    ... Since I have seen some interest on this newsgroup about which language ... The official compiler is dmd. ... The roguelike I am writing uses variable sized glyphs. ... trigger Hejlberg's garbage collector. ...
    (rec.games.roguelike.development)