Possible Duplicate:
What is the difference between char a[] = “string” and char *p = “string”?
What is the difference between using memcpy on stack memory vs on heap memory? The following code works on Tru64 but segfaults on LINUX
char * string2 = " "; (void)memcpy((char *)(string2),(char *)("ALT=---,--"),(size_t)(10)); The second version works on LINUX
char * string2 = malloc(sizeof(char)*12); (void)memcpy((char *)(string2),(char *)("ALT=---,--"),(size_t)(10)); Can someone explain the segfault on LINUX?
sizeof(char)is defined to be one.malloccounts the size to be allocated in terms of sizes ofchar.string2is not guaranteed to be nul-terminated after thememcpyhas finished. That might cause you some problems later.char *string2 = "ALT=---,--<2 spaces>";? If the app is safety-critical then you need to look especially carefully at what the effect is on Tru64 of modifying that string literal. The same string literal occurring in different parts of the code can point to the same memory, so it may be (let's hope not) that the original author relies on thememcpyto result in another instance of"<12 spaces>"somewhere else in the code being suddenly changed!