Pointer and Memory Allocation

Source: Internet
Author: User

1. the pointer is flexible, which makes it difficult to manage the pointer. when defining the pointer, a memory address will be opened in the stack to store the pointer (the memory in the stack is allocated and released by the system ), the pointer address memory only stores the pointer address and does not store the data pointed to by the pointer. It is worth noting that when defining the pointer, the pointer will randomly point to a memory, such as int * P; P points to a non-empty memory, which is quite risky. For example, if (! P) {printf ("P is blank") ;}; "P is not output here" P is empty "; so we want to leave the pointer P blank during definition, int * P = NULL is required. If the program is large, no error occurs in the execution result.

 

2. I have limited ability to assign values to pointers. I only know two types. One is to allocate a block of memory (dynamically allocated memory) on the stack so that the Pointer Points to the block of memory. the second method is to point the pointer to a piece of memory on the stack (usually to define a non-pointer variable to point the pointer to this variable). The two methods are implemented using the following code:

# Include <stdio. h> <br/> # include <malloc. h> <br/> int main () <br/>{< br/> // first <br/> int * P1 = (int *) malloc (sizeof (INT); // dynamically allocate a block of memory on the heap (manually allocated) <br/> scanf ("% d", P1 ); <br/> printf ("% d/N", * P1); <br/> free (P1 ); // release the memory </P> <p> // type 2 <br/> int I; // The system automatically allocates a block of memory on the stack. <br/> int * P2 = NULL; <br/> scanf ("% d", & I ); <br/> P2 = & I; // point P2 to the memory of I on the stack <br/> printf ("% d/N", * P2 ); <br/> return 0; <br/>}< br/>

 

3. Do you know why I didn't add free (P2) after the second method )? If the free (P2) compiler is added, a memory error is reported because P2 points to the I memory, and the I memory is

On the stack, the memory on the stack is managed by the system, such as allocation and release, without manual management. Here, free (P2) is used to release the memory of I, you do not need to worry about this memory, but you will certainly report an error when using free () management,

 

4. the memory is dynamically allocated. In C, malloc () is used for allocation and free () is used for release. In C ++, both new and delete are used for release, malloc () contains the memory size, which isSize_t (unsigned INT)Type. The free () bracket contains the memory address or pointer to the memory. The new is the c ++ overload function, and the new + size,

Delete + address. For specific usage, see the code:

# Include <stdio. h> <br/> # include <malloc. h> <br/> # include <iostream> <br/> using namespace STD; <br/> int main () <br/> {<br/> // type 1 <br/> int * P1 = new int; // dynamically allocate a block of memory on the heap (manually allocated) <br/> scanf ("% d", P1); <br/> printf ("% d/N", * P1); <br/> Delete P1; // release memory </P> <p> // second usage <br/> int * P2 = new int [3]; // allocate three int-type Memory Spaces <br/> int I = 0; <br/> while (I <3) <br/>{< br/> scanf ("% d", & p2 [I]); <br/> I ++; <br/>}< br/> Delete [] P2; // pay attention to this release method </P> <p> // third usage <br/> int * P3 = new int (45); // allocate a memory, and store 45 in this memory <br/> printf ("% d/N", * P3); <br/> Delete P3; </P> <p> return 0; <br/>}< br/>

 

In the second way, I input 56, 78, and 23 in sequence, which is the P2 storage method.

 

 

Note: if there are any errors, please point them out. Thank you!

 

 

 

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.