Static variables in C++


 
Thread Tools Search this Thread
Top Forums Programming Static variables in C++
# 1  
Old 12-06-2012
Static variables in C++

Are the following equivalent?

Code:
  static int  df, dl, du, count;

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

# 2  
Old 12-06-2012
Unless you're relying on undefined behavior, then yes, absolutely.
# 3  
Old 12-07-2012
Quote:
Originally Posted by Corona688
Unless you're relying on undefined behavior, then yes, absolutely.
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...

Last edited by JohnGraham; 12-07-2012 at 08:03 AM..
# 4  
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?
That he asks the question implies he may have found a difference and is seeking to explain why. Without seeing what he's done, it's hard to explain what he's done.

Or I may be reading too much into it. I do that sometimes.

Last edited by Corona688; 12-07-2012 at 11:48 AM..
# 5  
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
# 6  
Old 12-07-2012
Quote:
Originally Posted by Don Cragun
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:

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?
Saying code like that "depend[s] on undefined behavior" is too kind. Anyone who writes code that assumes the location of a variable based on the location of another variable is bat-!*()% crazystupid.

FWIW, multiple definitions on one line are a bad idea for other reasons, too.

What's this mean:
Code:
char* ptrA, ptrB;

# 7  
Old 12-07-2012
Quote:
Originally Posted by achenle
Saying code like that "depend[s] on undefined behavior" is too kind. Anyone who writes code that assumes the location of a variable based on the location of another variable is bat-!*()% crazystupid.

FWIW, multiple definitions on one line are a bad idea for other reasons, too.

What's this mean:
Code:
char* ptrA, ptrB;

Code:
char* ptrA, ptrB;

declares an object of type pointer to char named ptrA and an object of type char named ptrB. It also means that I will never hire the programmer that wrote this. Either they don't understand the rules of C or C++, or they don't understand that you shouldn't name a variable ptrx unless that variable is declared to be a pointer type (unless they're entering some kind of code obfuscation contest).

There is nothing wrong with declaring multiple objects with a single declaration (whether or not it is on one line or spread across several lines).

When writing code, use a set of style guidelines and be consistent. And if the guidelines you use aren't "common practice", make sure that anyone who will ever need to maintain your code also has a pointer to a written set of the guidelines used.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. Programming

Static

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

9. 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
Login or Register to Ask a Question