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


  Make the array freely.   

  1. ..clause 1..: Fault of array
  2. ..clause 2..: Securing of memory
  3. ..clause 3..: Expand the number of dynamic array elements.

[1] Fault of array

Chapter 13 explained the usage of the array.
This array : to the handling of a large amount of data though it is very efficient measures.
There are actually some faults, and the practicality is a little low.

The maximum fault of the array is not revokable while the number of elements is programmed.
The number of elements is only specified directly by the constant when the array is declared.
The user inputs while executing it, and the value cannot be used.


[   Revokable environment   ]
In the compiler named GCC because of an original enhancing
Be revokable while the number of elements is programmed.
Moreover, a similar function is added as for C99.
(It is not possible to do in C++. )
This is inconvenient though makes the program that operates by various purposes.
For instance, when you make software that manages the salary of the employee in the company
The array that memorizes employee's salary is needed ..more than the number of employees...

However, it is various in the world from a company several employees to the company of several thousand people.
When the number of elements is adjusted to 10 pieces, it is not possible to use it in the company of 11 people or more.
Oppositely, when the number of elements is adjusted to 10,000, 9990 remainder is useless in the company of ten people.
Because the memory is used also for the useless amount, it becomes useless of a huge memory.

Because the number of array elements cannot be freely changed in this manner
It is difficult to use the memory effectively, and lacks the practicality.

It returns to contents.


[2] Securing of memory

It explained it was inconvenient because the array was not able to change the number of elements freely in the preceding clause.
Therefore, malloc ([emuarokku]) function that freely makes the array is prepared.
To use the malloc function, # include should do < stdlib.h >.
The usage of the malloc function is as follows.


ポインタ変数 = malloc(必要なメモリのバイトサイズ);
メモリを確保できなかった場合は NULL が返る。
Because the initial address of the secured array is substituted for the returned pointer variable
If the [] operator is used for this, it is possible to use it as well as the array.

[   Safe   ]
Do the person who doesn't understand the meaning of two lines above over again from Chapter 15.
Advance previously after it understands because it is C language.
Because being able to specify by the malloc function is a size of each byte
Use the sizeof operator to secure the array of an arbitrary number of elements.
The memory secured by the malloc function might be called a heap.
Moreover, the array secured for the heap might be called a dynamic array.

NULL is returned when failing in the memory securing.
Because it cancels naturally if this is used as it is
It is necessary to check the return value of the malloc function.

However, when failing in a little memory securing
The entire system will be an at any moment frieze due to serious memory shortage.
There are no measures other than the forced ending.


[   Heap   ]
Area where big size used for the long term memory is stored.

[   Dynamic array   ]
Use the malloc function etc.
Array of arbitrary size prepared during program execution.
Though the memory secured by the malloc function remains until the program ends
Use and liberate free (free) function when the memory becomes unnecessary.
So that a useless memory will keep remaining when this is forgotten
Call the free function when you use the malloc function.
The usage of the free function is as follows.

free(ポインタ変数); 

[ Free function when   ending   ]
There is an exception though the free function explained the call without fail, too.
Even if you do not use the free function immediately before the program ends
It is because the memory is liberated at the same time as the program's ending.
However, I think that I should still call it if portability etc. are considered.
Specify the pointer variable that stores the return value of the malloc function for the pointer variable.

The following program dynamically secures the array of ten element numbers of int types.

 
#include <stdio.h> #include <stdlib.h> 

int main()  
{
	int i;  
	int *heap;  
	heap = (int *)malloc(sizeof(int) * 10);  if (heap == NULL) exit(0);  

	for (i = 0;i < 10;i++) { 
		heap[i] = i; 
	}
	printf("%d\n",heap[5]);  

	free(heap);  

	return 0;
}
Because the size of each byte of one int type variable is requested by sizeof(int)
The memory of as many as ten int type variables is secured by multiplying it ten.

The address that the malloc function returns is a pointer of the void type.
Because this type is a type that it is possible to substitute it for any pointer variable
To (int *) though it need not be Cast in reality
It is [demasu] ..the error.., except for no Cast in the C++ compiler.

Call the exit function and cancel when failing in the memory securing.
The exit function is a function that ends the program.
To use the exit function, # include should do < stdlib.h >.
By the way, the abort function might be used at the forced ending by the error.

Call the free function and liberate it when you finish using the secured array.


[ The realities of   malloc function   ]
Because the malloc function can make a dynamic array of a size favorite as liked it
The mechanism actually only puts the mark to the memory though it is very convenient.

This : in writing the name in the cake that exists in the refrigerator and the same thing.
Though there is no problem if all families do not eat others' cakes according to the name
There is a possibility that the cake is eaten by the misunderstanding by other people enough.

There is a character to look like the malloc function, and it is unexpectedly difficult to use it well.
Therefore, use an array as usual as possible at the program.
You will use the malloc function only by a necessary by all means part.

It returns to contents.


[3] Expand the number of dynamic array elements.

Dynamic arrays of a favorite number of elements can be made from the malloc function.
However, with this it described first, and, the number of array elements cannot be changed.
The problem is not completely said the solution.
Then, realloc (rear lock) function to which the number of elements is changed is prepared.
The usage of the realloc function is as follows.


新しいポインタ変数 = realloc(以前のポインタ変数,必要なメモリのバイトサイズ);
Specify the address of the memory secured by the malloc function for the pointer variable before.
The realloc function secures the size new with contents maintained memory.
Though the address of the enhanced memory is returned to a new pointer variable
The same variable as the pointer variable before can be specified if there is no special reason.

The following program changes the number of dynamic array elements by the realloc function.

 
#include <stdio.h> #include <stdlib.h> 

int main(void)  
{
    int *heap;      heap = (int *)malloc(sizeof(int) * 10);      heap = (int *)realloc(heap,sizeof(int) * 100);      free(heap);  
    return 0;
}
The number of elements is increased from 10 pieces to 100 pieces by the realloc function.

[ Let's decrease the   call frequency   ].
When the realloc function is called many times, the memory is cluttered.
Such a state is called a fragmentation, and it becomes unstable.
It secures it for a level large decision that is the first malloc function.
It is necessary to secure it greatly by one time when you call the realloc function.

It returns to contents.


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