Thread: Realloc problem

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    74

    Realloc problem

    i am trying to use DMA to create an array . The array is to be filled with the position of free records(records that have been marked free for use using a flag with a value of 1 )

    here is the code :
    Code:
     # include <stdio.h># include <stdlib.h>
    
    
    //----struct template---//
    struct st_student 
    {
        char name[20] ;
        int st_id ;
        int age ;
        char gender ; 
        int flag ; } ; 
    
    
    int main(void)
    {
        //--declare file pointer and open file--//
        FILE *FilePointer ; 
        FilePointer = fopen("students_flag.bin" ,"rb" ) ; 
        
        int i = 0 ; 
        int j = 0 ; 
        int * Dma_Array ; 
        int size_of_flags_array  ; 
        
        struct st_student student_records ; 
        
        
        if(FilePointer == NULL )
        {
            printf("error opening the students flag binary file \n " ) ; 
            exit (1) ; 
        }
        
        Dma_Array = (int * ) malloc( 0 * sizeof(int) ) ; 
        
        while(! feof (FilePointer))
        {
            if(ferror(FilePointer ) ) 
            {
                printf("Ferror on file \n " ) ; 
                exit (1) ; 
            }
            
            //---read the structure---//
            fread(&student_records , sizeof(struct st_student) , 1 , FilePointer ) ;
                
            //_______________________________print flags to screen for reference__________________________________________-//
            printf("flag is %d \n " , student_records.flag ) ; 
            
            //--if flag == 1 record is marked as free for use, copy position of free record to array ----//
            if(student_records.flag == 1)
            {
                Dma_Array = (int *) realloc(Dma_Array , (j+1)*sizeof(int)) ; 
                Dma_Array[j] = i ; 
                j++ ; 
            }//end if 
            i++;
        }//end while
        
        size_of_flags_array = (sizeof(Dma_Array) / sizeof(Dma_Array[0])) ; 
        
        //___________print size of array to screen for reference__________//
        printf("size of flags array is %d \n " , size_of_flags_array )  ; 
        
        //---print position of flags to screen----//
        for(i = 0 ; i < size_of_flags_array ; i ++)
        {
            printf("position of free records is %d \n " , Dma_Array[i] ) ; 
        }//end for 
        
        
        return 0 ; 
    }//End of main
    output to terminal is
    Code:
     
    flag is 0 
    flag is 0 
    flag is 1
    flag is 0 
    flag is 0 
    flag is 1 
    flag is 0 
    flag is 0 
    flag is 1
    flag is 0 
    flag is 0 
    size of flags array is 1 
    position of free records is 2
    i think there is a problem with my realloc call not sure what though any help is great , thanks

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I'm not sure what your problem is, but you need to know that line #34, "malloc(0)", is implementation-defined.
    Last edited by GReaper; 04-18-2015 at 05:36 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    hey im not sure what you mean by that could you expand a little ?

    also i just noticed that if i hard code the
    Code:
    for(i = 0 ; i  < ; size_of_flags_array )
    to
    Code:
    for(i = 0 ; i < 3 ; i ++)
    it prints out the correct values . So possibly my problem is in getting the size of the arrray
    Code:
    size_of_flags_array = (sizeof(Dma_Array) / sizeof(Dma_Array[0])) ;

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    so possibly it is getting the size of the pointer (4 ) / size of an int (4)
    giving 1 which is why its not printing out correctly.
    ??

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It's just that there's no standard way for a library to handle a "malloc(0)". It can either return NULL, or return non-NULL. Both are accepted by the standard, that is why it's called "implementation-defined". If you rely on the one or the other, you risk making your code non-portable. But I guess that wasn't your problem here.
    Devoted my life to programming...

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by alien1 View Post
    so possibly it is getting the size of the pointer (4 ) / size of an int (4)
    giving 1 which is why its not printing out correctly.
    ??
    Yep, you probably nailed it.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    Ah okay i understand now . thanks, I will change the malloc and when i realloc.
    Thanks : )

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    yes if i use
    Code:
    for (i = 0 ; i < j ; i ++ )
    it prints out the correct results
    and that way im not just hardcoding how many flags there should be in.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Whist this is so, the result (either way) is still valid to pass onto realloc.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    in the question i will have to check again for free flags after they have been modified . So i was going to turn that into a function. to keep main less cluttered and it was save me writing out the same piece of code again . my question is if i make it a function when the function ends will the memory taken by malloc and realloc be marked as free by the os ? or would it be better to use malloc in the main and then pass the pointer to the function ?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Anything you get from malloc/realloc remains with you until you explicitly call free(), or your program exits.

    Whether you allocate in main and pass the pointer around, or allocate in a function and return the pointer - it's entirely up to you.

    There are FAQ entries on using malloc and realloc, you should take a look.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Realloc Problem
    By KittenFace in forum C Programming
    Replies: 2
    Last Post: 11-05-2010, 10:19 AM
  2. problem with realloc()
    By TheDarkAvenger in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 12:16 PM
  3. problem with realloc
    By oooflascheooo in forum C Programming
    Replies: 5
    Last Post: 07-31-2008, 04:08 AM
  4. Problem with realloc
    By wd_kendrick in forum C Programming
    Replies: 4
    Last Post: 05-23-2008, 02:41 AM