473,462 Members | 1,128 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Standards compliance

In one of the C compilers I use if I declare a static variable
in a translation unit thus:
static short x;

to which there is no subsquent references to x anywhere within the
translation unit, I receive a warning stating that there are no
references to x. That is fine, I realise that the compiler is
free to issue whatever warning messages it pleases.

However, the compiler also suppresses generation of x's "space"
in the resulting executable.

Is this permitted by the Standard (89 or 99)?

Placing a
static char[] version = "version 1.2";
is useful when looking at a dump of a file to see what the version
string has in it, but if this information is suppressed from the
executable because there is no reference to the static var
/version/ then I have to resort to crude means to create an
"artificial" reference to /version/.

Is my compiler broken?

Oz
Nov 13 '05 #1
5 1417
oz*****@yahoo.com (ozbear) writes:
In one of the C compilers I use if I declare a static variable
in a translation unit thus:
static short x;

to which there is no subsquent references to x anywhere within the
translation unit, I receive a warning stating that there are no
references to x. That is fine, I realise that the compiler is
free to issue whatever warning messages it pleases.

However, the compiler also suppresses generation of x's "space"
in the resulting executable.

Is this permitted by the Standard (89 or 99)?
Yes. Since a strictly conforming program has no way to find out if space
is reserved for an object that is never referenced, the implementation is
free not reserve the space.
Placing a
static char[] version = "version 1.2";
is useful when looking at a dump of a file to see what the version
string has in it,


This is a kludge, which is not even guaranteed to work, as you've found
out yourself.

<OT>
The correct solution is to embed the version string in a special section
of the object file, instead of hiding it in a data section. Many
implementations provide a way to do the former.
</OT>

Martin
Nov 13 '05 #2
In <3fc46517.326668140@news-server> oz*****@yahoo.com (ozbear) writes:
Placing a
static char[] version = "version 1.2";
is useful when looking at a dump of a file to see what the version
It actually renders your code invalid and very unlikely to compile at all.
string has in it, but if this information is suppressed from the
executable because there is no reference to the static var
/version/ then I have to resort to crude means to create an
"artificial" reference to /version/.


Which can be trivially avoided by not making the array static. If it has
external linkage, the compiler can no longer perform the optimisation.
To avoid name conflicts, give the array the name of the source file (minus
the .c extension):

char mylib[] = "mylib version 1.2";

for the file mylib.c. So, even when looking at a dump of the whole
executable, you'll know that it is mylib.c that was version 1.2, while
main.c might be still at version 0.64. In theory, the linker could
optimise the array away, but in practice it is highly uncommon.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3
In <3fc46517.326668140@news-server> oz*****@yahoo.com (ozbear) writes:
In one of the C compilers I use if I declare a static variable
in a translation unit thus:
static short x;

to which there is no subsquent references to x anywhere within the
translation unit, I receive a warning stating that there are no
references to x. That is fine, I realise that the compiler is
free to issue whatever warning messages it pleases.

However, the compiler also suppresses generation of x's "space"
in the resulting executable.

Is this permitted by the Standard (89 or 99)?


Yes, by the as-if rule: this optimisation does not affect the observable
behaviour of your program, as defined in terms of the abstract C machine.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4
On 26 Nov 2003 12:54:25 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <3fc46517.326668140@news-server> oz*****@yahoo.com (ozbear) writes:
Placing a
static char[] version = "version 1.2";
is useful when looking at a dump of a file to see what the version


It actually renders your code invalid and very unlikely to compile at all.
string has in it, but if this information is suppressed from the
executable because there is no reference to the static var
/version/ then I have to resort to crude means to create an
"artificial" reference to /version/.


Which can be trivially avoided by not making the array static. If it has
external linkage, the compiler can no longer perform the optimisation.
To avoid name conflicts, give the array the name of the source file (minus
the .c extension):

char mylib[] = "mylib version 1.2";

for the file mylib.c. So, even when looking at a dump of the whole
executable, you'll know that it is mylib.c that was version 1.2, while
main.c might be still at version 0.64. In theory, the linker could
optimise the array away, but in practice it is highly uncommon.

Oh dear...yes....I got the [] in the wrong place in my post.

Thanks for the suggestion on using the source file name as the
variable name.

Oz
Nov 13 '05 #5
In <3fc51cbf.373684140@news-server> oz*****@yahoo.com (ozbear) writes:
On 26 Nov 2003 12:54:25 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <3fc46517.326668140@news-server> oz*****@yahoo.com (ozbear) writes:
Placing a
static char[] version = "version 1.2";
is useful when looking at a dump of a file to see what the version


It actually renders your code invalid and very unlikely to compile at all.
string has in it, but if this information is suppressed from the
executable because there is no reference to the static var
/version/ then I have to resort to crude means to create an
"artificial" reference to /version/.


Which can be trivially avoided by not making the array static. If it has
external linkage, the compiler can no longer perform the optimisation.
To avoid name conflicts, give the array the name of the source file (minus
the .c extension):

char mylib[] = "mylib version 1.2";

for the file mylib.c. So, even when looking at a dump of the whole
executable, you'll know that it is mylib.c that was version 1.2, while
main.c might be still at version 0.64. In theory, the linker could
optimise the array away, but in practice it is highly uncommon.

Oh dear...yes....I got the [] in the wrong place in my post.

Thanks for the suggestion on using the source file name as the
variable name.


And if you make it const, there is also a chance that it will be allocated
in the text segment and not in the data segment (in case it makes any
difference to you).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

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

Similar topics

162
by: Isaac Grover | last post by:
Hi everyone, Just out of curiosity I recently pointed one of my hand-typed pages at the W3 Validator, and my hand-typed code was just ripped to shreds. Then I pointed some major sites...
3
by: CJM | last post by:
On company in our group is looking at getting their rather basic website redeveloped externally. In typical fashion, they have declared that this project it 'marketing-led' rather than 'IT-led',...
3
by: Jules | last post by:
So with MS looking towards Longhorn Indigo messaging, for Service based Applications Integration, its brief dalience with adoption of industry wide Web Service standards is over. This is a major...
9
by: christopher diggins | last post by:
I would like some feedback if the follow code is standard compliant, and are there any obvious redundancies or inefficiencies? The purpose of the code is to emulate the behaviour of a struct FuBar...
17
by: Ian | last post by:
Hi there, Can anybody tell me where I can find a standards documents like you have in c#. I am trying to write javascript and would like to know what standards are i.e. Where to put the...
2
by: John Doe | last post by:
I have a webpage which shows and hides divs as a method of navigation. It works great in the ever evil IE. It works not so great (read not at all) in any standards compliant browser. I haven't...
23
by: Mario T. Lanza | last post by:
I have been authoring web sites for several years now and recently come to value web standards (as touted by Zeldman and many other web gurus). I have noticed with frustration that there are so...
22
by: Roy Schestowitz | last post by:
Paul Thurrott: "Wilson's (IE Lead Program Manager) post is disappointing because Microsoft doesn't plan to fully support the latest CSS standard in IE 7.0..." ...
2
by: Mr TVTL | last post by:
Hi there, I've created a series of layouts designed to be optimized for search engines, and yet also standards-compliant. The problem isn't in the layouts themselves, but in the display of the...
22
by: Harry Haller | last post by:
Is there any other way apart from: document.compatMode == 'CSS1Compat' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.