Ii. dynamic memory allocation and use

Source: Internet
Author: User

Ii. dynamic memory allocation and use
1. malloc (1) malloc allocation function: requires the size of the requested space (in bytes) (2) the returned value is a first address, which is received by a pointer (3) use sizeof to calculate the element size (4) use the malloc function to apply for memory space, which needs to be released after use; otherwise, memory leakage may occur (5) release function free needs to point to the allocated memory pointer (6) basic Format: void * malloc (unsigned int size); (7) Allocate the specified memory size, but do not clear the allocated space 0 (8) free (pointer ); // release heap space, Mark deletion, unclear content (9) Example: ① malloc and one-dimensional array "1" to add square brackets, pointers, two transformations of arrays, regardless of the dimension array, several * can generate 10 integers randomly in "2" and save them to the heap zone int * parr = malloc (sizeof (int) * 10 ); // create 10 integer spaces in the heap area for (int I = 0; I <10; I ++) {parr [I] = arc4random () % (100-10 + 1) + 10; // generates 10 random numbers [10,100]
} For (int I = 0; I <10; I ++) {printf ("% d", parr [I]); // output
} Printf ("\ n"); // line feed free (parr ); // release the Applied Space ② malloc and the two-dimensional array 1 allocate two rows and three columns of the Two-dimensional array 2 int (* pa) [3] = malloc (sizeof (int) * 2*3); int (* pa) [3] = malloc (sizeof (int) * 2*3 ); // create a 2-row, 3-column integer space pa [0] [0] = 1; // assign a value of pa [0] [1] = 13; ③ malloc and string "1" Save the string "Hello, Lanou" to heap area solution 1: char * pst = malloc (sizeof (char) * 12 ); // open up 12 char-type spaces. Note the \ 0 strcpy (pst, "Hello, Lanou") at the end of the string; // copy the string printf ("% s \ n ", pst); // output free (pst); // release memory solution 2: char a [] = "Hello, Lanou "; // copy the string char * ps = malloc (sizeof (a) in the constant area in the stack area; // open the strcpy (ps, a) space of the string size ); // copy the string in the stack area to the free (ps) in the stack area; // release the memory ④ malloc and the string array 1 char (* p) [255] = malloc (5*255); // open up 5 strings, each string contains 255 bytes of space "2" strcpy (p [0], "iPhone "); // copy the string to the first string array ⑤ malloc and the struct typedef struct person {// create a struct char name [20] with two elements; int age;
} Person; solution 1: int main () {Person * pp = malloc (sizeof (Person); // open a space strcpy (pp-> name, ""); // string copy, value: printf ("% s \ n", pp-> name); // output pp-> age = 34; // integer value: printf ("% d \ n", pp-> age); // output free (pp); // release memory} solution 2: person p = {"baye", 20}; // assign the initial value Person * pi = malloc (sizeof (Person) to the struct based on the features that struct values can be directly assigned )); // open up a space * pi = p for the size of the struct; // directly copy all the values of the struct in the stack area to the pi 6 malloc in the stack area and the pointer of the struct array "1" Using arrows, the struct array uses the struct array of three elements in "2". solution 1: Person * pt = malloc (sizeof (Person) * 3 ); // create three strcpy spaces (pt [0]. name, "Lanou"); // (pt + 0)-> name), copy the string pt [0]. age = 10; // assign printf ("% s \ n", pt [0] to an element of the first struct of the struct array. name); // output free (pt); // release memory solution 2: Person pt [3] = {"erdanzi", 23}, {"bee ", 20 },{ "liuwa", 30 }}; Person * pp = malloc (sizeof (Person) * 3); for (int I = 0; I <3; I ++) {pp [I] = pt [I]; // values can be directly assigned between structures.
} For (int I = 0; I <3; I ++) {printf ("% s % d \ n", pp [I]. name, pp [I]. age );
} Free (pp); // release memory 2. other memory allocation functions ① calloc 1 void * calloc (unsigned n, unsigned size ); 2. Use free to release 3 and allocate n size Space 4. Unlike malloc, the memory space applied for by calloc is initialized to 0 and 5 is not recommended, this vulnerability may cause access to the Internet. The C language does not provide cross-border protection. Programmers need to check cross-border protection. ② realloc void * realloc (void *, unsigned newSize) 2. Use free to release 3 and reassign 4 from small to large based on the given address and size. data will not be lost from large to small, example of data loss: // The original allocated space int * p = malloc (100); // The allocated space p = realloc (p, 150); 3. memory Operation Function ① meset void * memset (void * s, int c, size_t n); 2 is not only used in heap zone, it is also used to start with Stack zone 3 s, all bytes whose length is n are assigned a value of c 4, which is usually used to clear struct or array data ② memcpy void * memcpy (void * dest, const void * source, size_t n); 2: copying n Bytes from source to dest 3 is not only used in heap zone, it is also used for Stack area ③ memcmp int memcmp (const void * buf1, const void * buf2, unsigned int count) memory comparison in memcmp 1, comparison results are divided into> 0, <0, = 0 "3" can be used for both heap memory and stack 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.