Dynamic Memory Allocation in C Language

Source: Internet
Author: User

Why do we need to use functions to dynamically allocate memory? Isn't the system automatically allocate memory ??


Since someone may ask such a question, I 'd like to explain it here! First, let's get familiar with the computer memory!


There are four memory areas in the computer system:

1) STACK: store some defined local variables and parameters (formal parameters) in the stack );

2) character constant area: stores some character constants, such as char * p_str = "cgat", where "cgat" is stored in the character constant area;

3) Global zone: stores some global and static variables in the global zone;

4) Heap: the heap mainly uses the dynamically allocated storage space, that is, the dynamically allocated memory space we will talk about next.


When do we need to dynamically allocate memory space? For example. Int * P; we define a pointer to the int type P; P is used to store the value of an address, the reason we need to allocate space for the variable P is to give it a clear point. Let's take an example! You have now made a roadmap pointing to the direction, but you have not set this roadmap to a specific position. That is to say, the current roadmap is blind, in this way, we cannot use it to identify where the East is, where the West is, what the North is, and what the South is. Although we have defined a pointer variable in the computer's memory, we have not asked this variable to indicate the address of an exact int type variable, therefore, we must give it a clear direction. In this way, we need to define its direction by dynamically allocating memory!


We have encountered this situation when we first touched the pointer. int * P; P = & A; this method is not the dynamic memory allocation of the pointer. This is called the initialization of the pointer variable! Initialization also allows the pointer variable to be directed. Int * P; P = malloc (N * sizeof (type name); we allocated an address for a pointer Variable P through the malloc () function, in this way, the value we typed on the keyboard is stored in P, and then we can perform specific operations on this p, such as scanf ("% s", P) and so on.


When we end P operations, we need to release P's memory space. Why do we need to release the memory? As I have mentioned above, the dynamically allocated variables are stored in the heap, but the heap space is not infinitely large, maybe we may not be able to find anything when compiling a small program, but for those large programs, if we release the heap space in time, memory leakage will occur. The so-called memory leakage is because the heap space is dynamically allocated to the north, so when we use the dynamic heap space allocation, there is not enough space for us to use, in this way, we need to occupy the original space, that is, we will store other space for the value we typed. This will cause the original stored data to be destroyed, resulting in Memory leakage.



At the same time, when using the malloc () function, we should also note that when we release the space, we should also assign the original pointer variable to a null, that is, assign a null pointer, use it next time! What if we do not grant | null rows ?? The answer is: no! If we do not assign a null pointer, the original pointer variable becomes a wild pointer! What is a wild pointer? A wild pointer is a pointer that is not explicitly pointed to, and the system does not know where it will point. It is very dangerous to use a wild pointer. Therefore, when we use malloc () you must assign a null pointer to all functions! Compared with the malloc () function, the calloc () function does not need to be null. This is because () the system will automatically assign the original pointer to a null pointer, that is, "0 ". The prototype of the calloc () function is void.
* Calloc (count, sizeof (type name); for example, p = (char *) calloc (4, sizeof (char )); we allocated a space of "4" to the char pointer for P.


In addition to malloc () and calloc (), there is also a realloc () function that dynamically allocates space. This function allocates more space than the first two functions, prototype: void * realloc (void * P, size_t size); this function has several functions: 1) if there is enough space to expand the memory block of P, a pointer to P is returned; 2) If there is not enough space to expand P's memory block, it allocates a New Size Space for P and copies the original content to P, pointing to the beginning of P, release the original space and return a pointer to the new memory block. 3)
If P = NULL, it acts on similar malloc (); The following is an example of a program:

#define LONG 10#include<string.h>#include<stdlib.h>#include<stdio.h>main(){   char *message,buf[LONG];   gets(buf);   message=(char*)realloc(NULL,strlen(buf)+1);   strcpy(message,buf);   puts(message);   gets(buf);   message=(char*)realloc(message,(strlen(message)+strlen(buf)+1));   strcat(message,buf);   puts(message);   return 0;}
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.