My implementation of strcat(char*, const char*)
seems to work but then it causes a core dump.
strcat()
implementation:
char* strcat(char* dest, const char* src)
{
char* tmp = dest;
while(*tmp) ++tmp ;
while( (*tmp++ = *src++ ) != '\0') ;
return (dest);
}
Code in int main()
where I call strcat():
char arr3[] = "Mr. ";
char arr4[] = "Smith";
printf("Hello %s!", strcat(arr3, arr4));
It actually concatenated both strings and printed it out but still caused a core dump.
output : Hello Mr. Smith!Aborted (core dumped)
What did I do wrong?
Your program doing buffer overflow at runs time, by
strcat(arr3, arr4)
becausearr3
size is just equals to length of"Mr."
string , it has no extra memory space for extra chars (fromarr4
).size of
arr3
should be atlest string length of a"Mr. " + "Smith" + 1
(extra 1 for string termination
\0
char)My Suggestion is use dynamic memory allocation for sufficient size buffer, do something like code below:
Reason of Core-Dump:
In your code :
The size of
arr2
is = length of string"Mr."
length +1
(1 because of\0
) chars. Thestrcat()
first movestemp
pointers to point null in first while loopwhile(*tmp) ++tmp ;
.After that in second while loop
while( (*tmp++ = *src++ ) != '\0') ;
you are trying to accessing and assigning on memory that is not allocated ( my be out of your process control) and access a memory that you have not allocated is undefined behavior in C.Edit:
In code
arr3
is something like below in diagram, wheretemp
points toarr3
array:when loop
while(*tmp) ++tmp ;
breakstemp
starts pointing to memory location8
where null\0
is stored, like below diagram.When you do
temp++
in loopwhile( (*tmp++ = *src++ ) != '\0') ;
,temp
increment to point to memory location9
and onwards, but access and assigning on memory9
,10
.. is illegal because its not allocated. This causes OS kernel send a signal core dump to the your process which caused the exception. (interesting to note: as OS detects memory right violation by a process -- An invalid access to valid memory gives: SIGSEGV And access to an invalid address gives: SIGBUS).If you allocates extra memory (as @JerryCoffin and @Paul R suggested) then you can access memory beyond
\0
(memory location8
) is no problem.Note: If you not gives size at declaration then size of array will be equals to size of string e.g. in
char arr3[] = "Mr. ";
size is5
. But if you give size explicitly aschar arr3[84] = "Mr. ";
then size ofaar3
will be84
and initial memory containsMr.
then0
in rest of all locations.In my solution I completely allocate new memory blocks that is as large as both string
arr3
andarr4
can be stored with null symbol using dynamic memory allocation (malloc()
function). Additionally If you allocates dynamic memory usingptr = malloc()
orptr = calloc()
then you should free memory explicitly when work done usingfree(ptr)
.You haven't allocated any additional space in
str3
so attempting to append characters usingstrcat
will overflow the allocated storage - change your code to e.g.:The problem isn't with your
strcat
, but with how you're using/invoking it. You're writing past the end of the target array.Change
arr3
to something likechar arr3[32] = "Mr. ";
and all will be well.