469,890 Members | 1,419 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

char* and different memory moving functions like memcpy etc

Is it true that it is not allowed to overwrite statically initialized
char * ?
For example:

#include <stdio.h>
#include <string.h>

int main(void)
{
char *dest=" ";
char *src="kakjis";
memcpy(dest,src,1);
printf("%s",dest);
return 0;
}

the gived code compiled with gcc under SuSe 10.1 gives segmentation
fault;
if i replace char *dest=" "; with a correct malloc everything
works.

Could someone please explain this behaivor to me ?

Mar 16 '07 #1
2 1702
ziibrs wrote:
Is it true that it is not allowed to overwrite statically initialized
char * ?
No.
For example:

#include <stdio.h>
#include <string.h>

int main(void)
{
char *dest=" ";
char *src="kakjis";
memcpy(dest,src,1);
printf("%s",dest);
return 0;
}

the gived code compiled with gcc under SuSe 10.1 gives segmentation
fault;
if i replace char *dest=" "; with a correct malloc everything
works.

Could someone please explain this behaivor to me ?
It's not allowed to change the contents of a /string literal/.

It might be in write-protected memory (BOOM) or it might be in
write-ignoring memory (WHOOSH) or it might be in plain old
RAM (Mother, he has deaded "me"!).

--
Chris "eccles" Dollin
"Who are you? What do you want?" /Babylon 5/

Mar 16 '07 #2
Thanks, i kinda thought that could be the reason but i was not sure.

Thanks for the answer , it brought some joy into this cloudy day :)

Mar 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Roy Hills | last post: by
11 posts views Thread by Walter Dnes (delete the 'z' to get my real address | last post: by
6 posts views Thread by Juergen Wohnich | last post: by
42 posts views Thread by S S | last post: by
18 posts views Thread by Pedro Pinto | last post: by
30 posts views Thread by Yevgen Muntyan | last post: by
7 posts views Thread by daniel | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.