Multiple definitions of a variable in C++ not permitted

Discussion in 'C++' started by Charles L, Feb 7, 2005.

  1. Charles L

    Charles L Guest

    Can someone explain to me what the following means?

    "C permits multiple definitions of a variable in any given namespace,
    provided the definitions are the same and it generates only a single
    variable for the multiple definitions. C++, however, does not permit
    redefinition of a variable or any other entity for a very definite reason
    that we will discuss later." Chapter 1. C++ Tutorial. Coronado Enterprises

    Charles L
    Charles L, Feb 7, 2005
    #1
    1. Advertising

  2. Charles L

    hari4063 Guest

    In C you can write:

    int a; // in module1.c

    int a; // in module2.c

    and compiler-linker will make just one instance of 'a'. In C++ this is
    error.
    hari4063, Feb 7, 2005
    #2
    1. Advertising

  3. Charles L

    Jerry Coffin Guest

    Charles L wrote:
    > Can someone explain to me what the following means?


    > "C permits multiple definitions of a variable in any given namespace,
    > provided the definitions are the same and it generates only a single
    > variable for the multiple definitions. C++, however, does not permit
    > redefinition of a variable or any other entity for a very definite
    > reason that we will discuss later." Chapter 1. C++ Tutorial.
    > Coronado Enterprises


    It means your tutorial is wrong, though I'll admit that the reason it's
    wrong is fairly subtle. In fact, it's possible that in attempting to
    keep this simple, I'll distort things a bit as well, but I'm pretty
    sure that it's still a lot more accurate than what you've quoted above.

    Neither C nor C++ allows a single variable to be defined more than once
    -- but C has the concept of a "tentative definition" which is absent
    from C++. In C, you can have a series of tentative definitions that
    result in defining a variable. For example:

    int a;

    /* ... */

    int a = 2;

    The first is read as a tenative definition and the second is allowed.

    There are limitations on what can be part of a tentative definition
    though, so if you have two definitions, neither of which can be
    considered a tenative definition, then it's an error. For example:

    int a = 2;

    /* ... */

    int a = 2;

    would be an error -- a tentative definition canNOT include
    initialization. We're left with two definitions of the same variable,
    which, contrary to Coronado's claim, neither C nor C++ allows (even
    though the two definitions are identical).

    C++ has basically the same rule except that there's no such thing as a
    tentative definition -- in the first example above, the 'int a;' will
    be treated as the full definition of 'a', so the 'int a = 2;' will not
    be allowed. As you probably expect, the second example is rejected by
    C++ as well.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Feb 7, 2005
    #3
  4. Charles L

    Jack Klein Guest

    On Mon, 07 Feb 2005 07:02:45 -0500, "hari4063"
    <> wrote in comp.lang.c++:

    > In C you can write:
    >
    > int a; // in module1.c
    >
    > int a; // in module2.c
    >
    > and compiler-linker will make just one instance of 'a'. In C++ this is
    > error.


    No, you are quite wrong. Some linkers allows this, but the behavior
    is undefined if a C program contains more than one definition of any
    external symbol. So even with tools that allow it, it is not valid C.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 8, 2005
    #4
  5. Charles L wrote:

    > Can someone explain to me what the following means?
    >
    > "C permits multiple definitions of a variable in any given namespace,
    > provided the definitions are the same and it generates only a single
    > variable for the multiple definitions. C++, however, does not permit
    > redefinition of a variable or any other entity for a very definite reason
    > that we will discuss later." Chapter 1. C++ Tutorial. Coronado Enterprises


    > cat module1.c

    int a; // in module1.c

    > cat module2.c

    int a; // in module1.c

    > cat main.c

    #include <stdio.h>

    int a; // in main.c

    int main(int argc, char* argv[]) {
    fprintf(stdout, "a = %d\n", a);
    return 0;
    }

    > gcc -Wall -std=c99 -pedantic \

    -o main main.c module1.c module2.c
    > ./main

    a = 0
    > cat module1.cc

    int a; // in module1.cc

    > cat module2.cc

    int a; // in module1.cc

    > cat main.cc

    #include <iostream>

    int a; // in main.cc

    int
    main(int argc, char* argv[]) {
    std::cout << "a = " << a << std::endl;
    return 0;
    }

    > g++ -Wall -ansi -pedantic \

    -o main main.cc module1.cc module2.cc
    /tmp/cck0DYzn.o(.bss+0x0): multiple definition of `a'
    /tmp/ccaQWncm.o(.bss+0x0): first defined here
    /tmp/cc4YVX0q.o(.bss+0x0): multiple definition of `a'
    /tmp/ccaQWncm.o(.bss+0x0): first defined here
    collect2: ld returned 1 exit status
    E. Robert Tisdale, Feb 8, 2005
    #5
  6. Charles L

    Ron Natalie Guest

    Jack Klein wrote:
    > On Mon, 07 Feb 2005 07:02:45 -0500, "hari4063"
    > <> wrote in comp.lang.c++:
    >
    >
    >>In C you can write:
    >>
    >>int a; // in module1.c
    >>
    >>int a; // in module2.c
    >>
    >>and compiler-linker will make just one instance of 'a'. In C++ this is
    >>error.

    >
    >
    > No, you are quite wrong. Some linkers allows this, but the behavior
    > is undefined if a C program contains more than one definition of any
    > external symbol. So even with tools that allow it, it is not valid C.
    >

    C does permit the following (in a single TU):

    int a;
    int a = 5;

    The first is a "tentative definition". By the time the TU ends, a single
    variable is defined.

    Between translation units, the behavior is the same as C++. Once it's
    defined in one, you can't legally define it in another.
    Ron Natalie, Feb 8, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lee
    Replies:
    3
    Views:
    305
    John C. Bollinger
    Feb 5, 2004
  2. Chris McDonald

    bit-fields and permitted types (and [OT] gcc)

    Chris McDonald, Apr 18, 2005, in forum: C Programming
    Replies:
    1
    Views:
    599
    Eric Sosman
    Apr 18, 2005
  3. Tristin Davis
    Replies:
    4
    Views:
    312
    Tristin Davis
    Jul 4, 2008
  4. Sam

    Operation not permitted

    Sam, Feb 25, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    199
    James Willmore
    Mar 6, 2004
  5. @sh
    Replies:
    14
    Views:
    170
    Randy Webb
    Dec 22, 2005
Loading...

Share This Page