61 62 63 64
234 235 p r i n t f ( % 5 d :, i+1); g e t s ( s t u d e n t _ n a m e [ i ] ) ; if (student_name[i][0] == \ 0 ) i = MAX; p r i n t f (\ n :\ n ); 6 1 for (i = 0; student_name[i][0]!= \ 0&& i < MAX; i++) (Static Memory Allocation) (Dynamic Memory Allocation) C,,, return 0; p r i n t f (\ n :% 5 d :, i+1); p r i n t f (% s, student_name[i]); 6 2, C [ 6-1] # i n c l u d e < s t d i o h > #define MAX 100 char student_name[max][10]; int i; for (i = 0; i < MAX; i++) [ 6-1]10 100 98 (980 ), 100, 100, MAX, 1000 MAX1000 [ 6-2]
236 237 # i n c l u d e < s t d i o h > # i n c l u d e < s t r i n g h > char text[1024][1024]; 6 3 3,,, stdlibh malloch strcpy(text[0], ); p r i n t f (% s \ n, text[0]); return 0; [ 6-1],, malloc( ), calloc( ), realloc( ) [ 6-2]1K 1K 1M 1M, [ 6-1] MAX100000 [ 6-2],, Q uiz malloc( ), (size), NULL(stdlibh ) char 1 malloc( ) char ANSI C void void (generic), malloc( ) C,
238 239 void cast malloc( ) 100 malloc( ), int student_size, index; char *student_name; p r i n t f (\ n ); s c a n f (% d, &student_size); p r i n t f (\ n ); f f l u s h ( s t d i n ) ; student_name = (char*) malloc(10*student_size); if(student_name == NULL) p r i n t f (\ n \ n ); e x i t ( 0 ) ; char *string; str = (char*)malloc(100); if((str=(char*) mallor(100))==null) if(str = = NULL) p r i n t f (Not Enough memoryn ); e x i t ( 1 ) ; 50 50 # i n c l u d e < s t d i o h > # i n c l u d e < s t d l i b h > for (index = 0; index < student_size; index++) p r i n t f ( % d:, index+1); gets(student_name + (index*10)); p r i n t f (\ n \ n \ ); for (index = 0; index < student_size; index++) p r i n t f (\ n % d, index+1); p r i n t f (% s, student_name + (index*10)); f r e e ( s t u d e n t _ n a m e ) ; return 0;
240 241 malloc( ) fflush( ) fflush(stdin), student_name 10byte student_size Q uiz char *student_name; student_name = (char*) malloc(10*student_size); calloc( ) 0, #include <sdtlibh> #include <stdioh> #include <stringh> void *calloc(size_t number_items, size_t block_size); #include <stdlibh> int size, index, *ptr; p r i n t f (\ n ); s c a n f (% d, &size); ptr = (int *) calloc(size, sizeof(int)); if(ptr == NULL) p r i n t f (\ n ); p r i n t f ( \ n ); for (index = 0; index < size; index++) p r i n t f ( ); s c a n f (% d,ptr + (index*sizeof(int))); p r i n t f (\ n \ ); for (index = 0; index < size; index++) f r e e ( p t r ) ; p r i n t f (% d \ t,*(ptr + (index*sizeof(int)))); [ 6-4]calloc( ) size int
242 243 calloc( ) pre-ansi char, ANSI C void int s c a n f (% d,& s i z e ); ptr = (int *) calloc (size, sizeof(int)); realloc( ) malloc( ) calloc( ) f r e e ( m e s s a g e ) ;, realloc( ) realloc( )size originalblock originalblock, size [ 6-5]realloc( ) #include <stdioh> #include <stdlibh> #include <stringh> char buf[80], *message; p r i n t f (\ n ); g e t s ( b u f ) ; message = (char *)realloc(null, strlen(buf) + 1); strcpy(message, buf); p r i n t f (\ n % s \ t % p \ n, message, message); p r i n t f (\ n ); g e t s ( b u f ) ; message = (char *)realloc(null, strlen(buf) + 1); strcpy(message, buf); p r i n t f ( % s \ t % p \ n, message, message); free( ) free( ), free( ), [ 6-3], [ 6-4], [ 6-5] free( ) free( ) [ 6-6] # i n c l u d e < s t d i o h > # i n c l u d e < s t d l i b h >
244 245 int *ptr; ptr = (int *) malloc(sizeof(int)); *ptr = 1234; p r i n t f (*ptr : %d\n, *ptr); f r e e ( p t r ) ; p r i n t f (*ptr : %d\n, *ptr); [ 6-6] free( ),, memset( ) c (unsigned char ) block size memset( ), 0 block, [ 6-7] memset( ) #include <stdioh> #include <stdlibh> #include <stringh> struct student_node ; int number; char name[20]; struct student_node *student; student = (struct student_node *) malloc(sizeof (struct s t u d e n t _ n o d e ) ) ; p r i n t f ( : %d, % s \ n, student -> number, student -> n a m e ); memset(student, 0, sizeof(struct student_node)); p r i n t f ( : %d, % s \ n, student -> number, student -> n a m e ); f r e e ( s t u d e n t ) ; [ 6-7] malloc( ), memset( ) student = (struct student_node *) malloc(sizeof (struct s t u d e n t _ n o d e ) ) ; p r i n t f ( : %d, % s \ n, student -> number, student -> n a m e ); - 842150451, malloc( ) memset( ) student (0)
246 247 memset(student, 0, sizeof(struct student_node)); p r i n t f ( % d, % s \ n, student ->number, student -> n a m e ); memcpy( ) from to size, c c, from size c [ 6-8]memcpy( ) memmove( ) memmove( ) 2 from size to size, memmove( ) from, to [ 6-9]memmove( ) #include <stdioh> #include <stringh> char string1[20]=s t r i n g 1 ; char string2[20]=new string ; char *ptr; ptr = (char *)memcpy(string1, string2, 20); if (ptr) e l s e p r i n t f ( % s \ n, string1); p r i n t f ( \ n ); 6 4 #include <stdioh> #include <stringh> char string[20]=0 123456789 a b c d e f g h i ; memmove(string+5, string, 10); p r i n t f (memmove % s \ n, s t r i n g ); [ 6-9]string 10 "0123456789"string memcpy( ) memmove( ) memcpy( ), to from (Linked List) (Tree) (Node) [ 6-1]
248 249, [ 6-1 ], h e a d rear( tail) NULL [ 6-10] #include <stdioh> #include <stringh> #include <stdlibh> char str1[50] = memset( ), memcpy( ), memmove( ) ; char str2[50] = 1 234567890 ; char str3[50] = a b c d e f g h i j k l m n ; p r i n t f (string1 = %s\n, str1); memset(str1+3, *, 8); p r i n t f (memset( ) = %s\n, str1); p r i n t f (string2 = %s\n, str2); memcpy(str2+2, str2+5, 3); p r i n t f (memcpy( ) = %s\n, str2); [ 6-10]memset( ), memcpy( ), memmove( ) memset( ) memset( ) memset( ) str+3 str14 str14 11 * char str1[50] = memset( ), memcpy( ), memmove( ) ; p r i n t f (string1 = %s\n, str1); memset(str1+3, *, 8); p r i n t f (memset( ) = %s\n, str1); memcpy( ), str2+5( 6) 3( 678) str2+2( 2), char str2[50] = 1 234567890 ; p r i n t f (string2 = %s\n, str2); memcpy(str2+2, str2+5, 3); p r i n t f (memcpy( ) = %s\n, str2); memmove( ) memcpy( ),, p r i n t f (string3 = %s\n, str3); memcpy(str3+2, str3+5, 3); p r i n t f (memmove( ) = %s\n, str3); char str3[50] = a b c d e f g h i j k l m n ; p r i n t f (string3 = %s\n, str3); memcpy(str3+2, str3+5, 3); p r i n t f (memmove( ) = %s\n, str3);
250 251 # i n c l u d e < s t d i o h > # i n c l u d e < s t d l i b h > # i n c l u d e < s t r i n g h > struct student_node ; int number; char name[20]; struct student_node *next; char number, name[20]; struct student_node *head, *rear, *ptr; head = ptr = NULL; for (number = 1; ;number++) p r i n t f ( %d:, number); g e t s ( n a m e ) ; if (name[0] == \ 0 ) break; ptr = (struct student_node *) malloc(sizeof(struct s t u d e n t _ n o d e ) ) ; if (ptr == NULL) p r i n t f ( \ n ); e x i t ( 0 ) ; ptr -> number = number; strcpy(ptr -> name, name); ptr -> next = NULL; if (head == NULL) e l s e head = rear = ptr; rear -> next = ptr; rear = ptr; /* */ p r i n t f (\ n \ ); ptr = head; while (ptr!= NULL) p r i n t f (%d\t: %s\n, ptr -> number, ptr -> n a m e ); ptr = ptr -> n e x t ; /* while (head!= NULL) return 0; ptr = head -> n e x t ; f r e e ( h e a d ) ; head = ptr; [ 6-11], ptr= (struct student *) malloc(sizeof(struct student)); rearnext, rear if(head == NULL) head = rear = ptr;
252 253 e l s e rear -> next = ptr; rear = ptr;, [ 6-2] head rear ptr = head; while (ptr!= NULL) p r i n t f (%d\t: %s\n, ptr -> number, ptr -> n a m e ); ptr = ptr -> n e x t ;, head rear while (head!= NULL) ptr = head ->n e x t ; f r e e ( h e a d ) ; head = ptr; free( ), head int i; char *ptrnull, *cur, ch; c u rp t r(char*) malloc(27); for (i0, ch Z ; i < 26; i, ch ) * c u r + +ch; * c u r \ 0 ; p r i n t f (% s, ptr); free(ptr);, ( 100, Ctrl+Z ) while (head!= NULL) f r e e ( h e a d ); head = head -> n e x t ;
254,? strcat(const char* s1, const char* s2) s1s2 s1 s3s1s2, s3