What's up with this strcpy?

This is a discussion on What's up with this strcpy? within the C Programming forums, part of the General Programming Boards category; I'm having some problem with the strcpy function. Hopefully some of you can point out what I'm doing wrong please. ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    27

    Wink 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.
    Last edited by fanoliv; 06-19-2006 at 04:47 PM.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>Buffer = strcpy (Buffer, Path);

    strcpy (Buffer, Path); would suffice here. For me, strcpy is
    returning the address of the Buffer parameter, but perhaps it is
    returning the address of the Path parameter, in which case,
    explains your problem - maybe check that the addresses of Buffer
    and Path are different after this line of code? If they are not,
    then just use strcpy (Buffer, Path);

    >>Hopefully I've included sufficient snippets of code

    Not in my opinion, please post the code for FreeMem, and also
    code involving the declaration of Path in the code as well, since
    in your snippet, Path is declared as unsigned char - not a pointer.
    Try my above suggestion first though.

    [EDIT]
    OP has updated his code, so the above is out of context i guess...
    [/EDIT]

    [UPDATE]
    you shouldn't call free on a pointer that was not allocated with
    malloc.
    [/UPDATE]
    Last edited by Richie T; 06-19-2006 at 04:50 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    27
    My mistake about the declaration. I've fixed the snippet above and added stuff you suggested.

    As for the addresses of the pointers after the strcpy call, they're still the same as they were before, i.e. they are still pointing to different addresses. So I still don't know what's going on.

    Also, I realize now that there's a memory leak here since when I call strtok, it reassigns my Path and Type pointers to point to the memory that was assigned to Line. So when I call FreeMem, it's probably not doing what I thought it did. I'll go fix that first and see if it change anything.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    27
    What I tried above didn't help, actually. My Buffer is still messed up after I free the Line pointer. Now my code is like this.

    Code:
    int PutSthInBuffer (unsigned char *Buffer, int length) {
         unsigned char *Path;
         unsigned char *Type;
         unsigned char *Line;
    
         Line = calloc (SIZE, 1);
         
         //
         // 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 (Line, NULL, NULL); // 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);
    }
    Sorry, it's quite messy where I'm calling FreeMem, but it should work the same right? It should just free whatever memory was allocated to Line, but it shouldn't affect whatever I copied to Buffer. However, it is still messing up Buffer when I free Line. Any suggestion? Thanks a lot.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    27
    Ok...sorry about that. I figured out that the Line and Buffer are actually pointing to the same address all along (I was reading the addresses wrong earlier). So I guess now my question is, why are they pointing to the same address? I didn't assign Line to Buffer anywhere. Thanks.

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>I didn't assign Line to Buffer anywhere.

    Check the addresses before and after the line:

    Buffer = strcpy (Buffer, Path);

    If it's not happening there, then I don't know where it is
    happening - nonetheless I don't see the point in assigning the
    return value of strcpy to Buffer - just doesn't sit well with me.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    27
    Ok...figured it out. Sorry to waste all your time. The problem was when I was reading using fgets. I was reading correctly, but was reading into the wrong buffer, which in the process changed Line to point to the same thing as Buffer.

    I know...that's stupid. Sorry to waste your time.

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    //
    // 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).
    //
    Typical isn't it? The part that you think is working ok ends up
    biting back!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 06:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21