473,320 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Multiple definitions of a variable in C++ not permitted

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
5 2443
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: prettysmurfed | last post by:
Hi all I have this, probably stupid question, how to avoid multiple definitions when a header file is included more than once. I thought, when you wrote the header-file and used the...
14
by: Carramba | last post by:
hi! I have program with several funktion witch are in separete files, I have one include file were I have definet some variables and initiated 'const double fVar=0.874532;' this files is includet...
9
by: lbj137 | last post by:
I have two files: A.c and B.c. In both files I define a global variable, int xxxx; When I compile with a green hills compiler (and also i think with a GNU compiler) I get no errors or warnings....
8
by: yossi.kreinin | last post by:
Hi! When are multiple definitions of global variables with the same name considered legal in C, and how is it different from C++? It appears that in terms of assembly language, some C...
11
by: lars.uffmann | last post by:
Easily described problem: Using g++ version 3.3.5 under suse 9.3, bla.h: ----------- #ifndef myTEST #define myTEST ZFSInt test; #endif
28
by: Sriram Rajagopalan | last post by:
Hi, I was interested to know if there is a way to use a variable name split across multiple lines in C. For example : int this_is_a_very_long_variable_name = 10; I would like to split...
10
by: subramanian100in | last post by:
Suppose I declare a global variable int g; in two different files say a.c which has main() and b.c When I compile them to build an executable under gcc in Redhat Linux with the command ...
6
by: The Architect | last post by:
Hi, If I have the same symbol in the .data section of 2 obj files, LD gives a multiple declaration error on linking? Would like to know the reason for this (diab only issues a warning) Also...
2
by: Hel | last post by:
Hi, I'm sure you are familiar with this problem: A.h: template <unsigned Nstruct A { static const unsigned a; };
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.