Re: declaration vs. defintion

From: Alwyn (dt015a1979_at_mac.com.invalid)
Date: 11/01/04


Date: Mon, 01 Nov 2004 13:06:03 +0000

In article <2umpgfF2astf0U1@uni-berlin.de>, Val \
<chrisval@bigpond.com.au> wrote:

> "Alwyn" <dt015a1979@mac.com.invalid> wrote in message
> news:011120041226534152%dt015a1979@mac.com.invalid...
> | In article <2ummi2F2bqr3qU1@uni-berlin.de>, Val \
> | <chrisval@bigpond.com.au> wrote:
> | >
> | > AFAIU, when we say we have *defined* something, doesn't
> | > that imply that storage is associated with the particular
> | > identifier being defined ?,
>
> Hi Alwyn.
>
> | You shouldn't think of it in terms of storage being allocated.
>
> You're probably right, but it's difficult not to.
> Let me try to explain further.
>
> | Just think of a definition as a specification that is
> | sufficiently complete to be used in a program.
> |
> | Take for instance:
> |
> | const int Five = 5;
>
> The above is a definition, yes, I agree.
>
> | inline int timesFive(int n)
> | {
> | return n * Five;
> | }
>
> This function is also a definition.
>
> | There is no need for there to be storage associated with either Five or
> | timesFive, yet they have been defined, and they are usable, no?
>
> But in your example, they *do* have storage accossiated
> with them.

No, they don't necessarily. They just stand there for the compiler to
use as it thinks fit. If you take the address of either, then it will
be compelled to allocate space, otherwise there is no good reason for
it.

> This...
> int Five;
>
> ...alone is a definition of an integer variable with the
> identifier names 'Five', and has automatic storage.

Well, it could be global too. Without seeing the context, you cannot
tell whether it is automatic or global/static.

> | > therefore meaning that once we
> | > have a definition of an object, we cannot define it again ?,
> | > hence the ODR ?
> |
> | I believe the one-definition rule goes something like this:
> |
> | A translation unit must not contain more than one definition of any
> | variable, function, class type, enumeration type, or template, and
> | external definitions must not be redefined within a program.
>
> Ok, so how do you see that contradicting this:
>
> int main()
> {
> typedef int Int;
> typedef int Int;
> typedef int Int;
>
> std::cout << Int( 100 ) << std::endl;
>
> return 0;
> }
>
> ?

It doesn't. Typedefs are a special case. You can (re)define them as
many times as you like.

> | A user-defined type is not an object and is not included in
> | the above list.
>
> Right, because it is only the blueprint of the object to be
> instantiated from it.

Yes, though my wording was a little imprecise. Classes and enumerations
could be called user-defined types, and templates could also be
considered 'blueprints', yet they are in the list of things that cannot
be redefined. I was specifically thinking of 'typedef's there, which
are nothing more than a name for an existing type.

Hoping I haven't made things even more confusing for you. :-)

Alwyn



Relevant Pages

  • Re: declaration vs. defintion
    ... | You shouldn't think of it in terms of storage being allocated. ... | inline int timesFive ... identifier names 'Five', and has automatic storage. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: one-liner for characater replacement
    ... declaration you can omit the type and it is assumed 'int', ... a qualifier e.g. 'const'. ... unspecified but fixed default-promoted arguments returning int'. ... (Qualification only makes sense for things in storage ...
    (comp.lang.fortran)
  • Re: GCC bitfield packing
    ... The actual amount of storage taken us depends only upon the number of ... The size of the storage unit that those bits are stored ... depend upon the type of the bitfield declaration. ... _Bool, int, signed int, and unsigned int. ...
    (comp.std.c)
  • Re: declaration vs. defintion
    ... |> | You shouldn't think of it in terms of storage being allocated. ... | They just stand there for the compiler to use as it thinks fit. ... |> int Five; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How to eliminate this global variable, silent?
    ... Joe Wright wrote: ... "An object, sometimes called a variable, is a location in storage and its interpretation depends on two main attributes: its storage class and its type." ... you would probably agree that you are allocating an array of 10 ... I would say this allocates an array 10 of int. ...
    (comp.lang.c)