What's up with this strcpy?
I'm having some problem with the strcpy function. Hopefully some of you can point out what I'm doing wrong please.
Basically, I'm calling a function to read a file and put something in an output buffer. The function is something like this.
Code:
int PutSthInBuffer (unsigned char *Buffer, int length) {
unsigned char *Path;
unsigned char *Type;
unsigned char *Line;
// Memory is first allocated for all three local pointers.
//
// read a line, then call strtok and assign one of the tokens to
// Path. The reading line part can be safely assumed to be taken
// care of (don't want to post extra code).
//
Type = strtok (Line, " ");
Path = strtok (NULL, " ");
Buffer = strcpy (Buffer, Path);
FreeMem (Path, Type, Line); // this free memory from whatever
// pointers it's passed.
return 0;
}
void FreeMem (unsigned char *ptr1, unsigned char* ptr2, unsigned
char *ptr3) {
free (ptr1);
free (ptr2);
free (ptr3);
}
The problem I'm having is, before I call FreeMem, the content of Buffer is what it should be. After I call FreeMem, the content of Buffer is no longer valid. So the thing that the calling function gets in the Buffer is not a valid string.
I checked the addresses of Buffer and Path, and they point to different addresses. So how come when I free Path, the content of Buffer is messed too? Hopefully I've included sufficient snippets of code to understand this problem. Let me know if you think other piece of code may be helpful.