473,386 Members | 1,748 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,386 software developers and data experts.

global variable name conflict with standard header

Hello all
I would like to give a certain name to a certain global variable. Unfortunately,
this name is already used in math.h for a mathematical function. Worse, I do
need to use maths library and therefore have to include math.h. Thus my compiler
reports conflicting declarations of the same name.
Does anyone know any reasonably elegant way to resolve this conflict? I
understand that I can give up and choose another name, but somehow not happy
with that.
Any advice would be much appreciated!
- V.

Nov 14 '05 #1
16 3566
Vadim Biktashev wrote on 11/08/04 :
I would like to give a certain name to a certain global variable.
Unfortunately, this name is already used in math.h for a mathematical
function. Worse, I do need to use maths library and therefore have to include
math.h. Thus my compiler reports conflicting declarations of the same name.
Does anyone know any reasonably elegant way to resolve this conflict? I
understand that I can give up and choose another name, but somehow not happy
with that.


No way, you have to change the name. The usual trick is to add some
meaningful prefix

PREFIX_identifier

(BTW, it's not a C question)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
Vadim Biktashev wrote on 11/08/04 :
I would like to give a certain name to a certain global variable.
Unfortunately, this name is already used in math.h for a mathematical
function. Worse, I do need to use maths library and therefore have to include
math.h. Thus my compiler reports conflicting declarations of the same name.
Does anyone know any reasonably elegant way to resolve this conflict? I
understand that I can give up and choose another name, but somehow not happy
with that.


No way, you have to change the name. The usual trick is to add some
meaningful prefix

PREFIX_identifier

(BTW, it's not a C question)


Yes, it is: you cannot have two conflicting declarations of the same
identifier, hence the problem. Of course, the answer is "don't do that,
then", but that doesn't make the question off-topic.

Richard
Nov 14 '05 #3
Richard Bos wrote:
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:

Vadim Biktashev wrote on 11/08/04 :
I would like to give a certain name to a certain global variable.
Unfortunately, this name is already used in math.h for a mathematical
function. Worse, I do need to use maths library and therefore have to include
math.h. Thus my compiler reports conflicting declarations of the same name.
Does anyone know any reasonably elegant way to resolve this conflict? I
understand that I can give up and choose another name, but somehow not happy
with that.


No way, you have to change the name. The usual trick is to add some
meaningful prefix

PREFIX_identifier

(BTW, it's not a C question)

Yes, it is: you cannot have two conflicting declarations of the same
identifier, hence the problem. Of course, the answer is "don't do that,
then", but that doesn't make the question off-topic.

Richard

Perhaps, I was not precise enough.

The question is: is there any way to cancel a definition/declaration made
earlier, apart from opening a block and making a new definition local in that
block. My naive logic was, if it is possible for macros, why not make a similar
thing for regular C names.

- Vadim
BTW if this is really not for this newsgroup, perhaps a kind soul could direct
me to the right one?

Nov 14 '05 #4
Richard Bos wrote on 11/08/04 :
(BTW, it's not a C question)


Yes, it is: you cannot have two conflicting declarations of the same
identifier, hence the problem. Of course, the answer is "don't do that,
then", but that doesn't make the question off-topic.


Ok. I meant that it's not specific to C.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #5
Vadim Biktashev wrote:

[Wants to give a global variable the same identifier as
a Standard library function]

The question is: is there any way to cancel a definition/declaration
made earlier, apart from opening a block and making a new definition
local in that block. My naive logic was, if it is possible for macros,
why not make a similar thing for regular C names.


Because macro identifiers have no linkage.

"Linkage" is the magic that takes an identifier that
appears in several different scopes and makes all those
different appearances refer to the same object or function:

/* file1.c */
int myValue = 42;
...

/* file2.c */
void func(void) {
extern int myValue;
...
}

Here we have two occurrences of the identifier `myValue'
each with its own scope. The scope of the first covers
all of file1.c from the point of declaration to the end.
The scope of the second is limited to the body of func();
the identifier is not "visible" outside the block.

But when the two files are combined into a single
program, both `myValue' identifiers are made to refer to
the same `int' variable. This process of coalescing
identically-named externally-linked identifiers into a
single entity is driven entirely by their names: If two
externally-linked identifiers have the same spelling, they
are combined to refer to the same object or function.

... which means that if you try to use the same
externally-linked name for two or more different things,
the association of "name" to "thing" breaks down. The
process of linking identifiers together depends on those
identifiers being unique[*], and if they aren't ... The
linker will have no way to resolve the conflict between
"different" identically-named identifiers, and Something
Bad will happen. (Some linkers will detect the problem,
some will detect it only in certain circumstances, and
some will not detect it at all but will give you a program
that doesn't do what you want.)
[*] For externally-linked identifiers there are even
stronger restrictions: Some linkers may consider only the
first N characters of an identifier, and some may treat
upper- and lower-case letters as identical. The details
of these additional restrictions depend on which Standard
("C89/90" or "C99") your implementation conforms to.

--
Er*********@sun.com

Nov 14 '05 #6
Emmanuel Delahaye wrote:
Vadim Biktashev wrote on 11/08/04 :
I would like to give a certain name to a certain global variable.
Unfortunately, this name is already used in math.h for a
mathematical function. Worse, I do need to use maths library and
therefore have to include math.h. Thus my compiler reports
conflicting declarations of the same name. Does anyone know any
reasonably elegant way to resolve this conflict? I understand
that I can give up and choose another name, but somehow not
happy with that.


No way, you have to change the name. The usual trick is to add
some meaningful prefix

PREFIX_identifier

(BTW, it's not a C question)


Yes it is. The standard specifically forbids reusing any
identifier exported from any standard include file used, under
penalty of undefined behavior.

The underlying reason is that something in the library may be of
the form:

foo(fee)

and get used by another library portion that calls foo. If the
name has been redefined somehow, that call goes to the wrong
place. For example, if you #include <stdlib.h> and write your own
routine malloc(), the library routine for malloc may not get
loaded. However the realloc() routine may well call the original
malloc, and if rerouted its assumptions are no longer valid. All
of these may be called behind your back in the initialization
code.

So don't do that.

--
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"A man who is right every time is not likely to do very much."
- Francis Crick, co-discover of DNA

Nov 14 '05 #7

On Wed, 11 Aug 2004, CBFalconer wrote:

Emmanuel Delahaye wrote:
Vadim Biktashev wrote on 11/08/04 :
I would like to give a certain name to a certain global variable.
Unfortunately, this name is already used in math.h for a
mathematical function.
[...] (BTW, it's not a C question)


Yes it is. The standard specifically forbids reusing any
identifier exported from any standard include file used, under
penalty of undefined behavior.

The underlying reason is that something in the library may be of
the form:

foo(fee)

and get used by another library portion that calls foo. If the
name has been redefined somehow, that call goes to the wrong
place. For example, if you #include <stdlib.h> and write your own
routine malloc(), the library routine for malloc may not get
loaded. However the realloc() routine may well call the original
malloc, and if rerouted its assumptions are no longer valid. All
of these may be called behind your back in the initialization
code.


Amplification: It's not just the library-linkage problem. We
have sentences in the Standard that say explicitly things like

[#3] The implementation shall behave as if no library
function calls the rand function.

and yet it's /still/ UB to define 'extern int rand;'. This may be
for the benefit of dumb optimizers who can now inline any standard
library routine they like, without checking symbol tables or whatnot.
So for instance GCC can convert calls to 'memcpy' into inline
assembler, even if you've already defined

double memcpy(double m); // Copy the magnitude of exp^m

somewhere else.

-Arthur
Nov 14 '05 #8
Arthur J. O'Dwyer wrote:

Amplification: It's not just the library-linkage problem. We
have sentences in the Standard that say explicitly things like

[#3] The implementation shall behave as if no library
function calls the rand function.

and yet it's /still/ UB to define 'extern int rand;'. This may be
for the benefit of dumb optimizers who can now inline any standard
library routine they like, without checking symbol tables or whatnot.
[...]


Another reason is that the library implementation may
need "private" or "internal" interfaces to do its job. This
is most not likely the case with rand(), but is almost certainly
so in other areas. For example, exit() must close all open
streams, implying that there must be some kind of sub rosa
information sharing with fopen() and fclose().

The memory management functions are perhaps the "most
overridden" in the Standard library, because replacing them
can be so helpful in detecting certain kinds of bugs. No
one would be foolish enough to replace malloc() without also
replacing calloc() and realloc() and free(), but even if you
carefully replace all four of them, what are you going to do
about the private and undocumented _allocate_aligned() function,
used by fopen() to allocate stream buffers? The real fun starts
when fclose() calls the replacement free() to release a buffer
that the associated malloc() has never heard of ...

The Standard is often described as a contract between the
implementor and the user. Another word might be "frontier:"
the programmer stays on *this* side and the implementor stays
on *that* side, and all will be well. Just as the implementor
is forbidden to intrude on the user's turf by, say, adding
extraneous declarations to <stdlib.h>, the user is forbidden
to trespass on the implementor's turf by redefining longjmp().
C's border guards carry UBs instead of Uzis, but they can be
equally deadly.

--
Er*********@sun.com

Nov 14 '05 #9
Vadim Biktashev wrote:

Hello all
I would like to give a certain name to a certain global variable. Any advice would be much appreciated!


The first question that comes to my mind is,
is that certain name, a reserved identifier?

(In other words, "Which name?")

--
pete
Nov 14 '05 #10
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
Richard Bos wrote on 11/08/04 :
(BTW, it's not a C question)


Yes, it is: you cannot have two conflicting declarations of the same
identifier, hence the problem. Of course, the answer is "don't do that,
then", but that doesn't make the question off-topic.


Ok. I meant that it's not specific to C.


Not completely, but it's not a completely general problem, either. Many
languages have ways to specify the namespace of an identifier, allowing
you to do what the OP wants to do. C does not. It's true that C isn't
the only language which doesn't; but that alone doesn't make it
off-topic.

Richard
Nov 14 '05 #11
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
On Wed, 11 Aug 2004, CBFalconer wrote:
place. For example, if you #include <stdlib.h> and write your own
routine malloc(), the library routine for malloc may not get
loaded. However the realloc() routine may well call the original
malloc, and if rerouted its assumptions are no longer valid. All
of these may be called behind your back in the initialization
code.


Amplification: It's not just the library-linkage problem. We
have sentences in the Standard that say explicitly things like

[#3] The implementation shall behave as if no library
function calls the rand function.

and yet it's /still/ UB to define 'extern int rand;'. This may be
for the benefit of dumb optimizers who can now inline any standard
library routine they like, without checking symbol tables or whatnot.


Not just that. It would be allowed, for example, to
- save the state of rand();
- call rand() to get a random number;
- restore the previous state.
If the programmer can replace rand(), this is no longer possible.

Of course, this is hardly likely to be useful with rand() itself; but it
is a possibility.

Richard
Nov 14 '05 #12
Richard Bos wrote:
.... snip ...
Not completely, but it's not a completely general problem, either.
Many languages have ways to specify the namespace of an identifier,
allowing you to do what the OP wants to do. C does not. It's true
that C isn't the only language which doesn't; but that alone
doesn't make it off-topic.


Yes, C does have namespaces. To grossly simplify, anything
starting with a '_' is in system space, and most ordinary
identifiers are in user space. They just aren't named
namespaces. struct and enum tags are another area.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #13

"pete" <pf*****@mindspring.com> wrote
The first question that comes to my mind is,
is that certain name, a reserved identifier?

(In other words, "Which name?")

It would be a sin to tell you.
Nov 14 '05 #14
Malcolm wrote:

"pete" <pf*****@mindspring.com> wrote
The first question that comes to my mind is,
is that certain name, a reserved identifier?

(In other words, "Which name?")

It would be a sin to tell you.


Does it start with an underscore ?
Nov 14 '05 #15
Malcolm wrote:
"pete" <pf*****@mindspring.com> wrote
The first question that comes to my mind is,
is that certain name, a reserved identifier?

(In other words, "Which name?")


It would be a sin to tell you.


And cos we'd have to tan your floor and say pow!
Nov 14 '05 #16
On Thu, 12 Aug 2004 19:15:56 +0100, "Malcolm"
<ma*****@55bank.freeserve.co.uk> wrote:

"pete" <pf*****@mindspring.com> wrote
The first question that comes to my mind is,
is that certain name, a reserved identifier?

(In other words, "Which name?")

It would be a sin to tell you.

Just be cos?

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
12
by: Santiago de Compostela | last post by:
Hi The following program doesn't compile on MS VC++ or Bloodshed Dev-C++ #include <iostream> int strlen(const char *in) {
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
1
by: Jaco Naude | last post by:
Hi, I'm using a static library in my application which links fine except for a few global variables. The static library only contains a bunch of .cpp and .h files and the global variables are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.