Solved

gcc bug !?

Posted on 1998-10-31
11
318 Views
Last Modified: 2010-04-22
Hi,
I think that I found some kind of bug in gcc.
This simple code to cycle through devices changes the last letter (device name, hda, hdb, etc)
This code compiles and runs fine on msDev, Borland, AIX V5.
When I compiled it on my Linux machine it results in a core dump (segmentation fault) as soon as you try to change the device letter.


#include <stdio.h>


int main()
{
    char* sDev = "/dev/hd_\n", /* the '_' is char # 8 */
        *p = NULL;

    p = &sDev[8];

    printf("%s\n", sDev);

    *p = ((char)0x61); /* ERROR on Linux */

    printf("%s\n", sDev);

    return 0;
}

?
0
Comment
Question by:bod_1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1638422
modification of a string literal is undefined behavior
writable string literals is a common extension
0
 

Author Comment

by:bod_1
ID: 1638423
I was taught that in C you'r supposed to be able to alter strings and arrays through an item's index or a pointer to that index.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1638424
then you weren't taught in strict compliance to ANSI Standard X3.159-1989
try
static char sDev[] = "/dev/hd_\n";
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:bod_1
ID: 1638425
ahh,
I've been using msDev and Borland soo often that using strict compiling really messed me up.  I still need to get used to it.
That answers my question.
Thanks Ozo.
0
 

Author Comment

by:bod_1
ID: 1638426
Yes,
That was the problem, I tried the same thing declaring an array instead and there were no problems.
0
 

Author Comment

by:bod_1
ID: 1638427
Ozo,
Do you want these points?
You straightened out my question for me
0
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
ID: 1638428
although it is a common extension to allow writeable string literals, and to guarantee that identical string literalls be distinct,
the ANSI C standard actually specifies them to be unmodifiable,
and if a program attemps to modify a literal rhe behavior is undefined.
a strictly conforming program should instead use:

     static char sDev[] = "/dev/hd_\n";

0
 

Author Comment

by:bod_1
ID: 1638429
When you told me that I lost some respect for C.  That rule puts some serious restrictions on what can be done regarding strings.
Altering unknown length strings is sometimes a necessity.

eg.  For a recent assignment at school I implemented a function with the ...'s.  If I had it my way, I would have malloc'd a string to hold the arguments, and realloc'd the string as needed if there were more args.  Because of that rule, I just set a limit on the number of args that could be passed.  I would have preferred the limits being set by the amount of memory available.
If I sound ignorant (ie. undoubtably there are some good reasons behind that rule) please excuse me;  I am still very new to this.

Anyways, thanks for the help Ozo.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1638430
malloc and realloc are in the C standard, so it sounds like your way should work.

The ANSI C Rationale (which is not actually part of the X3.159-1989 standard itself) says about section 3.1.4:
String literals are specified to be unmodifiable.  This specification allows implementations to share copies of strings with identical text, to place string literals in read-only memory, and perform certain optimizations.
...
Those members of the Committee who insisted that string literals should be modifiable were content to have this practice designated a common extension (see F.5.5).
Existing code which modifies string literals can be made strictly conforming by replacing the string literal with an initialized static character array.
...
0
 

Author Comment

by:bod_1
ID: 1638431
Ah,  I never tried modifying the string after using malloc...assumed that the rule applied to all char*'s.
That's not so bad then.
Thanks again Ozo
0
 
LVL 84

Expert Comment

by:ozo
ID: 1638432
A string literal is a character sequence enclosed in double-quotes
(const char *) may be unmodifiable
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question