Re: When is a static data member defined?

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 10/20/04


Date: Wed, 20 Oct 2004 17:17:43 -0400

Steven T. Hatton wrote:
> Victor Bazarov wrote:
>
>
>>Steven T. Hatton wrote:
>>
>>
>>>Victor Bazarov wrote:
>>>
>>>
>>>
>>>>Steven T. Hatton wrote:
>>>>
>>>>
>>>>>Victor Bazarov wrote:
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>>Steven T. Hatton wrote:
>>>>>>
>>>>>>
>>>>>>>So my question might be rephrased: when is the memory
>>>>>>
>>>>>>>allocation determined for the static data member?
>>>>>>
>>>>>>Memory allocation for any object is determined when the object has
>>>>>>a complete type.
>>>>>
>>>>>
>>>>>So, in the example I posted, that would be when the member declaration
>>>>>S::C s is encountered in the struct definition.
>>>>>
>>>>>Here's just something to think about:
>>>>
>>>>So, have _you_ thought about it?
>>>>
>>>>
>>>>
>>>>>#include <iostream>
>>>>>
>>>>>using std::cout;
>>>>>using std::endl;
>>>>>using std::ostream;
>>>>>
>>>>>class C {
>>>>> int _v;
>>>>>public:
>>>>>
>>>>> C(const int& v = 0)
>>>>> :_v(v)
>>>>> {}
>>>>>
>>>>> ostream& print(ostream& out) const
>>>>> {
>>>>> return out << _c._v << " " << _v;
>>>>> }
>>>>>
>>>>> static const C _c;
>>>>
>>>>That's a declaration.
>>>>
>>>>
>>>>
>>>>>};
>>>
>>>
>>>C is a complete type. So can we say C::_c is allocated at this point?
>>
>>First of all, C is not a complete type until the closing curly brace is
>>encountered.
>
>
> My eyes must be playing tricks on me.
>
>
>>Second, no, C::_c is not
>>allocated at that point. Any object is only allocated when it's _defined_
>>not when it's _declared_.
>
>
> "Memory allocation for any object is determined when the object has
> a complete type."

Determination of memory allocation (the ability to allocate memory) and
the actual _act_ of allocating memory are two different things. You need
to be a bit clearer about what you ask.

Memory is allocated when a static or automatic object is defined. For
now we can probably leave dynamic objects alone.

>
> What about this?
>
> int foo(int i) {
> int x;
> return x * i;
> }

What about it? As far as I can tell it produces undefined behaviour
because 'x' has indeterminate value.

V



Relevant Pages

  • Re: How to release heap memory that is marked as free
    ... As I said, fragmentation is a very serious problem, and one of the most serious problems ... my allocator was accused of using massive amounts of memory. ... I'm going to have to re-think the memory allocation that I'm ... process's 'working set'. ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH 00/28] Swap over NFS -v16
    ... memory they can consume. ... So we need the extra (skb) ... included in the reserve? ... if the allocation had to dip into emergency reserves, ...
    (Linux-Kernel)
  • Re: Memory leak with CAsyncSocket::Create
    ... read my essay on how storage allocators work. ... Create method is consuming system memory that is not released back to ... The memory consumption is either shown as "Mem Usage" on the Task ... many levels of allocation going ...
    (microsoft.public.vc.mfc)
  • Re: OT: C++ overloading operators
    ... dynamic allocation, no matter how many "clever tricks" are used... ... though there's enough memory in the system, ... all these "flexible data types" map into CPU command ... The computing environment is completely ...
    (comp.dsp)
  • Re: realloc and malloc [was: memfrob and strfry ? OT]
    ... int main{ ... Can I have an example of malloc by itself? ... Here we have called malloc to attempt an allocation of ELEMENTS*sizeof ... memory you passed to malloc. ...
    (comp.lang.c)