473,324 Members | 2,417 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,324 software developers and data experts.

How likely is a function starting with an underscore to cause undefined behavior?

I know that functions starting with an underscore, or two underscores, are
reserved by the compiler/c++ and should not be used by the user and may
cause undefined behavior. My question is, how likely is it to actually
cause undefined behavior?

The reason I'm asking is I'm using a game engine where the sockets code is
not working correctly on my computer, but seems to work correctly on
everyone elses. I am not compiling the dll myself, but using the same one
that everyone else is using.

Well, the developer of the engine has released the one .cpp file that
contains the socket code, and I see he is using a number of functions
preceeded by an underscore. I have posted requesting that he rename the
functions to remove the underscore explaining that it is reserved.

But I want to know if I should keep looking in the code to try to find the
problem, or if him renaming the functions might fix it. Unfortunately, I
only have one file from the .dll so I can't recompile it to test, and since
the problem doesn't happen on his machine, he can't test. Not the best
debugging scenario around for sure.

Has anyone ever experienced any undefined behavior being caused by a
function name being preceeded by an underscore or two? Most of his actually
have 2 underscores in front such as:
int __get_free_ws_player()
Mar 20 '06 #1
16 3342
Jim Langston wrote:
I know that functions starting with an underscore, or two underscores, are
reserved by the compiler/c++ and should not be used by the user and may
cause undefined behavior.
Actually, it's identifiers starting with an underscore follwed by an
upper case letter, or starting with two underscores.
My question is, how likely is it to actually
cause undefined behavior?
How likely? That's undefined. One possible outcome of undefined is
"exactly what you expected".
The reason I'm asking is I'm using a game engine where the sockets code is
not working correctly on my computer, but seems to work correctly on
everyone elses. I am not compiling the dll myself, but using the same one
that everyone else is using.


Well, it's possible that it will do what you expected on one platform,
and do something unexpected on another platform. Platform is usually
considered to be the compiler and libraries used. I suppose it's
possible for it to cause problems at runtime. It is undefined.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 20 '06 #2
Ben Pope wrote:
Jim Langston wrote:
I know that functions starting with an underscore, or two underscores, are
reserved by the compiler/c++ and should not be used by the user and may
cause undefined behavior.


Actually, it's identifiers starting with an underscore follwed by an
upper case letter, or starting with two underscores.


Strictly speaking, a leading underscore followed by a digit or
lower-case letter is legal in your own namespaces (but never in the
global namespace or ::std): 17.4.3.1.2/1 [lib.global.names].
However, articles in the comp.std.c++ newsgroup have warned that some
compilers do improperly use such identifiers for their own purposes.

My advice is to steer clear of using leading underscore names
completely. It doesn't add benifits to your code, and it makes your
code less portable.

A lose-lose senerio.

Mar 20 '06 #3
On 20 Mar 2006 10:18:44 -0800, "Axter" <go****@axter.com> wrote:
However, articles in the comp.std.c++ newsgroup have warned that some
compilers do improperly use such identifiers for their own purposes.
Improperly? What about section 17.4.3.1.2?
My advice is to steer clear of using leading underscore names
completely. It doesn't add benifits to your code, and it makes your
code less portable.


And it is in violation of section 17.4.3.1.2.

--
Bob Hairgrove
No**********@Home.com
Mar 20 '06 #4
Axter wrote:
A lose-lose senerio.


A reminder: We treat the term "undefined behavior" as unholy and unclean. We
should work to a Sane Subset that stays so far within defined behavior that
its definity is totally obvious.

Undefined behavior could mean anything, from the program working correctly,
to compiler diagnostic (likely for some _ abuse), to the nearest toilet
exploding.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 20 '06 #5
Ben Pope <be***************@gmail.com> wrote:
Jim Langston wrote:
I know that functions starting with an underscore, or two
underscores, are reserved by the compiler/c++ and should not be used
by the user and may cause undefined behavior.


Actually, it's identifiers starting with an underscore follwed by an
upper case letter, or starting with two underscores.


Hmm, I vaguely remember that *any* identifier with two consecutive
underscores is reserved .. ?

regards
--
jb

(reply address in rot13, unscramble first)
Mar 20 '06 #6
Jakob Bieling wrote:
Ben Pope <be***************@gmail.com> wrote:
Jim Langston wrote:
I know that functions starting with an underscore, or two
underscores, are reserved by the compiler/c++ and should not be used
by the user and may cause undefined behavior.

Actually, it's identifiers starting with an underscore follwed by an
upper case letter, or starting with two underscores.


Hmm, I vaguely remember that *any* identifier with two consecutive
underscores is reserved .. ?


I dunno, maybe. I's ugly anyway, I don't do it.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 20 '06 #7
Ben Pope <be***************@gmail.com> wrote:
Jakob Bieling wrote:
Ben Pope <be***************@gmail.com> wrote:
Jim Langston wrote:
I know that functions starting with an underscore, or two
underscores, are reserved by the compiler/c++ and should not be
used by the user and may cause undefined behavior.
Actually, it's identifiers starting with an underscore follwed by an
upper case letter, or starting with two underscores.


Hmm, I vaguely remember that *any* identifier with two
consecutive underscores is reserved .. ?


I dunno, maybe. I's ugly anyway, I don't do it.


Right, me neither. Just thought I'd mention it for others .. if it
is correct, but I think it is.

regards
--
jb

(reply address in rot13, unscramble first)
Mar 20 '06 #8
Jakob Bieling wrote:
Ben Pope <be***************@gmail.com> wrote:
Jakob Bieling wrote:
Ben Pope <be***************@gmail.com> wrote:

Actually, it's identifiers starting with an underscore follwed
by an upper case letter, or starting with two underscores.

Hmm, I vaguely remember that *any* identifier with two
consecutive underscores is reserved .. ?


I dunno, maybe. I's ugly anyway, I don't do it.


Right, me neither. Just thought I'd mention it for others .. if it is
correct, but I think it is.


I think you're right.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 20 '06 #9

Ben Pope wrote:
Jakob Bieling wrote:
Ben Pope <be***************@gmail.com> wrote:
Jakob Bieling wrote:
Hmm, I vaguely remember that *any* identifier with two
consecutive underscores is reserved .. ?

I dunno, maybe. I's ugly anyway, I don't do it.


Right, me neither. Just thought I'd mention it for others .. if it is
correct, but I think it is.


I think you're right.


I know he's right :-)
17.4.3.1.2
Each name that contains a double underscore (_ _) or begins with an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.

Gavin Deane

Mar 20 '06 #10

Gavin Deane wrote:
Ben Pope wrote:
Jakob Bieling wrote:
Ben Pope <be***************@gmail.com> wrote:
> Jakob Bieling wrote:
>> Hmm, I vaguely remember that *any* identifier with two
>> consecutive underscores is reserved .. ?
>
> I dunno, maybe. I's ugly anyway, I don't do it.

Right, me neither. Just thought I'd mention it for others .. if it is
correct, but I think it is.


I think you're right.


I know he's right :-)
17.4.3.1.2
Each name that contains a double underscore (_ _) or begins with an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.


That is not complete.
Here's the full section pulled from the official C++ standard:
************************************************** ************************************************
17.4.3.1.2 Global Names
1 Certain sets of names and functions signature are always reserved to
the implementation:
-- Each name that contains a double underscore (_ _) or begins with
an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.
-- Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace.
************************************************** ************************************************

Notice the last paragraph is talking about any name that begins with an
underscore.

If you have a draft version of the standard, it may or may not have the
full definition.
I recommend you get the official standard for a complete discription.

Mar 20 '06 #11
Jim Langston wrote:

[snip]
Has anyone ever experienced any undefined behavior being caused by a
function name being preceeded by an underscore or two?
Not me.
Most of his actually have 2 underscores in front such as:
int __get_free_ws_player()


You could write a little test program that uses exactly those function names
from his file and see if the resulting undefined behavior matches your
expectations. This might give you a clue as to wether your implementation
is actually using those names for something hidden.
Best

Kai-Uwe Bux
Mar 20 '06 #12

Jim Langston wrote:
I know that functions starting with an underscore, or two underscores, are
reserved by the compiler/c++ and should not be used by the user and may
cause undefined behavior. My question is, how likely is it to actually
cause undefined behavior?


Well, assuming you are declaring a name with an underscore at such a
point where it is reserved for implementation use then your program is
ill-formed. That being the case the behavior of this program, if it
even compiles, is ALWAYS undefined.

What you mean is how likely is this to cause undesired behavior? Not
very. Your program either compiles or it doesn't. A name resolution
conflict is going to result in a compilation error; your linker
wouldn't accept the library either. You need to keep looking for the
problem.

Mar 20 '06 #13

Axter wrote:
Gavin Deane wrote:
Ben Pope wrote:
Jakob Bieling wrote:
> Ben Pope <be***************@gmail.com> wrote:
>> Jakob Bieling wrote:
>>> Hmm, I vaguely remember that *any* identifier with two
>>> consecutive underscores is reserved .. ?
>>
>> I dunno, maybe. I's ugly anyway, I don't do it.
>
> Right, me neither. Just thought I'd mention it for others .. if it is
> correct, but I think it is.

I think you're right.
I know he's right :-)
17.4.3.1.2
Each name that contains a double underscore (_ _) or begins with an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.


That is not complete.
Here's the full section pulled from the official C++ standard:
************************************************** ************************************************
17.4.3.1.2 Global Names
1 Certain sets of names and functions signature are always reserved to
the implementation:
-- Each name that contains a double underscore (_ _) or begins with
an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.
-- Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace.
************************************************** ************************************************

Notice the last paragraph is talking about any name that begins with an
underscore.


The context I retained in my post was only discussing whether double
underscores were definitely reserved for the implementation or not. So
I quoted the part of the standard relevant to that discussion. Perhaps
I could have made it more clear that I wasn't quoting the entirety of
17.4.3.1.2
If you have a draft version of the standard, it may or may not have the
full definition.
I recommend you get the official standard for a complete discription.


My quote came form the 1998 standard. I haven't got the 2003 version
yet, but I'd be surprised if this is an area that has changed.

Gavin Deane

Mar 21 '06 #14
Jim Langston wrote:
Has anyone ever experienced any undefined behavior being caused by a
function name being preceeded by an underscore or two?


Yes, and it was very tricky as the name was used as a macro by the
implementation.

HTH,
Michiel Salters

Mar 21 '06 #15

Noah Roberts wrote:
Jim Langston wrote:
I know that functions starting with an underscore, or two underscores, are
reserved by the compiler/c++ and should not be used by the user and may
cause undefined behavior. My question is, how likely is it to actually
cause undefined behavior?


Well, assuming you are declaring a name with an underscore at such a
point where it is reserved for implementation use then your program is
ill-formed. That being the case the behavior of this program, if it
even compiles, is ALWAYS undefined.

What you mean is how likely is this to cause undesired behavior? Not
very. Your program either compiles or it doesn't. A name resolution
conflict is going to result in a compilation error; your linker
wouldn't accept the library either. You need to keep looking for the
problem.


I have seen example code that shows under the right condition, you will
not get a compile error if you're using variable names reserved for the
implementation.
It would silently compile your code, with no compile errors, but the
compiled code would not result in intended logic.

Mar 21 '06 #16
Bob Hairgrove wrote:
On 20 Mar 2006 10:18:44 -0800, "Axter" <go****@axter.com> wrote:
However, articles in the comp.std.c++ newsgroup have warned that some
compilers do improperly use such identifiers for their own purposes.


Improperly? What about section 17.4.3.1.2?


He is right. He's referring to non global namespaces.
Those implementations that use identifiers with leading underscore
followed by lowercase or digit in a non global non std namespace do it
improperly.
My advice is to steer clear of using leading underscore names
completely. It doesn't add benifits to your code, and it makes your
code less portable.


And it is in violation of section 17.4.3.1.2.


Not.

namespace my {

void _foo(); // legal

};
Mar 22 '06 #17

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

Similar topics

10
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
6
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
102
by: tom fredriksen | last post by:
Hi I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to...
33
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
11
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
8
by: vaib | last post by:
hi all , It really seems that C never ceases to amaze . All this time i've been doing C and i thought i was quite adept at it but i was wrong . So without wasting any more time , here's the...
49
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: 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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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....

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.