C-language implementation of linked list (I.)

Source: Internet
Author: User
Tags arrays count function prototype int size integer printf

Preparation: Dynamic memory allocation
One, why use dynamic memory allocation
But when we are not learning the linked list, we always use an array if we want to store a larger number of the same or same structure of data. For example, we want to store a class score for a student, always define a float (existence of 0.5 points) array:
float score[30];
However, when using arrays, there is always a problem that bothers us: how big should the array be?
In many cases, you are not sure how large an array to use, such as the example above, you may not know the number of students in the class, then you need to define the array large enough. This way, your program applies a fixed size of memory space that you think is large enough to run. Even if you know the number of students in the class, but if the number of people has increased or decreased for some particular reason, you have to rewrite the program to enlarge the array's storage range. This allocating fixed size memory allocation method is called static memory allocation. However, this method of allocating memory is a serious flaw, especially when dealing with certain problems: in most cases, a large amount of memory space is wasted, in a few cases, when the array you define is not large enough, it may cause subscript error, even serious consequences.
So is there any other way to solve this kind of outer body? Yes, that's dynamic memory allocation.
Dynamic memory allocation refers to the method of allocating memory allocated or reclaimed in the process of program execution dynamically. Dynamic memory allocation does not require the allocation of storage space as a static memory allocation method, such as arrays, but is allocated by the system to the needs of the program, and the size of the allocation is the size of the program requirements. From the above dynamic, static memory allocation comparison can know the dynamic memory allocation phase of the characteristics of the Jingtai memory allocation:
1, do not need to allocate storage space beforehand;
2, the allocation of space can be expanded or reduced according to the needs of the program.
Ii. How to realize dynamic memory allocation and its management
To achieve dynamic allocation of storage space according to the needs of the program, you must use the following functions
1. malloc function
The prototype of the malloc function is:
void *malloc (unsigned int size)
The effect is to allocate a contiguous space of length to size in the dynamic storage area of the memory. The parameter is an unsigned integer number, and the return value is a pointer to the starting address of the allocated contiguous storage domain. It is also important to note that a null pointer is returned when the function does not successfully allocate storage space, such as insufficient memory. So when calling this function, you should detect if the return value is null and perform the appropriate action.
The following example is a dynamically assigned program:
#include
#include
Main ()
{
int Count,*array; /*count is a counter in which the array is an integer pointer and can also be understood to point to the first address of an integer array.
if ((Array (int *) malloc (10*sizeof (int)) ==null)
{
printf ("The storage space cannot be allocated successfully. ");
Exit (1);
}
for (count=0;count〈10;count++)/* To assign a value to an array * *
Array[count]=count;
for (count=0;count〈10;count++)/* Print array elements * *
printf ("%2d", Array[count]);
}
In the example above, 10 integer storage areas are dynamically assigned and then assigned and printed. In the example, the IF (array (int *) malloc (10*sizeof (int)) ==null) statement can be divided into the following steps:
1 allocate 10 integer contiguous storage space and return an integer pointer to its starting address
2 Assign the integer pointer address to the array
3) Detect whether the return value is null
2, free function
Because the memory area is always limited, must be limited to allocate, and a program to save resources, so when the allocated memory area is not used, it should be released, so that other variables or programs to use. Then we're going to use the free function.
Its function prototype is:
void free (void *p)
The function is to release the memory area that the pointer p points to.
The argument p must be a pointer that is returned when the malloc function or the Calloc function (another function that dynamically allocates a storage area) was previously called. Passing other values to the free function is likely to cause crashes or other catastrophic consequences.
Note: What is important here is the value of the pointer, not the pointer itself, which is used to request dynamic memory. Cases:
int *p1,*p2;
P1=malloc (10*sizeof (int));
P2=P1;
......
Free (p2) * or free (p2)/
The malloc return value is assigned to the P1, and the P1 value is assigned to P2, so p1,p2 can be used as a parameter to the free function at this time.
The malloc function is assigned to a storage area.
The free function frees up memory areas that have not been used.
Therefore, these two functions can be implemented to the memory region dynamic allocation and simple management.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.