C Instantiate memory allocation issues

Updated on technology 2024-05-21
5 answers
  1. Anonymous users2024-02-11

    First of all, heaps and stacks are two different things. Heap is mainly used for dynamic memory allocation, while stack is mainly used for space allocation of automatic variables within functions.

    In c, any variable of reference type is allocated on the heap. (Except in special cases such as mandatory declaration stackalloc).

    The various classes are reference types, so they are all allocated on the heap.

    In your example, there are two variables, one is c1 and one is c2, and they each have a member a, and they don't affect each other.

    Both are distributed on the heap and are in different positions. So the value of a in c1 is 12 and the value of a in c2 is 6, and there is no effect between the two.

  2. Anonymous users2024-02-10

    The landlord is a novice (anyway, I'm also a novice.,I've never entered the door),The concept of stacks is different.,How can you assign stacks.。

    I'll talk about it, if it's not right, let's discuss it again:

    First, A instantiates for the first time (i.e. C1), then the system allocates a piece of memory for it to store data(Actually, you can mark the address with a pointer here, int* c11=&.)

    Then A instantiates a second time (i.e., C2), and the system allocates a piece of memory to it to store the data;(Actually, you can still mark the address with a pointer here, int* c22=&.)

    Then you call, that is, you call the data with the memory address C11, i.e. the data is stored in the memory of the C11 address, you call the data, and then output to the screen;

    Then you assign a value, i.e. you change the data of the memory address c22 from 12 to 6;

    Finally, you call the output.

    a variable, when instantiated twice, each is allocated a piece of memory space (note: if you don't instantiate, then the system won't allocate memory for you).

    If there is any mistake or omission, I hope to correct it.

  3. Anonymous users2024-02-09

    The result of the print is:

    How can it be 12 and 6?Ask for advice!o( ohaha Let's get down to business, I won't tease you!

    Every time you instantiate an object with new a(), you call the constructor of class A to open up a new address in the heap (note: it opens up every time you use it). And there is an int variable in this address, and the value of this int variable is initialized to 12, and when you modify the value of this int variable, a new address will not be opened!

  4. Anonymous users2024-02-08

    The two main differences between static and dynamic memory allocation are:

    1. Static objects are variables with names, and we directly manipulate them. Dynamic objects, on the other hand, are variables that don't have names, and we manipulate them indirectly with pointers.

    2. The allocation and release of static objects are automatically handled by the compiler. Programmers need to understand this, but don't need to do anything. On the other hand, the allocation and release of dynamic objects, which must be managed explicitly by the programmer, is relatively error-prone, and is done through two expressions, new and delete.

    The dynamic dispatch of objects can be done with one of two versions of the new expression. The first version is used to assign a single object of a specific type.

    For example: int *pint = new int(1024);

    The expression returns the address of the object in memory. This address is then used to initialize the pointing object pint.

    The second version of the new expression, which is used to allocate an array of a specific type and number of bits.

    For example: int *pia=new int[4];

    An array of four integer elements is assigned. Unfortunately, there is no way to explicitly specify an initial value for each element of a dynamically allocated array.

    When we run out of dynamically allocated objects or arrays of objects, we must explicitly free up that memory. We can do this by using one of two versions of a delete expression, for example, by deleting a single object.

    delete pint;

    The array form of the delete expression is:

    delete pia;

    If you forget to delete the dynamically allocated memory, the program will end up with a memory leak.

  5. Anonymous users2024-02-07

    As opposed to static, dynamic is the allocation that is used in the execution of the program, and static is assigned at the beginning.

Related questions
7 answers2024-05-21

Just write it like this.

public class a >>>More

3 answers2024-05-21

Way. 1: Install protection software such as Kingsoft Guardian to effectively clear the system garbage. >>>More

13 answers2024-05-21

In fact, this is a simple question of variable type and variable function, I think it should be explained like this: first, the original purpose of variable definition is to use the variable we define to meet the needs of our program, so, above you define variable a, then later, you will use this variable to do some operations, in most cases, it is to be initialized, so, give a corresponding type of value to initialize the variable, so that some operations can be used in the future, second, the understanding of that sentence:" You want a to represent 5, so you want to assign the value of a to 5", in fact, it is an initialization process of the variable a, where a is int (integer), corresponding to an initial value of 5, which is expressed like this: >>>More

10 answers2024-05-21

You can use the smart pointer to release the pointer automatically.

6 answers2024-05-21

I don't know if you know anything about memory allocation. >>>More