Solved

gcc bug !?

Posted on 1998-10-31
11
313 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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
git hub on ubuntu clone a git hub repo how do i find the "packed' file that it downloaded 5 74
Guacamole and browser performance 1 87
awk file 6 52
error log using ftp 7 41
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

773 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