Thread: Declaration vs Definition

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    6

    Declaration vs Definition

    IBM appears to have conflicting information about when a variable is allocated storage (instantiated).

    According to following, it literally says on the top of the page that a variable won't get storage allocated when it's declared! This in itself is not the whole picture, and readers can get mis-led.

    IBM Linux Compilers
    A declaration establishes the names and characteristics of data objects used in a program. A definition allocates storage for data objects, and associates an identifier with that object. When you declare or define a type, no storage is allocated.

    It then later went on to clarify, towards the end of the page, the following, which states if towards the end of the file the variable is only declared and not initialized, it then becomes defined. And for C++, declaration is definition. Man they should add a disclaimer for the above statement to read this also.

    C only
    A tentative definition is any external data declaration that has no storage class specifier and no initializer. A tentative definition becomes a full definition if the end of the translation unit is reached and no definition has appeared with an initializer for the identifier. In this situation, the compiler reserves uninitialized space for the object defined.
    C++ only
    C++ does not support the concept of a tentative definition: an external data declaration without a storage class specifier is always a definition.

    This may be argued to be not as detailed as the IBM one above but it's less confusing.
    2.1 — Basic addressing and variable declaration Learn C++
    To recap briefly, computers have random access memory (RAM) that is available for program to use. When a variable is declared, a piece of that memory is set aside for that variable.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,615
    This is what the [basic.def] section of a draft of the C++ standard has to say:

    A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification 25 (7.5) and neither an initializer nor a function body, it declares a static data member in a class definition (9.2, 9.4), it is a class name declaration (9.1), it is an opaque-enum-declaration (7.2), it is a template-parameter (14.1), it is a parameter-declaration (8.3.5) in a function declarator that is not the declarator of a function-definition, or it is a typedef declaration (7.1.3), an alias-declaration (7.1.3), a using-declaration (7.3.3), a static_assert-declaration (Clause 7), an attributedeclaration
    (Clause 7), an empty-declaration (Clause 7), or a using-directive (7.3.4).
    [ Example: all but one of the following are definitions:
    Code:
    int a;                        // defines a
    extern const int c = 1;       // defines c
    int f(int x) { return x+a; }  // defines f and defines x
    struct S { int a; int b; };   // defines S, S::a, and S::b
    struct X {                    // defines X
    int x;                        // defines non-static data member x
    static int y;                 // declares static data member y
    X(): x(0) { }                 // defines a constructor of X
    };
    int X::y = 1;                 // defines X::y
    enum { up, down };            // defines up and down
    namespace N { int d; }        // defines N and N::d
    namespace N1 = N;             // defines N1
    X anX;                        // defines anX
    whereas these are just declarations:
    Code:
    extern int a;                 // declares a
    extern const int c;           // declares c
    int f(int);                   // declares f
    struct S;                     // declares S
    typedef int Int;              // declares Int
    extern X anotherX;            // declares anotherX
    using N::d;                   // declares d
    —end example ]
    It basically just lists what everything is. References to other parts of the standard should probably be noted, but they all describe syntax.
    Last edited by whiteflags; 04-04-2013 at 12:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declaration Vs Definition
    By surrounded in forum C Programming
    Replies: 26
    Last Post: 02-27-2009, 04:24 PM
  2. Is int i; is a declaration or definition?
    By forumuser in forum C Programming
    Replies: 11
    Last Post: 05-30-2007, 04:43 AM
  3. Declaration vrs Definition
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2004, 07:13 PM
  4. Declaration vs definition
    By ripper079 in forum C++ Programming
    Replies: 8
    Last Post: 03-15-2002, 11:00 PM
  5. Declaration/Definition ??
    By Manish in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 11:37 AM