Thread: Heap Corruption When freeing memory?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    Heap Corruption When freeing memory?

    My Function...
    Code:
    PHP Code:
    int findpatternchar sourcechar startchar end) {     char start_ptr strstr(sourcestart);     if (start_ptr != NULL)     {     char end_ptr strstr(start_ptrend);     if (end_ptr != NULL)     {     start_ptr += strlen(start);     end_ptr -= 1;     printf("start_ptr = %c\n", *start_ptr);     printf("end_ptr= %c\n", *end_ptr);     printf("findpattern: start = %d\n"start_ptr);     printf("findpattern: end= %d\n"end_ptr);     printf("= %d", (end_ptr-start_ptr)); /* I must write one more byte to avoid this heap corruption.? Why is this. I included the terminating character at the end. */ //char * textinpattern = (char*) malloc((end_ptr-start_ptr)+1); /* the fix... */ char textinpattern = (char*) malloc((end_ptr-start_ptr)+2);     memcpy(textinpatternstart_ptr, (end_ptr-start_ptr)+1);     memcpy(textinpattern+(int)(end_ptr-start_ptr)+1"\0"1);          printf("%s"textinpattern);     free (textinpattern);     return 1;     }     }else{     return 0;     } } 
    So I have to write one more byte of memory? I don't see why.. here is the problem code
    Code:
    PHP Code:
    char textinpattern = (char*) malloc((end_ptr-start_ptr)+1); 
    I included the terminating character to end the string. ? OR.. did I miss count
    Last edited by Clinthill98; 04-02-2013 at 07:35 PM.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Say start_ptr was 0x10000000 and end_ptr was 0x10000005; your code is copying the string between these inclusive of both ends. End-start is 5.

    You need six bytes to store the 6 positions in the string (0x1...0, 1, 2, 3, 4, 5). You need a 7th byte to store the terminating null.

    The correct size to malloc is end-start+2 characters. End-start+1 is the length of the data, and then you need to allocate length+1 bytes to make room for the null termination.

    You can rewrite like this to make it more clear (I also got rid of the one byte memcpy in favor of array syntax which I find easier to read). Added some comments to make it easier to follow if you don't understand:

    Code:
        int dataLength = (int)(end_ptr-start_ptr)+1;
    
        // this allocates space for textinpattern[0]...textinpattern[dataLength]
        char * textinpattern = (char*) malloc(dataLength+1); 
    
        // this sets textinpattern[0]...textinpattern[dataLength-1]
        memcpy(textinpattern, start_ptr, dataLength);
    
        // this sets the final terminating null at the last position in the memory we allocated
        textinpattern[dataLength]='\0';
    Last edited by Cat; 04-02-2013 at 08:23 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    These things work better when you adopt the convention that 'end' is actually one-past-the-end. It typically eliminates places where you have to add or subtract one, or in this case you'd only need to add one and not two.
    Not to mention that regardless you should make it clear what convention you are using.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    ah thanks guys.. ya threw me off.. I miss counted one 1 byte in the text in the pattern. ty for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This may be due to a corruption of the heap
    By Cobs in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2011, 09:53 PM
  2. Possible Heap Corruption
    By Michael5978 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2010, 04:09 PM
  3. Replies: 4
    Last Post: 11-10-2009, 06:47 PM
  4. This may be due to a corruption of the heap
    By krishnampkkm in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2009, 03:19 AM
  5. Heap corruption with dynamic memory
    By Head In Jar Man in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2009, 02:58 AM