C Language: Allocation and management of memory

Source: Internet
Author: User
Tags function prototype

1, the partition standard of memory area:

Code Snippet Storage CodeData segment static/Global data, constants (const) Heap Area (heap) dynamic memory allocation (more flexible setting container size to store data) Stack (stack) local variables, function parameters
2, the principle of memory allocation: Static allocation and dynamic allocation         Static allocation: The compiler allocates the program source code (variables)         Dynamic allocation: The program executes according to the dynamic requirements to allocate (determined by programmers, used up for release)           comparison: Static program pre-execution, high efficiency, poor flexibility, object is the name of the variable, memory allocation and release by the compiler, the dynamic object does not have the name of the variable, It is manipulated indirectly by pointers.  3, understanding the concept of wild pointers:        Concepts: The wild pointer is not a null (NULL) pointer, and the second is a pointer to a memory that does not belong to itself.       Three forms:    (1) Pointer variables are not initialized.     (2) after the pointer p is free or delete, it is not null, but p still points to the memory that is not already part of itself.     (3) Returns the address of a local variable through a function. Local variables are stored in the stack stack, once the function is executed, the data in the stack is freed, and the returned pointer is not pointing, that is, the piece of memory in the function does not belong to the returned pointer.  4, memory leak concept:        memory is allocated after the system is logged, but run out, finally not released, the record is not deleted at this time memory is occupied, that is, memory leaks. Similar to the library, good to borrow good also.  5, understanding the meaning of the malloc function prototype:      #include <stdlib.h>        malloc function prototypes:   void * malloc (unsigned int size);         generic type pointer, when really used, you need to cast to the type you want to use   & nbsp     Description: (1), size the meaning of this parameter is the amount of memory allocated (in bytes)  (2), return value: Failed, the return value is null (null pointer). Succeeds, the return is a         Remember: The return value is judged first? If it is empty, allocating memory fails. Conversely, the application is successful.        (1) Request a block with malloc save only one integral type of memory                 int *p = (int *) malloc (sizeof (int)       request a piece with malloc to hold 25 integer types of memory       (2) int *p = (int *) malloc ( sizeof (int) *25)      access to dynamic space       access to single memory       *P = 100;    &nbs P Access to dynamic arrays         p[1];* (p+1);(p + 1)        Arrays store data:       * (p+i )//In the process of storing data, p points to a memory header address has not changed, only the data in this piece of memory stored in the location. Tested        *p++;//cyclic storage data, p points to the memory of the first address has been changing, in fact, is a piece of memory just stored a data, p moved to another piece of space to the first address, and then save the next data. The function that tests the &NBSP;6, frees the memory the meaning of the free:     function prototype: free (void *block)//release must be malloc return address     Description: The application and release of dynamic memory must be paired  7, note: (1) If the function parameter is a pointer, do not expect the pointer to apply for dynamic memory. It is said that the pointer must point to a valid area of memory. (2) The pointer dies, P=null does not mean that the memory it points to is automatically released, the memory is freed, and free (p) does not mean that the pointer dies or is a null pointer. Both must be combined. (3) After malloc allocates memory, the allocated memory space must be freed by using free () before the program ends. MallOC and free must be a one-to-many relationship and cannot be over-released as a pair.   &NBSP;8, knowledge development:  (1) Assigning dynamic memory           function prototypes using Calloc functions: void *calloc (unsigned n,unsigned Size);          The function is to allocate n contiguous spaces of size in the dynamic storage of memory, which is generally large enough to hold an array.           meaning: Use the Calloc function to open up dynamic storage space for one-dimensional arrays, n is the number of elements, and the length of each element is size. This is the dynamic array. The function returns a pointer to the starting position of the assigned domain, or null if the assignment is unsuccessful.           For example:p = calloc (50,4);//Create a temporary assignment field of 50*4 bytes, assign the start address to the pointer variable p(2) using the ReAlloc function to allocate a dynamic memory function prototype: void* realloc (void *p,unsigned int size);function: If you have obtained the dynamic space through the malloc function or the Calloc function, you want to change its size, you can use the ReAlloc function to redistribute. meaning: Use the ReAlloc function to change the size of the dynamic space pointed to by P to size. The point of P is unchanged, and NULL is returned if the assignment is unsuccessful.

C Language: Allocation and management of memory

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.