Sponsored Content
Full Discussion: Static variables in C++
Top Forums Programming Static variables in C++ Post 302741087 by Don Cragun on Friday 7th of December 2012 11:18:35 AM
Old 12-07-2012
Quote:
Originally Posted by JohnGraham
Wish granted: Your mildly cryptic and explanation-devoid statement simply compels me to ask, what part of the above do you think may be associated with undefined behaviour?

I have to say, posting such a statement only seems designed to confuse the original poster without explaining much...
If the application is assuming that based on the location in memory of dl (&dl) it "knows" the address of df or du and makes use of that knowledge to get the value of df or to store a value into du that application will produce undefined results. This is true even though there is a good chance that with a little testing the user can figure out how a given compiler will arrange the locations of the integers declared by:
Code:
static int  df, dl, du, count;

or by:
Code:
static int  df;
static int  dl;
static int  du;
static int  count;

There is probably a much better than 50% chance that the two above ways of declaring these variables will result in them being allocated in the same positions relative to each other, but there is a non-zero percent chance that some compilers would allocate them in reverse order (or alphabetic order by name or some other order) in memory.

Any application that makes an assumption like this is depending on undefined behavior. As Corona688 said:
Quote:
Without seeing what he's done, it's hard to explain what he's done.
Is it possible that an assumption like this could change the behavior of a section of code? Absolutely.
Is it likely that an assumption like this would change the behavior of a section of code? Who knows?

Last edited by Don Cragun; 12-07-2012 at 12:20 PM.. Reason: fixed typo
 

9 More Discussions You Might Find Interesting

1. IP Networking

Static IP

Is it possible, and if so, how can I assign a static IP address to my RedHat 8.0 machine. I am using a Linksys router/switch with DHCP enabled. I have a small linux/windows2000 LAN at home. I want to assign a static IP so that I can setup port forwarding to my linux machine so that I can ssh into... (7 Replies)
Discussion started by: google
7 Replies

2. Programming

Static

What does this command exactly do cc -static example.c (6 Replies)
Discussion started by: wojtyla
6 Replies

3. Programming

Static variables memory allocation

Hi I want to know when and where memory for static variables are allocated in a C program. If it allocates during compilation will memory be allocated for the variable "i" during compilation itself. int count(); int main(){ printf("%d", count()); return 0; } int count() { ... (8 Replies)
Discussion started by: nathanmca
8 Replies

4. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

5. Shell Programming and Scripting

How to static the average value

Hi all, Sorry I make a mistake,the title should be "How to statistic the average value " I do five times about "mv 123 to 456" and do five times about "mv 456 to 123" As we know,"time" can get the real usr sys value, I want to get the average real,usr,sys of "mv 123 to 456" and "mv 456 to... (4 Replies)
Discussion started by: yanglei_fage
4 Replies

6. IP Networking

I need HELP to Set up Coyote Linux router with 1 static IP & 64 internal static IP

hello, i need help on setting my coyote linux, i've working on this for last 5 days, can't get it to work. I've been posting this message to coyote forum, and other linux forum, but haven't get any answer yet. Hope someone here can help me...... please see my attached picture first. ... (0 Replies)
Discussion started by: dlwoaud
0 Replies

7. Linux

Could static library include static library?

I have some static library(libxxx.a libyyy.a). And I want to generate my library(libzzz.a), libzzz.a will use libxxx.a and libyyy.a I wan't my application only use libzzz.a, (means libzzz.a had include libxxx.a, libyyy.a), how can I do that? Thank you. example: I have zzz.c. I do ... (4 Replies)
Discussion started by: freemagic
4 Replies

8. Programming

Even the Static cURL Library Isn't Static

I'm writing a program which uses curl to be run on Linux PCs which will be used by a number of different users. I cannot make the users all install curl on their individual machines, so I have tried to link curl in statically, rather than using libcurl.so. I downloaded the source and created a... (8 Replies)
Discussion started by: BrandonShw
8 Replies

9. Programming

GDB problem accessing static variables in C

Hi, Can anyone explain this please..... This is on AIX with GDB (tried a few versions). It only happens when program compiled in 64 bit and only with static variables.... A simple test program... ### snip #include <stdio.h> main() { static int n; n = 6; printf("hello %d\n", n);... (0 Replies)
Discussion started by: bagpussnz
0 Replies
SoAnnoText3CharOrientElement(3) 				       Coin					   SoAnnoText3CharOrientElement(3)

NAME
SoAnnoText3CharOrientElement - The SoAnnoText3CharOrientElement class is yet to be documented. FIXME: write doc. SYNOPSIS
#include <Inventor/elements/SoAnnoText3CharOrientElement.h> Inherits SoInt32Element. Public Member Functions virtual void init (SoState *state) FIXME: write doc. Static Public Member Functions static SoType getClassTypeId (void) static int getClassStackIndex (void) static void * createInstance (void) static void initClass (void) static void set (SoState *const state, SbBool isOriented) FIXME: write doc. static SbBool get (SoState *state) FIXME: write doc. static SbBool getDefault (void) FIXME: write doc. Protected Member Functions virtual ~SoAnnoText3CharOrientElement () Additional Inherited Members Detailed Description The SoAnnoText3CharOrientElement class is yet to be documented. FIXME: write doc. Constructor &; Destructor Documentation SoAnnoText3CharOrientElement::~SoAnnoText3CharOrientElement (void) [protected], [virtual] The destructor. Member Function Documentation SoType SoAnnoText3CharOrientElement::getClassTypeId (void) [static] This static method returns the class type. Reimplemented from SoInt32Element. int SoAnnoText3CharOrientElement::getClassStackIndex (void) [static] This static method returns the state stack index for the class. Reimplemented from SoInt32Element. void * SoAnnoText3CharOrientElement::createInstance (void) [static] This API member is considered internal to the library, as it is not likely to be of interest to the application programmer. void SoAnnoText3CharOrientElement::initClass (void) [static] This static method initializes static data for the SoAnnoText3CharOrientElement class. Reimplemented from SoInt32Element. Author Generated automatically by Doxygen for Coin from the source code. Version 3.1.3 Wed May 23 2012 SoAnnoText3CharOrientElement(3)
All times are GMT -4. The time now is 02:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy