Is it because of diffrence between bcc32 and gcc?

J

jigsaw

I found that a simple c program is treadted diffrently with bcc32 and
gcc.

The bcc++ version is: Borland C++ 5.5.1 for Win32 Copyright (c) 1993,
2000 Borland, under WinXP Pro

The gcc version is: gcc 4.0.2, under ubuntu 5.1

The c program is:

/* code start */
#include <stdio.h>

int
main(void)
{
char *s;

s = "abc";
s[0] = 'b'; /* s is expected to be "bbc" now */
printf("%s\n", s);
return 0;
}
/* code end */

This program can pass compilation under both bcc32 and gcc.
And it runs as I had expected under WinXP - the output is "bbc".
But when I tried to run it under ubuntu, it fails with one line of
message: Segmentation fault

I also use splint to examin the program, splint compains that:

/* splint start */
Splint 3.1.1 --- 28 Apr 2003

test.c: (in function main)

test.c:8:2: Suspect modification of observer s[0]: s[0] = 'b'

Storage declared with observer is possibly modified. Observer storage
may not

be modified. (Use -modobserver to inhibit warning)

test.c:7:6: Storage s[0] becomes observer

Finished checking --- 1 code warning
/* splint end*/

But I just don't understand what splint is trying to express.

Can anyone do me a favor to point out the flaw of my program? thx
 
I

Ian Collins

jigsaw said:
I found that a simple c program is treadted diffrently with bcc32 and
gcc.

The bcc++ version is: Borland C++ 5.5.1 for Win32 Copyright (c) 1993,
2000 Borland, under WinXP Pro

The gcc version is: gcc 4.0.2, under ubuntu 5.1

The c program is:

/* code start */
#include <stdio.h>

int
main(void)
{
char *s;

s = "abc";
s is pointing at a string literal, which may be in read only memory.
s[0] = 'b'; /* s is expected to be "bbc" now */

You are attempting to change said string literal.
 
M

Michael Mair

jigsaw said:
I found that a simple c program is treadted diffrently with bcc32 and
gcc.

The bcc++ version is: Borland C++ 5.5.1 for Win32 Copyright (c) 1993,
2000 Borland, under WinXP Pro

The gcc version is: gcc 4.0.2, under ubuntu 5.1

Compile both programmes as C code (not C++).
Use a modus conforming to a standard C version.
Use the highest possible or at least a very high warning level.
(e.g. gcc: "gcc -std=c89 -pedantic -Wall -O")
The c program is:

/* code start */
#include <stdio.h>

int
main(void)
{
char *s;

s = "abc";
s[0] = 'b'; /* s is expected to be "bbc" now */

FAQ:
http://c-faq.com/charstring/index.html
look for "string literal". Ideally, read all.
printf("%s\n", s);
return 0;
}
/* code end */

This program can pass compilation under both bcc32 and gcc.
And it runs as I had expected under WinXP - the output is "bbc".
But when I tried to run it under ubuntu, it fails with one line of
message: Segmentation fault

That is the problem with relying on non-standard behaviour...
I also use splint to examin the program
<snip>

This is an excellent idea :)


Cheers
Michael
 
G

Gimmmo

But I just don't understand what splint is trying to express.

Can anyone do me a favor to point out the flaw of my program? thx


The C Programming Language, Second Edition
Brian W. Kernighan and Dennis M. Ritchie

Page 104:

char *pmessage = "now is the time"; /* a pointer */

pmessage is a pointer, initialized to point to a string CONSTANT;
the pointer may subsequently be modified to point elsewhere, but
the result is UNDEFINED if you try to MODIFY the string contents.

You should have the book in hand, it's the bible of C.

Rgds.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top