char* and different memory moving functions like memcpy etc

Z

ziibrs

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 ?
 
C

Chris Dollin

ziibrs said:
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"!).
 
Z

ziibrs

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 :)
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top