gcc bug?

V

Victor Irzak

Hello!

This program causes seg fault on gcc, but executes fine on icc and VC7.
Is there a reason for it or is it a bug?
Note: if "char * const str" is changed to "char * str", the gcc problem disappears.

#include "stdio.h"

int main() {
char * const str="ya";
char *first = &str[0], *second =&str[1];
char tmp;

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

tmp = *first;
*first = *second;
*second = tmp;

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

return 0;
}
 
A

Artie Gold

Victor said:
Hello!

This program causes seg fault on gcc, but executes fine on icc and VC7.
Is there a reason for it or is it a bug?
Note: if "char * const str" is changed to "char * str", the gcc problem disappears.

#include "stdio.h"

int main() {
char * const str="ya";
char *first = &str[0], *second =&str[1];
char tmp;

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

tmp = *first;
*first = *second;

You're trying to mutate a string literal -- which is not necessarily
mutable. Hence, in the gcc case, BOOM!
*second = tmp;

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

return 0;
}

See, for example:

http://www.eskimo.com/~scs/C-faq/q1.32.html

HTH,
--ag
 
A

Arthur J. O'Dwyer

This program causes seg fault on gcc, but executes fine on icc and VC7.
Is there a reason for it or is it a bug?

Yes. It's a bug in your program.
Note: if "char * const str" is changed to "char * str",
the gcc problem disappears.

The bug is still present even with the 'const' removed.
#include "stdio.h"

Should be
#include said:
int main() {
char * const str="ya";

'str' points to the string "ya", which is stored somewhere
off in memory, possibly in ROM (think: a segment to which your
program cannot write).
char *first = &str[0], *second =&str[1];

'first' and 'second' also point into that same string, in
read-only memory. By the way, do you understand that

char *first = str, *second = str+1;

would be equivalent to the line above?
char tmp;

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

Prints the string "ya". This is fine, so far.
tmp = *first;
*first = *second;

Here's your bug. You try to assign a new value to
the char object pointed to by 'first' -- and that
object is off in read-only memory. You can't modify
string literals in C.
This is where the program segfaults with GCC.
*second = tmp;

A second bug.
printf("%s\n", str);

return 0;
}


To make your program work as expected, and remove
the undefined behavior, you could create an array
local to 'main' in which to store your string:

char str[] = "ya";

(then proceed as above). See this newsgroup's
FAQ for more information.

-Arthur
 
E

Eric Sosman

Victor said:
Hello!

This program causes seg fault on gcc, but executes fine on icc and VC7.
Is there a reason for it or is it a bug?
Note: if "char * const str" is changed to "char * str", the gcc problem disappears.

#include "stdio.h"

int main() {
char * const str="ya";
char *first = &str[0], *second =&str[1];
char tmp;

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

tmp = *first;
*first = *second;
*second = tmp;

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

return 0;
}

It's a bug ... in your code. You are trying to
modify the contents of the string literal "ya", and
this produces undefined behavior.

<off-topic>

It's likely that gcc is taking the `const' as a
hint to put "ya" in read-only memory. The compilers
are not obliged to do this (the others you mention
apparently do not). On the other hand, they are free
to use read-only memory for "ya" even if `const' is
not present.

</off-topic>
 
A

Arthur J. O'Dwyer

<off-topic>

It's likely that gcc is taking the `const' as a
hint to put "ya" in read-only memory. The compilers
are not obliged to do this (the others you mention
apparently do not). On the other hand, they are free
to use read-only memory for "ya" even if `const' is
not present.

Just for curiosity's sake, can anyone verify whether
gcc actually will put the target of a 'char * const'
pointer into a read-only area (but not the target of
a non-const-qualified pointer)?

I had thought of giving the explanation Eric did,
but then I noticed that it wasn't the *target* string
that was 'const' in Victor's example, it was the value
of the *pointer* itself! I really wouldn't expect
gcc to make that optimization based on the constness
of the *pointer*!
</OT>

-Arthur
 
M

Mark A. Odell

Just for curiosity's sake, can anyone verify whether
gcc actually will put the target of a 'char * const'
pointer into a read-only area (but not the target of
a non-const-qualified pointer)?

I had thought of giving the explanation Eric did,
but then I noticed that it wasn't the *target* string
that was 'const' in Victor's example, it was the value
of the *pointer* itself! I really wouldn't expect
gcc to make that optimization based on the constness
of the *pointer*!
</OT>

You seek the -fwritable-strings flag. Without it strings will be
non-writable and you are correct that the pointer being const would not
influence gcc's placement of the string.
 
E

Erik de Castro Lopo

Arthur J. O'Dwyer said:
Just for curiosity's sake, can anyone verify whether
gcc actually will put the target of a 'char * const'
pointer into a read-only area (but not the target of
a non-const-qualified pointer)?

For GCC vesion 3 and later, all string literals (const
or otherwise) are read only unless the -fwriteable-strings
compiler swithc is used.

There is also a -Wwrite-strings to force a warning when
and attempt is made to write a string literal.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
Orcad Express 9: "Its a nice demo but I wouldn't want to use it
on a day-to-day basis" -- me
 
A

Alex

Victor Irzak said:
This program causes seg fault on gcc, but executes fine on icc and VC7.
Is there a reason for it or is it a bug?
Note: if "char * const str" is changed to "char * str", the gcc problem disappears.
#include "stdio.h"
int main() {
char * const str="ya";
char *first = &str[0], *second =&str[1];
char tmp;
printf("%s\n", str);
tmp = *first;
*first = *second;
*second = tmp;
printf("%s\n", str);
return 0;
}

You are trying to modify the string literal "ya". This is
forbidden as it may reside in read-only memory. The following,
however, is legal:

char str[] = "ya";

Alex
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top