Difference between the C language malloc calloc realloc and the working mode & amp; prevents wild pointers, callocrealloc

Source: Internet
Author: User

Difference between C-language malloc calloc realloc and working mode & Prevention of wild pointer, callocrealloc

1. C language and Memory Allocation Method

?? Distribute from static storage area

When the program is compiled, it is allocated. This program exists throughout the entire runtime, such as global variables and static variables.

?? Create on Stack
?? When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.

?? Allocated from the stack, also known as dynamic memory allocation.
???? When running, the programmer uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. the lifetime of the dynamic memory is determined by the user, and the usage is flexible, but the problem is also the most.

2. alloca, calloc, malloc, free, and realloc Functions
??? ? <1> alloca applies for memory from the stack, so it does not need to be released.
? ??? <2> the memory allocated by malloc is located in the heap and has no memory initialization content. Therefore, after malloc, call the memset function to initialize the memory space.
??? <3> calloc initializes the memory of this Part and sets it to 0.
??? <4> realloc adjusts the memory size applied for by malloc.
??? <5> the applied memory must be released using the function free.
Note:

??? ? When the program runs malloc but is not free, memory leakage may occur. some of the memory is not used, but it is not free. Therefore, the system considers that this part of memory is still in use, resulting in a constant request for memory from the system, reducing the available memory of the system. however, memory leakage only means that when the program is running and the program exits, the OS will reclaim all resources. therefore, restarting the program properly may be helpful sometimes.
3 .? The statement of the three functions is:
?????????? Void * malloc (unsigned size );
??????? Void * realloc (void * ptr, unsignednewsize );??
????? ???? Void * calloc (size_t numElements, size_t sizeOfElement );?
??? All are in the stdlib. h function library. Their return values are the addresses allocated by the request system. If the request fails, NULL is returned.
??? (1) function malloc ()
??????? In the dynamic storage area of the memory, a contiguous area with a length of size bytes is allocated. The parameter size is the length of the memory space required, and the first address of the area is returned.
??? (2) function calloc ()
??????? Similar to malloc, The sizeOfElement parameter is the length of the unit element of the request address, and numElements is the number of elements, that is, the continuous address space for applying numElements * sizeOfElement bytes in the memory.
??? (3) function realloc ()
??????? Allocate space to a pointer that has been allocated an address. The ptr parameter is the original space address, and newsize is the length of the requested address.
????Differences:
????(1)The malloc function Cannot initialize the allocated memory space, but the calloc function can. if the memory space allocated by the malloc () function has never been used before, each of them may be 0;

Otherwise, if the memory has been allocated, a variety of data may be left over. that is to say, when the program using the malloc () function starts (the memory space has not been re-allocated), it can work normally, but after a period of time (the memory space has been re-allocated) problems may occur.
????(2)The calloc () function initializes every bit in the allocated memory space to zero. That is to say, if you allocate memory for elements of the character or Integer type, these elements are always initialized to 0; if you allocate memory for pointer elements, these elements are usually initialized to a null pointer; if you allocate memory for real data, these elements will be initialized to zero of the floating point type.
????(3)The malloc function applies to the system to allocate a specified size of memory space. the return type is void. void * Indicates a pointer of unknown type. c, C ++ stipulates that the void * type can be forcibly converted to any other type of pointer.
????(4)Realloc can expand or contract the space indicated by the given pointer. Whether it is expansion or reduction, the content in the original memory will remain unchanged. of course, for downgrading, the contents of the reduced part will be lost. realloc does not guarantee that the adjusted memory space is the same as the original memory space. instead, the pointer returned by realloc is likely to point to a new address.
???? (5)Realloc allocates memory from the stack. when a memory space is expanded, realloc () tries to directly obtain additional bytes from the byte following the existing data on the stack. If yes, it is natural that the world is peaceful; if the number of bytes after the data is not enough, the problem arises. The first free block with sufficient size on the stack will be used, and the existing data will be copied to the new location, the old block is put back on the stack. an important information transmitted in this sentence is that the data may be moved.

4. Concerning the behavior of realloc:

1. If realloc fails, NULL is returned;
2. When realloc fails, the original memory does not change, that is, it is not free or not move (this place is prone to errors );
3. if there is enough memory remaining behind the original memory, the realloc memory = the original memory + the remaining memory, and the realloc still returns the original memory address; if the original memory does not have enough memory, realloc will apply for a new memory and copy the original memory data to the new memory. The original memory will be free, realloc returns the address of the new memory;
4. If the size is 0, the effect is equivalent to free ();
5 .? The pointer passed to realloc can be null, equivalent to malloc;
6. the pointer passed to realloc must be previously allocated through malloc (), calloc (), or realloc.

5. Example of address problems after realloc:

1 .???? If there is enough memory remaining behind the original memory, the realloc memory = the original memory + the remaining memory, and the realloc still returns the original memory address


 

 

2. if the original memory does not have enough memory, realloc will apply for a new memory and copy the original memory data to the new memory. The original memory will be free, realloc returns the address of the new memory

 

4. Wild pointer

1. What is a wild pointer?

Pointer to the memory released or the memory with no access permission.

Instead of a NULL pointer, it seems to point to a valid memory, but the memory has actually been released.

2. Why?

(1) pointer variables are not initialized. When a pointer variable is created, it does not automatically become a NULL pointer. Its default value is random, which means it is random. Therefore, the pointer variable should be initialized at the same time when it is created, either set the pointer to NULL or set it to direct to the legal memory.

(2) After the pointer p is free or deleted, It is not set to NULL.

(3) pointer operations go beyond the scope of variables.

3. How to Prevent wild pointers?

1) Remember to initialize the pointer when declaring it.

2) When the pointer has no use value, remember to release it. After the release is successful, remember to assign a NULL value to the pointer.

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.