p[1] = 'b'

C

chunpulee

hi

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *p = "aa";

p[1] = 'b'; /* Segmentation fault */
printf("%s\n", p);

return 0;
}

my os is linux fc8
 
B

bert

hi

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
        char *p = "aa";

        p[1] = 'b';   /* Segmentation fault */
        printf("%s\n", p);

        return 0;

}

my os is linux fc8

Try char *pp, *p = "aa";
pp = p;
pp[1] = 'b';

pp points to read/write memory, but p may
point to read-only memory, whence your
segmentation fault.
--
 
C

Chris Dollin

chunpulee said:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *p = "aa";
p[1] = 'b'; /* Segmentation fault */

You are not permitted to write into a string literal [1,2]. As it happens,
your implementation caught you.
printf("%s\n", p);
return 0;
}

[1] More precisely, you're not permitted to write into whatever piece of
store implements the string literal.

[2] In the apparently-similar case `char p[] = "aa";`, you can write
inside `p` all you like; it's not a string literal, just initialised
from one.
 
I

Ian Collins

bert said:
hi

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *p = "aa";

p[1] = 'b'; /* Segmentation fault */
printf("%s\n", p);

return 0;

}

my os is linux fc8

Try char *pp, *p = "aa";
pp = p;
pp[1] = 'b';

pp points to read/write memory, but p may
point to read-only memory, whence your
segmentation fault.

NO, pp points to the same read only memory as p.
 
E

Edwin van den Oetelaar

chunpulee said:
hi

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *p = "aa";

p[1] = 'b'; /* Segmentation fault */
printf("%s\n", p);

return 0;
}

my os is linux fc8
FYI : there used to be a GCC option to make this work, '-fwriteable-strings' but it is gone a long
time. Use char p[]="aa" instead.
 
I

Ian Collins

Richard said:
Yes there is. The problem is that it doesn't warn you that your program
won't work on many other platforms.
Or that it could have corrupted some random bit of memory.
 
A

arnuld

Or that it could have corrupted some random bit of memory.

yes, it happened at my workplace. One of my colleagues did that same
char *p = "abcdefg" thing and then wrote an expression that modifies
the value of p[3] or something.

I don't know how it happened but during run time in some other
function, there was a local array whose contents were changed. After
banging my head for hours the problem was still unresolved.
Accidentally, out of being a practice of writing a standard conforming
code, I changed that to char p[] = "abcdefg" and everything ran fine.
(in my heart) I thanked Steve Summit for the FAQ :)
 
K

Keith Thompson

arnuld said:
Or that it could have corrupted some random bit of memory.

yes, it happened at my workplace. One of my colleagues did that same
char *p = "abcdefg" thing and then wrote an expression that modifies
the value of p[3] or something.

I don't know how it happened but during run time in some other
function, there was a local array whose contents were changed. After
banging my head for hours the problem was still unresolved.
Accidentally, out of being a practice of writing a standard conforming
code, I changed that to char p[] = "abcdefg" and everything ran fine.
(in my heart) I thanked Steve Summit for the FAQ :)

Another fix, perhaps a better one, would have been to change
char *p = "abcdefg";
to
const char *p = "abcdefg";
This would have caused the compiler to complain about the attempt to
modify the string.

It's impossible to tell without context, but it could be that the code
that modified p[3] was incorrect, and could cause problems for other
code that depends on the originally stored value of p. The change
you made might have masked that error rather than fixing it.

Of course if you really want the string to be modifiable, then
changing the declaration to
char p[] = "abcdefg";
is the right solution. (Presumably in real life it has a name that
doesn't imply it's a pointer.)
 
K

Keith Thompson

chunpulee said:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *p = "aa";

p[1] = 'b'; /* Segmentation fault */
printf("%s\n", p);

return 0;
}

Read section 8 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
Then read the rest of it.

I know you've already gotten an answer to your question, but the FAQ
is likely to answer your next question as well.
 
C

CBFalconer

chunpulee said:
#include <stdio.h>
#include <stdlib.h>

int main(void) {
char *p = "aa";

p[1] = 'b'; /* Segmentation fault */
printf("%s\n", p);
return 0;
}

my os is linux fc8

Your code is in error. "char *p = "aa";" sets p to point to a
constant string, possibly because it is mounted in read-only
memory. If you compiled with -Wwrite-strings (assuming you are
using gcc) it would have warned you. Or you could have used "const
char *p = "aa";". The reason for all this is that the const
appeared in C long after code like yours (not altering the strings)
was written.
 
C

CBFalconer

chunpulee said:
no problem in win32

You failed to quote previous messages. All messages should be
understandable by themselves.

Yes, it still is a problem. Even if it can be written to. C is
allowed to use that "aa" string elsewhere, and that elsewhere will
expect it to be an aa.
 
L

Lew Pitcher

You failed to quote previous messages. All messages should be
understandable by themselves.

Yes, it still is a problem. Even if it can be written to. C is
allowed to use that "aa" string elsewhere, and that elsewhere will
expect it to be an aa.

To make the point clear:

{
char *sheep_says = "baa";
char *stuff = "aa";
}

The standards allow the compiler to arrange the static values so that
the "aa" of (stuff) coincides with the "aa" of (sheep_says+1).


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
B

Barry Schwarz

hi

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *p = "aa";

p[1] = 'b'; /* Segmentation fault */
printf("%s\n", p);

return 0;
}

my os is linux fc8

Your code invokes undefined behavior by trying to modify a string
literal. The fact that linux stored the string in read only memory
prevented your program from actually performing the operation.

The fact that Windows did not, as you report in a subsequent message,
does not in any way ameliorate the undefined behavior. It merely
substituted one type of undefined behavior (appearing to do what you
asked) for the previous type (invoking a seg fault).

In both cases, the behavior is undefined.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top