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

pre ANSI code and writable-strings?

char *s = "Hello";
s[0] = 'J';
puts(s);

might print "Jello" in a pre-ANSI compiler - is the behaviour of this
program undefined in any pre-ANSI compiler - or would it always have printed
"Jello" with a pre-ANSI compiler?
In gcc with the "writable-strings" option this program prints
Jello
If there were more than one semantics for what this progran did under a
pre-ANSI compiler, which semantics were chosen by gcc for the
"writable-strings" option, and for what reason?

Oct 12 '06 #1
7 4828
>char *s = "Hello";
>s[0] = 'J';
puts(s);

might print "Jello" in a pre-ANSI compiler - is the behaviour of this
program undefined in any pre-ANSI compiler - or would it always have printed
"Jello" with a pre-ANSI compiler?
Since there is no pre-ANSI C standard, anything a pre-ANSI compiler
does is undefined.
>In gcc with the "writable-strings" option this program prints
Jello
There are no "compiler options" in ANSI C, there are only different
implementations. The number of combinations of such compiler options
can make the number of implementations enormous. In this case,
both "writable-strings" and non-"writable-strings" can conform to
ANSI C.
>If there were more than one semantics for what this progran did under a
pre-ANSI compiler, which semantics were chosen by gcc for the
"writable-strings" option, and for what reason?
The behaviors I know of in pre-ANSI compilers are (a) string literals
were put in writable storage, or (b) string literals were put in
non-writable storage (which might be RAM with memory management
protecting it). If a write was attempted to non-writable storage,
the behavior likely fell into two classes: (b1) the write was
ignored, or (b2) the write attempt caused some kind of CPU trap
which aborted the program or got mapped into a signal. Which of
(b1) or (b2) happened usually depended on hardware.

Even some compilers that did not implement non-writable-strings
could be compiled with a more complex (and system-specific) procedure
involving xstr which would preprocess the C and generate a big array
with the strings in it, and change the program to refer to that
array. That array was made read-only by, er, implementation-specific
magic.

Oct 12 '06 #2
Gordon Burditt <go***********@burditt.orgwrote:
can make the number of implementations enormous. In this case,
both "writable-strings" and non-"writable-strings" can conform to
ANSI C.
But a program which relies on the behavior implied by the
"writable-strings" option is non-conforming.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 14 '06 #3
Christopher Benson-Manica wrote:
Gordon Burditt <go***********@burditt.orgwrote:
>can make the number of implementations enormous. In this case,
both "writable-strings" and non-"writable-strings" can conform
to ANSI C.

But a program which relies on the behavior implied by the
"writable-strings" option is non-conforming.
The program doesn't rely on it. The option prevents creating some
non-conforming programs without having a warning.

--
"The mere formulation of a problem is far more often essential
than its solution, which may be merely a matter of mathematical
or experimental skill. To raise new questions, new possibilities,
to regard old problems from a new angle requires creative
imagination and and marks real advances in science."
-- Albert Einstein

Oct 14 '06 #4
CBFalconer <cb********@yahoo.comwrites:
Christopher Benson-Manica wrote:
>Gordon Burditt <go***********@burditt.orgwrote:
>>can make the number of implementations enormous. In this case,
both "writable-strings" and non-"writable-strings" can conform
to ANSI C.

But a program which relies on the behavior implied by the
"writable-strings" option is non-conforming.

The program doesn't rely on it. The option prevents creating some
non-conforming programs without having a warning.
I think there's some confusion between two different options. gcc has
(had?) an option to make string literals writable, and another option
to warn about attempts to modify them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 14 '06 #5
>>can make the number of implementations enormous. In this case,
>>both "writable-strings" and non-"writable-strings" can conform
to ANSI C.

But a program which relies on the behavior implied by the
"writable-strings" option is non-conforming.
True. But a compiler which works either way is still allowed. A
program which depends on either behavior to the exclusion of the
other is non-conforming.
>The program doesn't rely on it. The option prevents creating some
non-conforming programs without having a warning.
I think this would be better stated as "the option prevents creating
some non-conforming programs without having a hard program crash
at runtime". If the compiler puts strings in write-protected memory
and write attempts cause program traps and aborts, that's what will
happen.

ANSI C doesn't define anything called a "warning", or make any
distinction between "warning" and "error".

Oct 15 '06 #6
Gordon Burditt <go***********@burditt.orgwrote:
But a program which relies on the behavior implied by the
"writable-strings" option is non-conforming.
True. But a compiler which works either way is still allowed. A
program which depends on either behavior to the exclusion of the
other is non-conforming.
Yes, although I'm having a difficult time imagining an otherwise
conforming program that relies on non-writable strings for its correct
operation, unless the correct operation of the program is to dump
core on attempting to modify a string literal.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 16 '06 #7
>But a program which relies on the behavior implied by the
>"writable-strings" option is non-conforming.
>True. But a compiler which works either way is still allowed. A
program which depends on either behavior to the exclusion of the
other is non-conforming.

Yes, although I'm having a difficult time imagining an otherwise
conforming program that relies on non-writable strings for its correct
operation, unless the correct operation of the program is to dump
core on attempting to modify a string literal.
Ok, here's sort of an example, and it depends on more than just
non-writable-strings:

A compiler that makes strings non-writable is allowed to combine
them, so, that, for example, "hello" and "ello" have addresses that
are 1 byte apart, and two occurrences of the same string literal
in the same compilation unit are guaranteed (by this particular
implementation only) to have the same address. A program could
depend on that, using address comparison rather than strcmp() to
compare two string literals.

Ok, perhaps it's a lame example, but it doesn't require core dumps
as part of expected behavior. It does break rules about pointer
comparison for equality.

Oct 17 '06 #8

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

Similar topics

1
by: Jaime Montes | last post by:
I have found that adding in the start of the file the character '-1' and '-2' I can read the file as a Unicode, and to write any character I have to write pairs of character so for 'a' I write '0'...
5
by: TheDD | last post by:
Hello all, i've downloaded the source code of a GPL project but i don't manage to compile it. It have been written with an old g++ (<3) since there are #include <xxx.h> and no std:: at all......
62
by: SAN3141 | last post by:
There doesn't seem to be consensus about when to put code in the database or in the middle tier. There was a long discussion about this in an Oracle newsgroup (message ID:...
100
by: Roose | last post by:
Just to make a tangential point here, in case anyone new to C doesn't understand what all these flame wars are about. Shorthand title: "My boss would fire me if I wrote 100% ANSI C code" We...
4
by: Nick | last post by:
Hi, I am trying to output a string of chinese characters as a text file. When I open a file for writing from VB, the file is automatically set to UTF-8 encoding (can tell by opening the file...
3
by: nicksop | last post by:
How Can I convert ANSI to OEM with StreamWriter ?
2
by: Nikolay Petrov | last post by:
How to read ANSI text. The sream reader reads only UTF-8
5
by: mmxbass | last post by:
In Visual Studio 2005, how can I set my project to be a strict ANSI-C project with absoloutly nothing else? I don't want my project to even compile if it contains any C++ or non-ANSI code.
10
by: Michael B. Trausch | last post by:
Alright... I am attempting to find a way to parse ANSI text from a telnet application. However, I am experiencing a bit of trouble. What I want to do is have all ANSI sequences _removed_ from...
65
by: Leslie Kis-Adam | last post by:
Hi everyone! Does anyone know, if it is possible to clear the screen in ANSI C? If it is,then how? Any help would be appreciated. Laszlo Kis-Adam <dfighter_AT-NOSPAM_freemail.hu
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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
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...

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.