Memory Allocation Error - /w Valgrind Output

This is a discussion on Memory Allocation Error - /w Valgrind Output within the C Programming forums, part of the General Programming Boards category; Hi, I need some help with this error, I simply can't find the cause. I know it is probably going ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Memory Allocation Error - /w Valgrind Output

    Hi,

    I need some help with this error, I simply can't find the cause. I know it is probably going to be obvious, but I'm fairly sure malloc is being used incorrectly.

    Any and all comments welcome.


    Valgrind Error:
    Code:
    ==9316== 1 errors in context 1 of 6:
    ==9316== Invalid write of size 1
    ==9316==    at 0x4C2839F: strcat (mc_replace_strmem.c:176)
    ==9316==    by 0x400F6E: replace (stringfunctions.h:25)
    ==9316==    by 0x402104: main (root.c:466)
    ==9316==  Address 0x51b0069 is not stack'd, malloc'd or (recently) free'd
    ==9316== 
    ==9316== 
    ==9316== 1 errors in context 2 of 6:
    ==9316== Invalid read of size 1
    ==9316==    at 0x4C28374: strcat (mc_replace_strmem.c:176)
    ==9316==    by 0x400F6E: replace (stringfunctions.h:25)
    ==9316==    by 0x402104: main (root.c:466)
    ==9316==  Address 0x51b0059 is 0 bytes after a block of size 25 alloc'd
    ==9316==    at 0x4C274A8: malloc (vg_replace_malloc.c:236)
    ==9316==    by 0x400F03: replace (stringfunctions.h:20)
    ==9316==    by 0x402104: main (root.c:466)
    My Function:

    Code:
    LINE: 19 -> void replace(char *initial, char *startbreak, char *endbreak, char *insert){
    LINE: 20 -> char *replacebuf;
    LINE: 21 ->  if ((replacebuf = malloc(strlen(initial) + strlen(insert))) == NULL)
    LINE: 22 ->    err(EX_OSERR, "data malloc");
    LINE: 23 ->  
    LINE: 24 ->  printf("%ld", startbreak - initial);
    LINE: 25 ->  strncpy(replacebuf, initial, (int)(startbreak - initial) - 1);
    LINE: 26 ->  strcat(replacebuf, insert);
    LINE: 27 ->  strcat(replacebuf, endbreak);
    LINE: 28 ->  strcpy(initial, replacebuf);
    LINE: 29 ->  free(replacebuf);
    LINE: 30 -> }
    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,674
    > Invalid write of size 1
    +
    > malloc(strlen(initial) + strlen(insert))

    You need +1 to store the \0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    You have a good point, I should really have that in there!

    I Tried adding +1 to my malloc statement, but I receive the same errors.

    I'll go back through where I pass the variables from and make sure everything is a closed string!

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Btw, strncpy may not null terminate your string.
    It's safer to do it explicitly.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    You malloc enough space for initial and insert, but copy in part of initial, then insert, then endbreak. I assume startbreak and endbreak are pointers to somewhere in the middle of initial. Are you sure they're between the start of initial and it's terminating null character? Also, are you sure endbreak comes after startbreak? Those two things could cause you to copy too much junk into insert resulting in your memory issue. You could try something that is equivalent, but logically a little simpler (note, this is untested):
    Code:
    void replace(char *initial, char *startbreak, char *endbreak, char *insert)
    {
        char *endbuf;
    
        if ((endbuf = malloc(strlen(endbreak)+1) == NULL)
            err(EX_OSERR, "data malloc");
    
        strcpy(endbuf, endbreak);
        printf("%ld", startbreak - initial);
        strcpy(startbreak, insert);    // copy the insert into initial at the "startbreak" position
        strcat(initial, endbuf);       // cat endbuf onto the end of the string
    
        free(endbuf);
    }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Thanks for the suggestions I'll give them a try and let you know how it goes.

    That is a good point andurll. From what I can see this code is a bit less costly too.

    Yes the pointers always have the following order -

    Initial,
    Startbeak,
    Endbreak,
    \0 ---> not a pointer, but indicates that endbreak comes before EOS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increasing memory allocation in function
    By Ramses800 in forum C Programming
    Replies: 3
    Last Post: 12-16-2008, 04:30 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. Memory Allocation :: C/C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2002, 10:38 AM

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