Thread: Program Crashing - Possibly Related to Dynamic Memory Allocation

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    132

    Program Crashing - Possibly Related to Dynamic Memory Allocation

    I wrote a program that tries to store the whole content of a file (its own source code) in a pointer to char variable, and then tries to print that content when the EOF is reached in the file. The program is crashing so far. I added the following line to it, so that I can clearly see the character just read:

    printf( "***%c***\n", currentCharacter );

    The program compiles and links properly, but the output ends up being:

    Program Crashing - Possibly Related to Dynamic Memory Allocation-result-png

    The program is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int stringLength( char *arr );
    
    int main( void )
    {
        int counter = 0;
        
        char *charPtr = NULL;
        
        char currentCharacter;
        
        FILE *filePtr = fopen( "read_content_to_array.c", "r" );
        
        if ( filePtr != NULL )
        {
            while ( !feof( filePtr ) )
            {
                currentCharacter = fgetc( filePtr );
                
                printf( "***%c***\n", currentCharacter );
                
                if ( counter > 0 )
                {
                    if ( ( charPtr = realloc( charPtr, ( 1 + stringLength( charPtr ) ) * sizeof( char ) ) ) != NULL )
                    {
                        *( charPtr + counter ) = currentCharacter;
                        *( charPtr + counter + 1 ) = '\0';
                    }
                    else
                    {
                        printf( "Memory error...\n" );
            
                        return 1; 
                    }
                }
                else
                {
                    if ( ( charPtr = malloc( 2 * sizeof( char ) ) ) != NULL )
                    {
                        *( charPtr + counter ) = currentCharacter;
                        *( charPtr + counter + 1 ) = '\0';
                    }
                    else
                    {
                        printf( "Memory error...\n" );
            
                        return 1; 
                    }
                }
                
                ++counter;
            }
            
            if ( counter > 0 )
            {
                printf( "%s", charPtr );
        
                free( charPtr );
            }
    
            fclose( filePtr );
        }
        else
        {
            printf( "File error...\n" );
            
            return 1;
        }
        
        return 0;
    }
    
    int stringLength( char *arr )
    {
        int i = 0;
        int length = 0;
        
        while ( *( arr + i++ ) != '\0' )
        {
            ++length;
        }
        
        return length;
    }
    What am I doing wrong? Thanks in advance.

    [EDIT:] I tried using two pointer variables, as suggested to telmo_d in the last answer of the topic located in How to get address of the start of the memory allocated?, but the program still crashes. Here's the modified program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int stringLength( char *arr );
    
    int main( void )
    {
        int counter = 0;
        
        char *charPtr1;
        char *charPtr2;
        
        char currentCharacter;
        
        FILE *filePtr = fopen( "read_content_to_array.c", "r" );
        
        if ( filePtr != NULL )
        {
            while ( !feof( filePtr ) )
            {
                currentCharacter = fgetc( filePtr );
                
                printf( "***%c***\n", currentCharacter );
                
                if ( counter > 0 )
                {
                    if ( ( charPtr2 = realloc( charPtr1, ( 1 + stringLength( charPtr1 ) ) * sizeof( char ) ) ) != NULL )
                    {
                        charPtr1 = charPtr2;
                        
                        *( charPtr1 + counter ) = currentCharacter;
                        *( charPtr1 + counter + 1 ) = '\0';
                    }
                    else
                    {
                        printf( "Memory error...\n" );
            
                        return 1; 
                    }
                }
                else
                {
                    if ( ( charPtr1 = malloc( 2 * sizeof( char ) ) ) != NULL )
                    {
                        *( charPtr1 + counter ) = currentCharacter;
                        *( charPtr1 + counter + 1 ) = '\0';
                    }
                    else
                    {
                        printf( "Memory error...\n" );
            
                        return 1; 
                    }
                }
                
                ++counter;
            }
            
            if ( counter > 0 )
            {
                printf( "%s", charPtr1 );
        
                free( charPtr1 );
                free( charPtr2 );
            }
    
            fclose( filePtr );
        }
        else
        {
            printf( "File error...\n" );
            
            return 1;
        }
        
        return 0;
    }
    
    int stringLength( char *arr )
    {
        int i = 0;
        int length = 0;
        
        while ( *( arr + i++ ) != '\0' )
        {
            ++length;
        }
        
        return length;
    }
    Last edited by stdq; 05-13-2015 at 10:13 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You seem to be trying to free your malloc() memory twice. You only allocate memory for charPtr2 but you try to free both charPtr1 and charPtr2.

    Jim
    Last edited by jimblumberg; 05-13-2015 at 10:25 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    It's high time you read this FAQ and paid attention to it!
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Code:
                    if ( ( charPtr2 = realloc( charPtr1, ( 1 + stringLength( charPtr1 ) ) * sizeof( char ) ) ) != NULL )
                    {
                        charPtr1 = charPtr2;
                         
                        *( charPtr1 + counter ) = currentCharacter;
                        *( charPtr1 + counter + 1 ) = '\0';
                    }
    charPtr2 is only ever a temporary variable to make sure the realloc worked, before updating charPtr1.

    > 1 + stringLength( charPtr1 )
    Think carefully about what this is, and what you need when storing your data.

    You might also want to think about the overall efficiency of calling stringLength() every single time when the string just gets longer and longer.
    Compare the result of stringLength() with counter


    FWIW, you need to use better variable names.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Thank you both for your input. I'll carefully check the program and the FAQ.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 27
    Last Post: 09-27-2014, 06:54 AM
  2. A possibly foolish question about dynamic allocation.
    By manasij7479 in forum C++ Programming
    Replies: 4
    Last Post: 08-24-2011, 06:09 PM
  3. Dynamic Memory Allocation
    By schifers in forum C Programming
    Replies: 12
    Last Post: 05-14-2008, 01:49 PM
  4. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 12:25 AM
  5. Dynamic Memory Allocation
    By BoneXXX in forum C Programming
    Replies: 11
    Last Post: 03-26-2007, 01:43 AM

Tags for this Thread