#include <string.h> char *_debug_strcat(char *string1, const char *string2, const char *file, size_t file);
This is the debug version of strcat. Like strcat, it concatenates string2 to string1 and ends the resulting string with the null character.
_debug_strcat validates the heap after concatenating the strings, and performs this check only when the target is within a heap. _debug_strcat makes an implicit call to _heap_check. If _debug_strcat detects a corrupted heap when it makes a call to _heap_check, _debug_strcat reports the file name file and line number file in a message.
Returns a pointer to the concatenated string string1.
This example contains a programming error. The buffer1 object is not large enough to store the result after the string " program" is concatenated.
/* _debug_strcat.hc */ #include <stdlib.h> #include <stdio.h> #include <string.h> #define SIZE 10 int main(void) { char *buffer1; char *ptr; buffer1 = (char*)malloc(SIZE); strcpy(buffer1, "computer"); ptr = strcat(buffer1, " program"); printf("buffer1 = %s\n", buffer1); return 0; }
The output is similar to:
End of allocated object 0x00073c80 was overwritten at 0x00073c8a. The first eight bytes of the memory block (in hex) are: 636F6D7075746572. This memory block was (re)allocated at line number 12 in _debug_strcat.c. Heap state was valid at line 13 of _debug_strcat.c. Memory error detected at line 15 of _debug_strcat.c.