Memory Allocation for memory

Source: Internet
Author: User

I am very interested in memory allocation. When I think of memory allocation, I can bring out some experiences for your reference.

First, let's start with the statement.

Int Myint, * p_myint;

// Specify two variations, one of which is an integer variable Myint, and the other is an integer variable pointing to an integer variable p_myint, which is not initialized.

Int Myint = 100, * p_myint;

// Specify two variations, an integer variation Myint, and an integer variant pointing to an integer variation. p_myint Myint is initialized to 100, it contains 2 characters, because the int type changes by 2 characters (this is the case for most data collectors );

The variable p_myint is not initialized.

Int Myint = 100; int * p_myint = & Myint;

// Specify two variations, an integer variation Myint, and an integer variant pointing to an integer variation. p_myint Myint is initialized to 100, 2 characters long;

P_myint is initialized to the address of variable Myint. It contains four characters, because the variable volume always contains four characters (in 32-bit hosts );

Therefore * p_myint = 100.

Well, now there is such a problem that needs to be solved:

A metric pointing to an integer of 200 is required.

There is a "good way ":

Int * p_myint = & 200;

Can this be solved?

This generation seems subtle, but it has a small problem-it is awesome! This kind of self-made idea is not feasible.

This line indicates, on the surface, that a Domain Name Pointing to the int type refers to the parameter p_mypoint and initializes it to point to the integer 200.

But you didn't tell the hacker where the whole number of data is stored. How is it? For example, if you specify a change and set the value to 200, then point p_mypoint to the address of the change, just like

Int Myint = 200;

Int * p_myint = & Myint;

In this way, it is completely possible, because the connector will explicitly allocate a piece of memory to the Myint variable and put "200" on the memory ", in this case, the memory value of the "operator" of a "operator" is 200, and its "Generation operator" is Myint, it is certainly not a problem to obtain the Myint address.

But now someone says, "We still need to find a medium variation so that p_myint points to it. Now, I only need to set a metric. This metric refers to an integer of 200 !". Why?

......

Let's name a finger first...

Int * p_myint;

Then ...... Then I want it to point to a certain place. Isn't it enough to put 200 in this place?

Good, then...

P_myint = (int *) malloc (sizeof (INT ));

* P_myint = 200;

Wow, so far?

Don't be afraid. Let's take a look. The first line is to allocate an empty space for the number of words required by the int type to the place specified by the p_myint parameter, and then let it (p_myint) point to "that location" (the memory space of the newly allocated worker ).

Malloc is the function of memory allocation, and its memory is an integer. For example, 3 is the allocation of three characters. The returned void type refers to a volume, because we want an integer to indicate the volume, so we need to add (int *) to convert it into an integer to indicate the volume.

Someone said, it should be * p_myint = (int *), malloc (sizeof (INT, because it is to allocate a blank space to the place to which the cursor refers, instead of assigning a blank space to the finger, it means that the cursor already exists and there are four character strings. I thought so at first, but this is not true. You think, * is the value operation operator, but now p_myint does not know where to point (because it has not been initialized), you will ask it to take the value of that place, can it be obtained?

This sentence is a continuous value sentence. The calculated value is (int *) malloc (sizeof (INT). It is an integer) forward to p_myint.

Next sentence

* P_myint = 200;

That is, we will allocate 200 bytes to the new memory space in that region. This problem will be solved.

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.