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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaration vs Definition
    By grob115 in forum C++ Programming
    Replies: 1
    Last Post: 04-04-2013, 12:15 AM
  2. declaration Vs Definition
    By surrounded in forum C Programming
    Replies: 26
    Last Post: 02-27-2009, 04:24 PM
  3. definition/declaration/...
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2007, 02:38 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