Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Hit the bug

Hit the bug

Scheduled Pinned Locked Moved Clever Code
debugginghelpc++announcementworkspace
4 Posts 4 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akur1
    wrote on last edited by
    #1

    Following things has found in MS VC++ 6.0 environment: Here is the code snippet:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define __COUNT 3
    
    void foo( char ** ppStr, char * pVal )
    {
        if ( NULL != *ppStr )
        {
            free( *ppStr );
            *ppStr = NULL;
        }
        *ppStr = (char*)malloc(sizeof(char));
        strcpy( *ppStr, pVal );
        return;
    }
    
    int main()
    {
        char * pVar = NULL;
        char szVal[__COUNT][32] = { "TEST_1", "TEST_2", "TEST_3" };
        for ( int i = 0; i < __COUNT; i++ )
        {
            foo( &pVar, szVal[i] );
            printf( "Pointer holder: 0x%0X, Allocated start address: 0x%0X, Value: %s\n", &pVar, &pVar[0], pVar );
        }
        return 0;
    }
    

    Run the code in DEBUG environment and get the following error message Debug Error! Program: E:\RA\MISC\Test_Mem\Debug\Test_Mem.exe DAMAGE: after Normal block (#51) at 0x00430030. (Press Retry to debug the application) Abort Retry _Ignore_ If you select the Ignore option then you will get the following result Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_1 Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_2 Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_3 But in case of RELEASE build no such error appears. This is really unusual. But it is the fact the code has error at malloc statement. The same would occur even if you run the exe from command line. Why the RELEASE build has failed to trace the error... :confused: Well the bug could be easily removed if we modify the malloc part as follows:

    *ppStr = (char*)malloc(sizeof(char) * ( strlen( pVal ) + 1 ));
    

    -- modified at 3:10 Thursday 5th October, 2006

    S S R 3 Replies Last reply
    0
    • A akur1

      Following things has found in MS VC++ 6.0 environment: Here is the code snippet:

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      #define __COUNT 3
      
      void foo( char ** ppStr, char * pVal )
      {
          if ( NULL != *ppStr )
          {
              free( *ppStr );
              *ppStr = NULL;
          }
          *ppStr = (char*)malloc(sizeof(char));
          strcpy( *ppStr, pVal );
          return;
      }
      
      int main()
      {
          char * pVar = NULL;
          char szVal[__COUNT][32] = { "TEST_1", "TEST_2", "TEST_3" };
          for ( int i = 0; i < __COUNT; i++ )
          {
              foo( &pVar, szVal[i] );
              printf( "Pointer holder: 0x%0X, Allocated start address: 0x%0X, Value: %s\n", &pVar, &pVar[0], pVar );
          }
          return 0;
      }
      

      Run the code in DEBUG environment and get the following error message Debug Error! Program: E:\RA\MISC\Test_Mem\Debug\Test_Mem.exe DAMAGE: after Normal block (#51) at 0x00430030. (Press Retry to debug the application) Abort Retry _Ignore_ If you select the Ignore option then you will get the following result Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_1 Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_2 Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_3 But in case of RELEASE build no such error appears. This is really unusual. But it is the fact the code has error at malloc statement. The same would occur even if you run the exe from command line. Why the RELEASE build has failed to trace the error... :confused: Well the bug could be easily removed if we modify the malloc part as follows:

      *ppStr = (char*)malloc(sizeof(char) * ( strlen( pVal ) + 1 ));
      

      -- modified at 3:10 Thursday 5th October, 2006

      S Offline
      S Offline
      Sarath C
      wrote on last edited by
      #2

      One more thing. Never use delete with malloc. use free instead Because the allocation and freeing up strategy may differ. This is not in the case of malloc. You should use the corresponding freeing function which which you are used to allocate the memory.

      -Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin

      My blog - Sharing My Thoughts, An Article - Understanding Statepattern

      1 Reply Last reply
      0
      • A akur1

        Following things has found in MS VC++ 6.0 environment: Here is the code snippet:

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        
        #define __COUNT 3
        
        void foo( char ** ppStr, char * pVal )
        {
            if ( NULL != *ppStr )
            {
                free( *ppStr );
                *ppStr = NULL;
            }
            *ppStr = (char*)malloc(sizeof(char));
            strcpy( *ppStr, pVal );
            return;
        }
        
        int main()
        {
            char * pVar = NULL;
            char szVal[__COUNT][32] = { "TEST_1", "TEST_2", "TEST_3" };
            for ( int i = 0; i < __COUNT; i++ )
            {
                foo( &pVar, szVal[i] );
                printf( "Pointer holder: 0x%0X, Allocated start address: 0x%0X, Value: %s\n", &pVar, &pVar[0], pVar );
            }
            return 0;
        }
        

        Run the code in DEBUG environment and get the following error message Debug Error! Program: E:\RA\MISC\Test_Mem\Debug\Test_Mem.exe DAMAGE: after Normal block (#51) at 0x00430030. (Press Retry to debug the application) Abort Retry _Ignore_ If you select the Ignore option then you will get the following result Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_1 Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_2 Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_3 But in case of RELEASE build no such error appears. This is really unusual. But it is the fact the code has error at malloc statement. The same would occur even if you run the exe from command line. Why the RELEASE build has failed to trace the error... :confused: Well the bug could be easily removed if we modify the malloc part as follows:

        *ppStr = (char*)malloc(sizeof(char) * ( strlen( pVal ) + 1 ));
        

        -- modified at 3:10 Thursday 5th October, 2006

        S Offline
        S Offline
        Steve S
        wrote on last edited by
        #3

        1. you wouldn't normally mix malloc and delete, since malloc doesn't do any construction on the thing it hands back. You should really be using free unless you are also using new. 2. The bug isn't particularly subtle. You are reimplementing a library function (strdup) and doing it badly. Allocating the wrong size is one of those 'obvious' things, although admittedly it's usually the size off by one type of error. In addition, you aren't even checking that malloc has returned a valid address, which is more subtle (although more common). 3. Why should the release build track the error? This will slow down allocation/deallocation of memory, and why should my application have reduced performance simply because another developer wasn't writing correct code? I'd spot issues like this during testing of the debug version (I'm not arrogant enough to believe I'd not make mistakes, of course), or for harder to spot stuff, use an appropriate tool like BoundsChecker or similar.

        Steve S Developer for hire

        1 Reply Last reply
        0
        • A akur1

          Following things has found in MS VC++ 6.0 environment: Here is the code snippet:

          #include <stdio.h>
          #include <stdlib.h>
          #include <string.h>
          
          #define __COUNT 3
          
          void foo( char ** ppStr, char * pVal )
          {
              if ( NULL != *ppStr )
              {
                  free( *ppStr );
                  *ppStr = NULL;
              }
              *ppStr = (char*)malloc(sizeof(char));
              strcpy( *ppStr, pVal );
              return;
          }
          
          int main()
          {
              char * pVar = NULL;
              char szVal[__COUNT][32] = { "TEST_1", "TEST_2", "TEST_3" };
              for ( int i = 0; i < __COUNT; i++ )
              {
                  foo( &pVar, szVal[i] );
                  printf( "Pointer holder: 0x%0X, Allocated start address: 0x%0X, Value: %s\n", &pVar, &pVar[0], pVar );
              }
              return 0;
          }
          

          Run the code in DEBUG environment and get the following error message Debug Error! Program: E:\RA\MISC\Test_Mem\Debug\Test_Mem.exe DAMAGE: after Normal block (#51) at 0x00430030. (Press Retry to debug the application) Abort Retry _Ignore_ If you select the Ignore option then you will get the following result Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_1 Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_2 Pointer holder: 0x12FF7C, Allocated start address: 0x430030, Value: TEST_3 But in case of RELEASE build no such error appears. This is really unusual. But it is the fact the code has error at malloc statement. The same would occur even if you run the exe from command line. Why the RELEASE build has failed to trace the error... :confused: Well the bug could be easily removed if we modify the malloc part as follows:

          *ppStr = (char*)malloc(sizeof(char) * ( strlen( pVal ) + 1 ));
          

          -- modified at 3:10 Thursday 5th October, 2006

          R Offline
          R Offline
          ricecake
          wrote on last edited by
          #4

          akur1 wrote:

          *ppStr = (char*)malloc(sizeof(char) * ( strlen( pVal ) + 1 ));

          When you malloc() something, you don't need to multiply by sizeof(char), since sizeof(char) is defined to be exactly 1. Always.

          -- Marcus Kwok

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups