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