String literals
zeljko oluic
zoluic@telostech.com
Wed Apr 5 12:53:00 GMT 2000
Hi dear fellow,
ÃÂ
I am having a problem running the enclosed test program. I expected
program to pass the test points 1, 2 and 3 but instead it cored at point
2 (3). After looking into generated assembler code I noticed that
string literals casted to array initializers (see pointers S2
and s3) were put into read only segment. Is this correct (i.e. in compilance
with ANSI C standard ) ?
I am running gcc version 2.95.2 19991024 on SCO UNIX.
ÃÂ
ÃÂ
Thanks for your help,
ÃÂ
Zeljko Oluic
ÃÂ
Enc. strings test program
ÃÂ
#include <stdio.h>
ÃÂ
ÃÂ
char S1[] = "can be modified";
char *S2 = (char []) {"can be modified too"};
ÃÂ
ÃÂ
void
string_constant(void)
{
ÃÂ ÃÂ ÃÂ char *s1;
ÃÂ ÃÂ ÃÂ char *s2;
ÃÂ ÃÂ ÃÂ char *s3;
ÃÂ
ÃÂ
ÃÂ ÃÂ ÃÂ s1 = "if modified possible run-time error";
ÃÂ ÃÂ ÃÂ s2 = "if modified possible run-time error";
ÃÂ ÃÂ ÃÂ s3 = (char []) {"can be modified too"};
ÃÂ
ÃÂ
ÃÂ ÃÂ ÃÂ if (s1 == s2) printf("shared strings\n");
ÃÂ ÃÂ ÃÂ else printf("strings are not shared\n");
ÃÂ
ÃÂ ÃÂ ÃÂ printf("the following need to be ok\n");
ÃÂ ÃÂ ÃÂ printf("1\n");
ÃÂ ÃÂ ÃÂ S1[0] = 'x';
ÃÂ ÃÂ ÃÂ printf("2\n");
ÃÂ ÃÂ ÃÂ S2[0] = 'x';
ÃÂ ÃÂ ÃÂ printf("3\n");
ÃÂ ÃÂ ÃÂ s3[0] = 'x';
ÃÂ
ÃÂ ÃÂ ÃÂ printf("the following can cause a run-time error\n");
ÃÂ ÃÂ ÃÂ s1[0] = 'x';
ÃÂ
ÃÂ ÃÂ ÃÂ if (*s1 == 'x')ÃÂ printf("strings are writable\n");
ÃÂ ÃÂ ÃÂ else printf("strings are not writable\n");
}
More information about the Gcc-help
mailing list