※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Exercise 19   

  1. ..clause 1..: Basic knowledge
  2. ..clause 2..: Program readout
  3. Memo
  4. ..clause 4..: Description type
  5. ..clause 5..: Basic knowledge(example solution)
  6. ..clause 6..: Program readout(example solution)
  7. Memo(example solution)
  8. ..clause 8..: Description type(example solution)


[1] Basic knowledge


[  1-1  ]
What is called the array that can be freely made during program execution?

[  1-2  ]
Is it used for the long term, and what is called a big memory area of the size?
Example solution

It returns to contents.


[2] Program readout


[  2-1  ]
There is a problem in the transplant in the following program.
Explain what problem it is concisely.
 
#include <stdio.h> #include <stdlib.h> 

int main()  
{
	int i;  
	int *data;  
	
	data = (int *)malloc(sizeof(int) * 10);  

	return 0;
}
Example solution

It returns to contents.


Memo


[  3-1  ]
Exercise 16
..".. make the program that inputs the name, the age, and sex of three and displays it.
However, data is assumed to be a memory with the structure.
Moreover, the data input and the display are assumed to do making a function respectively special. 」
Remodel even the people how many based on the problem to input it.
If -1 is input to the age, it is assumed the end of input.
- Because the array number is int type, it only has to be able to treat even the maximum value of the int type.
Example solution

It returns to contents.


[4] Description type


[  4-1  ]
To store data if the array is used
Explain it dares to use a dynamic array concisely why.
Example solution

It returns to contents.


[5] Basic knowledge(example solution)


[  1-1  ]
Dynamic array

[  1-2  ]
Heap
Problem

It returns to contents.


[6] Program readout(example solution)


[  2-1  ]
Because it has not liberated a dynamic array
There is a possibility that the memory area uselessly remains.
Problem

It returns to contents.


Memo(example solution)


[  3-1  ]
 
#include <stdio.h> #include <string.h> #include <stdlib.h> 

typedef struct { 
	char name[256]; int 
	age; 
	int sex;  
} People;  

void InputPeople(People *data);  
void ShowPeople(People data);  

int main(void)  
{
	int i,count,datasize;  
	People *data;  
	
	datasize = 10;  
	data = (People*)malloc(sizeof(People) * datasize);  
	
	count = 0;  
	while (1) { InputPeople(&data
		[count]); 
		if (data[count].age == -1) break;  count++;  
		
		if (count >= datasize) { 
			datasize += 10; 
			data = (People*)realloc(data,sizeof(People) * datasize);  
		}
	}
	
	for (i = 0;i < count;i++) { 
		ShowPeople(data[i]); 
	}

	free(data);  
	
	return 0;  
}

void InputPeople(People *data)  
{
	Printf ("Name");  
	scanf("%s",data->name);  
	Printf ("Age");  
	scanf("%d",&data->age);  
	Printf ("Sex (one-..man.. two-woman)");  
	scanf("%d",&data->sex);  
	printf("\n");  
}

void ShowPeople(People data)  
{
	char sex[16];  
	
	Printf ("Name: %s\n" data.name);  
	Printf ("Age: %d\n" data.age);  
	
	 Strcpy (sex "Man");
	} else { 
		Strcpy (sex "Woman");  
	}
	
	Printf ("Sex: %s\n" sex);  
	printf("\n");
}

- Though neither InputPeople nor the ShowPeople function have changed
  If the InputPeople function is changed, it is possible to input easily and to do.
- To decrease the call of the realloc function, ten has been increased.
- When the free function is not called, it is a great demerit mark.
Problem

It returns to contents.


[8] Description type(example solution)


[  4-1  ]
Because it is freely decided while programming executing the number of elements
Because the memory can be efficiently treated.
Problem

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.