290 Intermediate C Programming
DecP ack_de stroy ( dp );29
return EXIT _SUCCES S ;30
}31
Here is the Makefile:
GCC = gcc1
CFLAGS = -g - Wall - Wshadow2
VALGRIND = valgrind -- tool = memc h eck -- verbose --log - file3
4
decpack : d e cp ack . c decpack . h main .c5
$( GCC ) $ ( CFLAGS ) decpack .c main .c -o $@6
$( VALGRIND )= valgr indlog ./ decpack7
8
clean :9
/ bin / rm -f *. o decpack * log10
17.3 Binary File and Pointer
Section 16.6 describes how to use fread and fwrite to read and write the attributes of
an object. What happens if the object contains one or more pointers? Consider the following
example:
// str uctfile .c1
#in clude < stdio .h >2
#in clude < stdlib .h >3
#in clude < time .h >4
#pragma pack (1) // tell compiler not to pad any space5
typedef s t ru ct6
{7
int length ;8
int * data ;9
} Array ;10
// for simplicity , this progr a m does not check errors11
int main ( i n t argc , char ** argv )12
{13
int length = 10;14
char * f i lename = " data ";15
// create an object16
Array * aptr1 = NULL ;17
printf (" sizeof ( aptr1 ) = % dn " , ( i nt ) s i z e o f ( aptr1 ) );18
aptr1 = malloc ( s i z e o f ( Array ) );19
printf (" sizeof ( aptr1 ) = %d , sizeof ( Array ) = %d n" ,20
( i n t ) s i z e o f ( aptr1 ) , ( i n t ) s i z e o f ( Array )) ;21
// allocat e memory for the data22
aptr1 -> length = length ;23
aptr1 -> data = malloc ( s i z e o f ( i n t ) * ( aptr1 -> length )) ;24
printf (" sizeof ( aptr1 ): %d , sizeof ( aptr1 -> data ): %d n" ,25
( i n t ) s i z e o f ( aptr1 ) , ( i n t ) s i z e o f ( aptr1 -> data )) ;26
Programming Problems Using Structure 291
// ini tialize the values of the array27
int ind ;28
for ( ind = 0; ind < ( aptr1 -> length ); ind ++)29
{30
aptr1 -> data [ ind ] = ind ;31
}32
// save the data to a file33
FILE * fptr = fopen ( filename , "w ");34
// write the data to the file35
i f ( fwrite ( aptr1 , s i z e o f ( Array ) , 1, fptr ) != 1)36
{37
// fwrite fail38
return EXIT _FAILUR E ;39
}40
printf (" ftell ( fptr ) = % dn ", ( in t ) ftell ( fptr )) ;41
fclose ( fptr );42
43
// fill the array with random numbers44
// ensure the heap contains garbage before re l easing it45
srand ( time ( NULL )) ; // set the seed of the random number46
for ( ind = 0; ind < ( aptr1 -> length ); ind ++)47
{48
aptr1 -> data [ ind ] = rand ();49
}50
51
// release memory52
free ( aptr1 -> data );53
free ( aptr1 );54
// read the data from the file55
Array * aptr2 = NULL ;56
aptr2 = malloc ( s i z e o f ( Array ) );57
fptr = fopen ( filename , " r") ;58
i f ( fread ( aptr2 , s i z e o f ( Array ) , 1, fptr ) != 1)59
{60
// fread fail61
return EXIT _FAILUR E ;62
}63
// add the data64
int sum = 0;65
for ( ind = 0; ind < ( aptr2 -> length ); ind ++)66
{67
sum += aptr2 -> data [ ind ];68
}69
printf (" sum = %d n" , sum ) ;70
// release memory71
free ( aptr2 );72
return EXIT _SUCCES S ;73
}74
292 Intermediate C Programming
Assume this program runs on a 64-bit (8 bytes) machine and furthermore, that each
integer uses 4 bytes. Also assume that the program never returns EXIT FAILURE. What is
the output of this program? Here is a sample output:
sizeof(arrptr1) = 8
sizeof(arrptr1) = 8, sizeof(Array) = 12
sizeof(arrptr1) = 8, sizeof(arrptr1 -> data) = 8
ftell(fptr) = 12
sum = 1289469162
The value of sum changes if the program is run again. The array’s elements are set to
random values (line 49) after the data has been written to the file (line 36). When line
59 reads the data, the elements’ values should be 0, 1, 2, ..., right? Wrong. If we run this
program with valgrind, it will tell us that the program has an “Invalid read” at line 68.
Why? The reason is that we cannot use fwrite to save the value of a pointer because this
value is a memory address. The address is meaningless when saved into a file. Instead of
saving the address, the program must save the data stored at the address. To summarize, it
makes no sense to write memory addresses to a file; nor does it make sense to read memory
addresses from a file.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset