By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Multiple definitions of a variable in C++ not permitted

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

P: n/a
On Mon, 07 Feb 2005 07:02:45 -0500, "hari4063"
<za**************@pmf.unsa.ba> 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/~a...FAQ-acllc.html
Jul 23 '05 #4

P: n/a
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
Jul 23 '05 #5

P: n/a
Jack Klein wrote:
On Mon, 07 Feb 2005 07:02:45 -0500, "hari4063"
<za**************@pmf.unsa.ba> 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.

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.