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 definitions are translated
to "weak" symbols, in which case multiple definitions are merged by the
linker, while some become "normal" symbols, triggering linker errors in
case they are defined more then once. Is it true that in C++, multiple
definitions are always disallowed?
This code (2 translation units) compiles with gcc -ansi, but triggers
linker errors with g++:
//weak1.c (or weak1.cpp):
#include <stdio.h>
int a[5];
void f();
int main()
{
printf("&a=%p\n",a);
f();
}
//weak2.c (or weak2.cpp):
#include <stdio.h>
int a[10];
void f()
{
printf("f: &a=%p\n",a);
}
Compiled with gcc, it prints:
&a=0x80495e0
f: &a=0x80495e0
- showing that the definitions `int a[5]' and `int a[10]' were merged.
A symbol table listing shows that the dimension of a[] in the linked
binary is 10. Adding initializers to the definitions, for example,
triggers linker errors in C.
Thanks in advance!
Yossi 8 3227 yo***********@gmail.com wrote: Hi!
When are multiple definitions of global variables with the same name considered legal in C,
Never.
and how is it different from C++?
Not topical on comp.lang.c, but I would expect it to be the same.
It appears that in terms of assembly language, some C definitions are translated to "weak" symbols, in which case multiple definitions are merged by the linker, while some become "normal" symbols, triggering linker errors in case they are defined more then once.
That may be the case on some implementations, but it is not true on all.
On one of the C implementations I use regularly it will produce a link
time error on multiple definitions of a symbol, on another it won't.
That's the wonder of Undefined Behaviour, *anything* can happen.
Is it true that in C++, multiple definitions are always disallowed?
This code (2 translation units) compiles with gcc -ansi, but triggers linker errors with g++:
Use different options with gcc and you may get different results.
//weak1.c (or weak1.cpp): #include <stdio.h> int a[5];
<snip>
//weak2.c (or weak2.cpp): #include <stdio.h> int a[10];
<snip>
Undefined behaviour in C. Anything can happen.
<snip>
binary is 10. Adding initializers to the definitions, for example, triggers linker errors in C.
So will selecting the correct option in some versions of gcc.
Specifically the -fno-common option. You should also use -pedantic if
you want gcc to be a fully compliant C compiler, and looking at and
enabling the other warnings would also be a good thing.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc
On 4 Mar 2006 04:34:34 -0800, in comp.lang.c , yo***********@gmail.com
wrote: Hi!
When are multiple definitions of global variables with the same name considered legal in C,
Never, there can be only one definition of any object.
Multiple global declarations are fine though, so long as they're the
same. Your example with two different declarations of 'a' should have
triggered a warning from your compiler, and is I believe undefined
behaviour.
and how is it different from C++?
No idea.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
On Sat, 04 Mar 2006 14:44:54 +0000, in comp.lang.c , Flash Gordon
<sp**@flash-gordon.me.uk> wrote: yo***********@gmail.com wrote: Hi!
When are multiple definitions of global variables with the same name considered legal in C,
Use different options with gcc and you may get different results.
//weak1.c (or weak1.cpp): #include <stdio.h> int a[5];
<snip>
//weak2.c (or weak2.cpp): #include <stdio.h> int a[10];
<snip>
Undefined behaviour in C. Anything can happen.
IIRC these are declarations and tentative definitions. The fact that
they're different is indeed UB but its not illegal to have several
tentative definitions.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
> IIRC these are declarations and tentative definitions. The fact that they're different is indeed UB but its not illegal to have several tentative definitions.
Could you please explain that? If I understand correctly, you say that
`int a[5]' in both translation units are "declarations and tentative
definitions", but I don't understand exactly what that means. Does it
mean that multiple definitions are going to be allowed by the compiler,
but the behaviour is only defined when they are identical (if so,
identical on what level)? Which definitions are tentative?
TIA,
Yossi
Thanks!
I use -Wall -ansi -pedantic. I assumed it does the trick. Forgive me if
it's the wrong place to ask, but while we're at it - what are the
correct options to ask gcc for it's fullest standard compliance? It
looks like -ansi -pedantic are not enough.
Or is it standard compliant to ignore multiple definitions of a[],
especially if I fixed them to define the same array size? Do any
multiple definitions trigger undefined behaviour, or only different
ones?
Mark McIntyre <ma**********@spamcop.net> writes: On Sat, 04 Mar 2006 14:44:54 +0000, in comp.lang.c , Flash Gordon <sp**@flash-gordon.me.uk> wrote:
yo***********@gmail.com wrote: Hi!
When are multiple definitions of global variables with the same name considered legal in C,
Use different options with gcc and you may get different results.
//weak1.c (or weak1.cpp): #include <stdio.h> int a[5];
<snip>
//weak2.c (or weak2.cpp): #include <stdio.h> int a[10];
<snip>
Undefined behaviour in C. Anything can happen.
IIRC these are declarations and tentative definitions. The fact that they're different is indeed UB but its not illegal to have several tentative definitions.
This is only true within the same translation unit. A tentative
definition still causes the object to be defined at the end of the
unit. Since both units define it, this causes UB regardless of
disagreement on type.
-Micah yo***********@gmail.com wrote: Could you please explain that? If I understand correctly, you say that `int a[5]' in both translation units are "declarations and tentative definitions", but I don't understand exactly what that means.
A tentative definition is a declaration that will be interpreted as a
definition only if no other (non-tentative) definition appears in the
translation unit. In your case, there are no other definitions in the
translation units, so both of the tentative definitions are, in fact,
interpreted as definitions, resulting in multiple definitions for the
same object, which results in undefined behavior. The code is
incorrect, but there's no obligation for the compiler/linker to diagnose
it.
-Larry Jones
I've got an idea for a sit-com called "Father Knows Zilch." -- Calvin
On Sat, 04 Mar 2006 21:13:54 GMT, in comp.lang.c , la************@ugs.com wrote: yo***********@gmail.com wrote: Could you please explain that? If I understand correctly, you say that `int a[5]' in both translation units are "declarations and tentative definitions", but I don't understand exactly what that means.
A tentative definition is a declaration that will be interpreted as a definition only if no other (non-tentative) definition appears in the translation unit.
Agh, for some reason i had it in my head that the rule was across all
TUs.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =---- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: SilverShadow |
last post by:
Hello,
I'm having trouble with something that may be easily remedied. I use
Cantera running on Python. I need to make multiple "Reactor()" objects
and have them assigned a different (user...
|
by: weston |
last post by:
I've got a piece of code where, for all the world, it looks like this
fails in IE 6:
hometab = document.getElementById('hometab');
but this succeeds:
hometabemt =...
|
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...
|
by: capes |
last post by:
have functions in my cell model calling CVODE for each time step.
When I build the application I get a number of link errors:
\cardiacsimulator.o Release\ccardiaccell.o Release\ccelldata.o...
|
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
...
|
by: Gaijinco |
last post by:
I'm having a weird error compiling a multiple file project:
I have three files:
tortuga.h where I have declared 5 global variables and prototypes for
some functions.
tortuga.cpp where I...
|
by: stenasc |
last post by:
Hi,
In a file test1.c, in have the two functions f1 and f2. These are
declared in the header file t1.h. I want to call these functions in
another file subprog.c, which contains the function...
|
by: teddysnips |
last post by:
Weird.
I have taken over responsibility for a legacy application, Access 2k3,
split FE/BE.
The client has reported a problem and I'm investigating. I didn't
write the application.
The...
|
by: icarus |
last post by:
global_vars.py has the global variables
set_var.py changes one of the values on the global variables (don't
close it or terminate)
get_var.py retrieves the recently value changed (triggered right...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |