Declarations and Definitions

This is a discussion on Declarations and Definitions within the C++ Programming forums, part of the General Programming Boards category; Hi The following text is from a book: Declarations and Definitions Letís digress for a moment to note a subtle ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    Declarations and Definitions

    Hi

    The following text is from a book:
    Declarations and Definitions
    Letís digress for a moment to note a subtle distinction between the terms definition and declaration as applied to variables.

    A declaration introduces a variableís name (such as var1) into a program and specifies its type (such as int). However, if a declaration also sets aside memory for the variable, it is also called a definition. The statements

    int var1;
    int var2;

    in the INTVARS program are definitions, as well as declarations, because they set aside memory for var1 and var2. Weíll be concerned mostly with declarations that are also definitions, but later on weíll see various kinds of declarations that are not definitions.
    I will tell you what I understand of definition and declaration. In the following code
    Code:
    struct POINT
    { float x, y; };
    is a declaration because it doesn't set aside memory. It's just a blueprint. But
    Code:
    POINT p1, p2, p3;
    is a definition because it sets aside memory space for the variables. Do I have it right? Please let me know. Thanks.

    Code:
    // adding two points p1, p2
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    /////////////////////////////////////////////////////////
    struct POINT
    { float x, y; };
    /////////////////////////////////////////////////////////
    
    int main()
    
    {
        POINT p1, p2, p3;
    
        cout << "enter the x-coord of first point: "; cin >> p1.x;
        cout << "enter the y-coord of first point: "; cin >> p1.y;
    
        cout << endl;
    
        cout << "enter the x-coord of second point: "; cin >> p2.x;
        cout << "enter the y-coord of second point: "; cin >> p2.y;
    
        cout << endl;
    
        p3.x = p1.x + p2.x;
        p3.y = p1.y + p2.y;
    
        cout << "the third point is: " << p3.x << ", " << p3.y << endl;
    
        cout << endl;
    
        system("pause");
    
        return 0;
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Yes, you're right. Keep it up!
    Devoted my life to programming...

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I'm not an expert on this subject but here is how I understand it. You should probably make sure other posters confirm my suspicions before you trust them.

    You get different kinds of declarations and definitions, to name a few: structure declarations/definitions, function declarations/definitions, variable declarations/definitions. In general if some construct in the code tells you everything you need to know about some entity, then it's a definition; if the construct leaves out some information that will be given ("defined") somewhere else then it's a declaration.

    So starting with the simplest example, consider functions. If I write
    Code:
    void f() {
        std::cout << "Inside f()\n";
    }
    then that's a function definition. It tells you everything you need to know about the function: what type it returns, what parameters it takes, what its code is. But if I want to call the function f() from a different file, or higher up in the same file (before it was defined), then I'll need a function prototype. Right?
    Code:
    void f();
    This is a declaration, because it tells you enough information about f() to be able to use it, but not everything. Clearly, we can't tell from this what f() actually does. The declaration is saying to the compiler, "Somewhere there's a function that looks like this. I promise I'll give you the code for it at some point."

    You have something similar with different kinds of entities. Consider structs/classes. When I say
    Code:
    struct S {
    public:
        int x;
    };
    then that's a structure definition because it tells you absolutely everything about the structure. However, C++ allows me to try to write mutually recursive data structures (as long as I use pointers instead of straight variables), e.g.
    Code:
    struct A {
        B *b;  // error here
    };
    
    struct B {
        A *a;
    };
    This code won't compile, because by the time of the struct A definition, the compiler doesn't know what B is and it will throw an error message at you. But I can add a structure forward declaration, essentially a structure declaration, and the compiler will be happy:
    Code:
    struct B;  // forward declaration
    
    struct A {
        B *b;
    };
    
    struct B {
        A *a;
    };
    The "struct B;" declaration doesn't tell me everything about a structure. I don't know what the members are, so I can't access any of them. Most importantly, I don't know how big a structure of type B is, since that would require knowing the size of the elements. And C++ doesn't let you create an object, an instance of a structure, without knowing how big it is. This is what prevents me from doing
    Code:
    struct B;  // forward declaration
    
    struct A {
        B b;  // error: forward-declared structure, don't know size of B
    };
    
    struct B {
        A a;
    };
    which would require infinite memory. Oops.

    So now for the last type, variables. Consider global variables. If I say
    Code:
    int global;
    in some file, that's a variable definition: it tells you everything you need to know about the variable. Where it is (in global scope), what the name is, what the type is. But if I put the same line in another file, this would generate a linker error. The linker would be trying to resolve all undefined symbols and combine all existing symbols into one object (the executable), and it would find that the symbol "global" was defined twice. It would be like having a main() in one file and another main() in a different file. So what you can do is to leave the definition of global in one file and in all other files that need to use the variable, put instead
    Code:
    extern int global;
    This is a variable declaration, because it tells the compiler and the linker, "Somewhere else there's a definition of this variable of this type. I'm not telling you where, that's the linker's job, but for now you should mark this symbol as unresolved and not allocate any memory for it."

    Now for one last note: your idea of "blueprints" is related to the issue of declaration/definition. When you say
    Code:
    struct POINT
    { float x, y; };
    you're creating a blueprint, a description for what all POINTs will look like. This doesn't allocate any memory. But when you say
    Code:
    POINT p1, p2, p3;
    then you're instantiating the structure to create variables. A struct definition by itself just exists without taking any memory, and instances of the struct or class (which are referred to as objects) are the actual variables that you know and trust.

    You can find more information about this by googling for "declaration vs definition" or something similar.

    Which book are you using, just out of curiosity?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, Sipher, dwks.

    @ dwks: It's Object-Oriented Programming in C++ by Robert Lafore. Thanks for the reply.

    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,301
    Quote Originally Posted by jackson6612 View Post
    I will tell you what I understand of definition and declaration. In the following code
    Code:
    struct POINT
    { float x, y; };
    is a declaration because it doesn't set aside memory. It's just a blueprint. But
    Code:
    POINT p1, p2, p3;
    is a definition because it sets aside memory space for the variables. Do I have it right? Please let me know.
    Despite assurances you have received otherwise, you do not quite have it right.

    The first code you show is a definition (not just a declaration) of a struct type named POINT. The second code you show defines three variables of that type.

    The difference between a type declaration and a type definition is that a type definition does not provide enough information to the compiler to allow creation of variables of that type or computing sizeof() for that type.

    So, this is a type declaration
    Code:
        struct POINT;
    but this is a type definition (of the same type).
    Code:
    struct POINT { float x, y; };
    The first (also known in some circles as a forward declaration) allows creation of references or pointers to POINT only. It is not possible to create instances of POINT (i.e. define variables), given only a declaration. Given a pointer (or reference) to POINT, it is not possible to access POINT's members if given only a declaration of POINT. The compiler needs POINT to be defined (not just declared) in order to allow that.
    Last edited by grumpy; 05-26-2011 at 06:10 PM.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    You may want to edit this bit of your post, grumpy: "The difference between a type declaration and a type definition is that a type definition does not provide enough information to the compiler". No doubt an oversight.

    ...

    To the OP,

    I don't know whether the text will sometime later get into a little more detail about the semantics of declarations and definitions. However, it's quite understandable that from what you read in that text you shown us, that you may be confusing variables with types. So here's a quick 101:

    1. The terms declaration and definition have different meanings when used in the context of variables and of types. This is the golden rule.

    2. As far as variables are concerned, what you read there is correct. A declaration simply introduces a name into the scope but does not allocate storage space. A definition, on the other hand, is always a declaration too; Besides introducing a name, it also allocates storage.

    Code:
    extern foo;    // declaration of the variable foo. (google for "c++ extern" to understand the keyword if you don't already)
    int foo;         // definition of the variable foo (which is also a declaration
    3. But for user-defined types definitions, the context is different and so are the meanings of these words. So, a type definition is finished when you type the closing curly brace. At that moment that type is fully defined and can be used in your code as you start creating new instances. A user-defined type is said to be declared, when you just introduce your name, but don't define it (you don't open a curly brace).

    Code:
    class foo;  // type declaration (also known as forward declaration)
    
    class foo {   // class definition
       int x;
       int y;
    };
    4. The declaration in the context of user-defined types has a similar meaning to that of a variable declaration. And sort-of similar semantics. It introduces a name into the scope. On the case of user-defined types this has a few uses that you will find later in your explorations. On the other hand, the term "definition" has here a completely different meaning from variables definitions. Keep this difference in mind.
    Last edited by Mario F.; 05-26-2011 at 07:39 PM.
    The programmerís wife tells him: ďRun to the store and pick up a loaf of bread. If they have eggs, get a dozen.Ē
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-18-2011, 03:49 PM
  2. Replies: 23
    Last Post: 03-07-2011, 04:28 PM
  3. declarations, definitions and externals
    By Drogin in forum C Programming
    Replies: 2
    Last Post: 08-30-2009, 01:17 PM
  4. Inline Definitions and Declarations?
    By legit in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2009, 01:59 PM
  5. Declarations and definitions in C
    By jack1234 in forum C Programming
    Replies: 8
    Last Post: 12-17-2007, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21