Re: variables in a static method
6 Jul 1999 22:54:22 -0400

          From comp.compilers

Related articles
variables in a static method (Yale Hu) (1999-07-05)
Re: variables in a static method (1999-07-06)
Re: variables in a static method (1999-07-06)
| List of all articles for this month |

Newsgroups: comp.compilers
Date: 6 Jul 1999 22:54:22 -0400
Organization: - Share what you know. Learn what you don't.
References: 99-07-017
Keywords: C++

    Yale Hu <> wrote:
> In C++, a static method of class X is shared by all instances of X. So
> the code generated by a compiler for a static method should have only
> one copy in memory at run time, and all instances of the class refers
> to the address when the method gets called.

I may be misunderstanding what you are saying here; if so, please
clarify your question.

If you are asking how many copies of the executable code of a static
member function are in memory, the answer is "one." For most (all?)
implementations, however, there is no difference for non-static member
functions -- there will be only one copy of the code in memory,
regardless of how many objects of that class have been created.

While it's possible to imagine that creating a new object would make a
per-object copy of each of the class's member functions (there's
nothing in the standard that would forbid that kind of implementation,
as far as I can tell), the normal technique is to have a single copy
of each member function and simply pass the address of the object for
which it is called in an implicit parameter, which is taken as the
value of "this". The text of a non-static member function is thus
shared by all objects of its class.

Also, there's nothing that requires an object of a class to "refer to"
the address of a member function. The common implementation technique
for virtual member functions is for each object to have a pointer to a
table of pointers to the correct member functions, but some other
dispatch mechanism (involving a type lookup, for instance) would also
be standard-conforming. In particular, there is no need for any
object of a class to exist in order to invoke a static member function
of that class.

> If the static method defines some variables (e.g. X::M() { char
> v[100]; ...} ), it seems that each call of the method creates a copy
> of the variables. I question is how a compiler allocates the memory
> for the variables, allocating with objects at the time the objects get
> created? with a fixed offset of the object address?
> [The variables are local to the routine, so I imagine that they're
> allocated on the stack for the duration of the call, just like any
> other automatic variables. C++ stashes data in only three places:
> in the structure that represents an instance of the class for normal
> class members, in static memory for static and external data, and on
> the stack for local variables within a routine. -John]

John is correct here. There is absolutely no difference between a
static member function and a non-member function except for scope and
access issues. Apart from name lookup and access permission, a static
member function is identical in all respects to a non-member function,
including the allocation, initialization, and destruction of any local
William M. Miller,
Software Emancipation Technology (

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.