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
Binding Helpers(3)					     Hardware Locality (hwloc)						Binding Helpers(3)

NAME
Binding Helpers - Functions static inline void hwloc_distributev (hwloc_topology_t topology, hwloc_obj_t *root, unsigned n_roots, hwloc_cpuset_t *cpuset, unsigned n, unsigned until) static inline void hwloc_distribute (hwloc_topology_t topology, hwloc_obj_t root, hwloc_cpuset_t *cpuset, unsigned n, unsigned until) static inline void * hwloc_alloc_membind_policy_nodeset (hwloc_topology_t topology, size_t len, hwloc_const_nodeset_t nodeset, hwloc_membind_policy_t policy, int flags) static inline void * hwloc_alloc_membind_policy (hwloc_topology_t topology, size_t len, hwloc_const_cpuset_t cpuset, hwloc_membind_policy_t policy, int flags) Detailed Description Function Documentation static inline void* hwloc_alloc_membind_policy (hwloc_topology_ttopology, size_tlen, hwloc_const_cpuset_tcpuset, hwloc_membind_policy_tpolicy, intflags) [static] Allocate some memory on the memory nodes near given cpuset cpuset. This is similar to hwloc_alloc_membind_policy_nodeset, but for a given cpuset. static inline void* hwloc_alloc_membind_policy_nodeset (hwloc_topology_ttopology, size_tlen, hwloc_const_nodeset_tnodeset, hwloc_membind_policy_tpolicy, intflags) [static] Allocate some memory on the given nodeset nodeset. This is similar to hwloc_alloc_membind except that it is allowed to change the current memory binding policy, thus providing more binding support, at the expense of changing the current state. static inline void hwloc_distribute (hwloc_topology_ttopology, hwloc_obj_troot, hwloc_cpuset_t *cpuset, unsignedn, unsigneduntil) [static] static inline void hwloc_distributev (hwloc_topology_ttopology, hwloc_obj_t *roots, unsignedn_roots, hwloc_cpuset_t *cpuset, unsignedn, unsigneduntil) [static] Distribute n items over the topology under root. Distribute n items over the topology under roots. Array cpuset will be filled with n cpusets recursively distributed linearly over the topology under root, down to depth until (which can be INT_MAX to distribute down to the finest level). This is typically useful when an application wants to distribute n threads over a machine, giving each of them as much private cache as possible and keeping them locally in number order. The caller may typically want to also call hwloc_bitmap_singlify() before binding a thread so that it does not move at all. Note: This function requires the root object to have a CPU set. This is the same as hwloc_distribute, but takes an array of roots instead of just one root. Note: This function requires the roots objects to have a CPU set. Author Generated automatically by Doxygen for Hardware Locality (hwloc) from the source code. Version 1.7 Sun Apr 7 2013 Binding Helpers(3)
All times are GMT -4. The time now is 06:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy