By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

char* and different memory moving functions like memcpy etc

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.