Multiple definitions of a variable in C++ not permitted

C

Charles L

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
 
H

hari4063

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.
 
J

Jerry Coffin

Charles said:
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.
 
J

Jack Klein

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.
 
E

E. Robert Tisdale

Charles said:
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
 
R

Ron Natalie

Jack said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top