- ..clause 1..: Fault of array
- ..clause 2..: Securing of memory
- ..clause 3..: Expand the number of dynamic array elements.
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.
This is inconvenient though makes the program that operates by various purposes.
[ 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++. )
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 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.
Because the initial address of the secured array is substituted for the returned pointer variableポインタ変数 = malloc(必要なメモリのバイトサイズ); メモリを確保できなかった場合は NULL が返る。
Because being able to specify by the malloc function is a size of each byte
[ 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.
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.
Though the memory secured by the malloc function remains until the program ends
[ Dynamic array ]
Use the malloc function etc.
Array of arbitrary size prepared during program execution.
free(ポインタ変数);
Specify the pointer variable that stores the return value of the malloc function for the pointer variable.
[ 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.
The following program dynamically secures the array of ten element numbers of int types.
Because the size of each byte of one int type variable is requested by sizeof(int)#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; }
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.
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.
Specify the address of the memory secured by the malloc function for the pointer variable before.新しいポインタ変数 = realloc(以前のポインタ変数,必要なメモリのバイトサイズ);
The following program changes the number of dynamic array elements by the realloc function.
The number of elements is increased from 10 pieces to 100 pieces 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; }
[ 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.