block overrun

This is a discussion on block overrun within the C Programming forums, part of the General Programming Boards category; Hello, Can anyone please help me to understand, i am really tired of debugging this code now. The following the ...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

    block overrun

    Hello,

    Can anyone please help me to understand, i am really tired of debugging this code now. The following the is my version of stringtok function. I did a valgrind to see if there are any memory leak. But i dint get any memory leak but i got a block overrun. I have been trying to find out where i am over running. It cant see anything here

    Code:
    char ** TokentheString( char *Line, char *Del )
    {
        char **Tokenised, **ExtraToken = NULL;
        char Token[BUFSIZ], FormatSpec[15];
        char *TempLine = Line;
        int iOffset, iMAXS = 2,iIndex ;
        
        if( ( Tokenised = malloc( sizeof(char *) * iMAXS ) ) != NULL )
        {
            iIndex = 0;
            sprintf(FormatSpec, "%s%s%s", "%[^", Del, "]%n");
            
            while( sscanf(TempLine, FormatSpec, Token, &iOffset) == 1 )
            {
                if( iIndex >= iMAXS )
                {
                    iMAXS += 5;
        
                    if( ( ExtraToken = realloc( Tokenised, sizeof(char *) * iMAXS ) ) != NULL )
                    {
                        Tokenised = ExtraToken;
                        ExtraToken = NULL;
                        free(ExtraToken);
                    }
                    else
                    {
                        printf("Error: Realloc failed\n");
                        return NULL;
                    }
                }
                
                if( iIndex < iMAXS )
                {
                    if( ( Tokenised[iIndex] = malloc( sizeof(char) * strlen(Token) + 1) ) != NULL )
                    {  
                        strcpy( Tokenised[iIndex], Token );          
                        TempLine += iOffset;
                        
                        if( *TempLine != Del[0] )
                              break;                                
                        ++TempLine;
                    }
                    else
                    {
                        printf("Error: Memory allocation failed\n");
                        return NULL;
                    }
                }
                ++iIndex ;
            }
            
            if( ( Tokenised[iIndex] = malloc( sizeof(char) * 15) ) != NULL )
                strcpy( Tokenised[iIndex], "NULL");
        }
        else
        {
             printf("Error: Memory allocation failed\n");
            return NULL;
        }
        
        return Tokenised;
    }
    And the valgrind output

    Code:
    ==18652== Invalid write of size 4
    ==18652==    at 0x8049C8F: TokentheString (glLib.c:277)
    ==18652==    by 0x80491BC: PopulateData (glLib.c:22)
    ==18652==    by 0x8048E59: main (FlyComp.c:19)
    ==18652==  Address 0x4030818 is 0 bytes after a block of size 408 alloc'd
    ==18652==    at 0x40055E2: realloc (vg_replace_malloc.c:306)
    ==18652==    by 0x8049B5C: TokentheString (glLib.c:244)
    ==18652==    by 0x80491BC: PopulateData (glLib.c:22)
    ==18652==    by 0x8048E59: main (FlyComp.c:19)
    ==18652==
    ==18652== Invalid read of size 4
    ==18652==    at 0x8049C91: TokentheString (glLib.c:277)
    ==18652==    by 0x80491BC: PopulateData (glLib.c:22)
    ==18652==    by 0x8048E59: main (FlyComp.c:19)
    ==18652==  Address 0x4030818 is 0 bytes after a block of size 408 alloc'd
    ==18652==    at 0x40055E2: realloc (vg_replace_malloc.c:306)
    ==18652==    by 0x8049B5C: TokentheString (glLib.c:244)
    ==18652==    by 0x80491BC: PopulateData (glLib.c:22)
    ==18652==    by 0x8048E59: main (FlyComp.c:19)
    ==18652==
    ==18652== Invalid read of size 4
    ==18652==    at 0x8049CA0: TokentheString (glLib.c:278)
    ==18652==    by 0x80491BC: PopulateData (glLib.c:22)
    ==18652==    by 0x8048E59: main (FlyComp.c:19)
    ==18652==  Address 0x4030818 is 0 bytes after a block of size 408 alloc'd
    ==18652==    at 0x40055E2: realloc (vg_replace_malloc.c:306)
    ==18652==    by 0x8049B5C: TokentheString (glLib.c:244)
    ==18652==    by 0x80491BC: PopulateData (glLib.c:22)
    ==18652==    by 0x8048E59: main (FlyComp.c:19)
    ==18652==
    ==18652== Invalid read of size 4
    ==18652==    at 0x80493AA: PopulateData (glLib.c:24)
    ==18652==    by 0x8048E59: main (FlyComp.c:19)
    ==18652==  Address 0x4030818 is 0 bytes after a block of size 408 alloc'd
    ==18652==    at 0x40055E2: realloc (vg_replace_malloc.c:306)
    ==18652==    by 0x8049B5C: TokentheString (glLib.c:244)
    ==18652==    by 0x80491BC: PopulateData (glLib.c:22)
    ==18652==    by 0x8048E59: main (FlyComp.c:19)
    ==18652==
    The red color highlighted line is the one which shows that i am over running. I dont really see anything wrong over there. Anyone find pit fall there?

    thanks a lot

    ssharish

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Is the realloc'd Tokenised being freed here?
    Code:
    free(ExtraToken);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Hi Dave,

    Well, that was one of my confusion which i had. The Tokensized string is freed in the calling function 'Yes'. But what about the ExtraToken thats just used for the reallocation purpose like you can see what i am doing with that.

    ssharish

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Oh, wait. ExtraToken is NULLed, so that line is a no-op.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Dave_Sinkula View Post
    Oh, wait. ExtraToken is NULLed, so that line is a no-op.
    My confusion here was, when i free the ExtraToken i get seg fault. But when NULL it and then free, it dons't give me seg fault. I dont understand that bit?

    ssharish

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You take your life into your own hands when you free ExtraToken, since it is also Tokenised. And you're still using that.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    The second param should be const char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ssharish2005 View Post
    My confusion here was, when i free the ExtraToken i get seg fault. But when NULL it and then free, it dons't give me seg fault. I dont understand that bit?

    ssharish
    free(NULL) doesn't do ANYTHING (other than take a few nanoseconds to call into free() which does
    Code:
    void free(void *ptr)
    {
       if (ptr == NULL)
          return;
       ...  // Actual code to free memory block. 
    }

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    But I only get memory leaks. How about a test case? The code runs fine without crashes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Elysia View Post
    But I only get memory leaks. How about a test case? The code runs fine without crashes.
    Hi Elysia, the code works fine with no error. You mentioned about the memory leak. Where was it.I through there was no leaks at all. And perhaps if you could at my first post in this thread. I have copied the output generated by the valgrind. It complains about the block overrun which i am not able to figure it out.

    Does anyone see any mistakes i am doing or the usage of realloc function. Do i have to free ExtraToken is that ok if i leave it like that.

    Maps, ok freeing null wouldn't have any affect. So how do i free the ExtraToken or shouldn't i have to do that. I am pretty sure if i don't do that, it would be a memory leak.

    thank you

    ssharish

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Do i have to free ExtraToken is that ok if i leave it like that.
    No, freeing ExtraToken (Now 'Tokenized') then using Tokenized would be bad -- giving you a segfault.

    I seriously think you need to redesign this program. Perhaps split it up into a few functions.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    Quote Originally Posted by ssharish2005 View Post
    Hi Elysia, the code works fine with no error. You mentioned about the memory leak. Where was it.I through there was no leaks at all. And perhaps if you could at my first post in this thread. I have copied the output generated by the valgrind. It complains about the block overrun which i am not able to figure it out.
    The memory leaks are mostly, I think, due to you returning an allocated pointer which I do not free. It's a 2D array and I have no size, so it could be somewhat difficult.
    I just asked if you have a test case because I can't even reach the line in the function that it complains causes the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. Replies: 4
    Last Post: 01-15-2002, 11:04 PM
  5. pointers
    By fanaonc in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 01:18 AM

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