#include <string.h> char *_debug_strcpy(char *string1, const char *string2, const char *file, size_t line);
This is the debug version of strcpy. Like strcpy, it copies string2, including the ending null character, to the location specified by string1.
_debug_strcpy validates the heap after copying the string to the target location, and performs this check only when the target is within a heap. _debug_strcpy makes an implicit call to _heap_check. If _debug_strcpy detects a corrupted heap when it makes a call to _heap_check, _debug_strcpy reports the file name file and line number line in a message.
Returns a pointer to the copied string string1.
This example contains a programming error. The source string is too long for the destination buffer, and the strcpy operation damages the heap.
/* _debug_strcpy.c */ #include <stdlib.h> #include <stdio.h> #include <string.h>
#define SIZE 10
int main(void) { char *source = "1234567890123456789"; char *destination; char *return_string;
destination = (char*)malloc(SIZE); strcpy(destination, "abcdefg"),
printf("destination is originally = '%s'\n", destination); return_string = strcpy(destination, source); printf("After strcpy, destination becomes '%s'\n\n", destination); return 0; }
The output is similar to:
destination is originally = 'abcdefg' End of allocated object 0x00073c80 was overwritten at 0x00073c8a. The first eight bytes of the memory block (in hex) are: 3132333435363738. This memory block was (re)allocated at line number 13 in _debug_strcpy.c. Heap state was valid at line 14 of _debug_strcpy.c. Memory error detected at line 17 of _debug_strcpy.c.