有关内存分配的疑问

有关内存分配的疑问

问题描述:

大家好,

如果我们声明一个变量(int a;),那么在此声明时是否有任何内存分配给该变量?

而且如果我们声明一个类指针(MyClass * ptr),那么在此声明时间分配给"ptr"的任意数量的静态内存来存储动态创建的内存(ptr = new MyClass())的地址吗? >
提前非常感谢...

Hi All,

If we declare a variable (int a;), is there any memory allocated to the variable at this declaration time?

And also if we declare a class pointer (MyClass *ptr) any amount of static memory allocated to ''ptr'' to store the address of the dynamically created memory (ptr = new MyClass()) on this declaration time?

Thanks a lot in advance...

首先,什么是声明时间"?一些内存是由应用程序的加载程序分配的,另外一些是在运行时分配的.这取决于您的变量是什么.它可以是静态的,并可以由已加载的专用于静态数据的内存部分分配.根据系统体系结构,它可以是段",页面内存的某些页面等.堆栈变量是在堆栈上分配的,但是堆栈本身是在创建线程时分配的,这是在主(启动)线程的情况下以及在运行时创建的线程在运行时为应用程序加载的阶段.

现在,关于分配给堆的类的对象……不仅仅涉及堆:该对象有两个不同的部分:变量指向指针,而指针指向放置对象本身"的堆中的内存.因此,是的,指针引用的内存是在堆中分配的,但是指针本身完全像在整数变量的情况下那样放置在内存中,如上所述.但最重要的是,可以在堆本身中分配一个指针.一个简单的例子:一个指针可以是其他类的成员.以及指向该类对象的指针.好吧,您了解… :-)

—SA
First, what is "declaration time"? Some memory is allocated by the loader of you application, some is additionally allocated during run time. It depends on what is your variable. It can be static and allocated by the loaded is some section of memory dedicated to static data. It can be "segment", some pages of pages memory, etc., depending on system architecture. Stack variable is allocated on stack, but the stack itself is allocated when its thread is created, as a phase of loading of application in case of main (starting) thread and during run time for threads created during run time.

Now, about the object of the class allocated on heap… Not just heap is involved: there are two different parts of this object: the variable points to the pointer, and the pointer points to the memory in heap where "object itself" is placed. So, the memory referenced by the pointer is, yes, allocated in heap, but the pointer itself is placed in memory exactly as in the case of integer variables, as described above. But on the top of it, a pointer can be allocated in heap itself. Simple example: a pointer can be a member of other class. And the pointer to the object of that class&hellop; well, you understand… :-)

—SA


内存有几种类型,但是,作为程序员,您只需要处理两种类型的内存:'' stack "和"".

堆栈是编译器将要处理的内存.在函数中声明为局部参数的指针或int之类的变量将存储在此处.通常会在调用函数时分配该内存(但是编译器可能会偶尔处理该内存),并在函数返回时释放该内存.关于编译器如何处理您不能做很多事情,但是某些编译器设置可能会巧妙地改变行为.

堆是可以以编程方式访问的内存.使用new语句动态分配的任何内存量都将分配到那里,您有责任稍后使用delete(或delete [])释放该内存.

因此,为回答您的问题,每次调用函数声明它们时,您提到的int和指针变量都将分配在堆栈上,并且一旦函数返回,内存将被释放.但是,如果将变量声明为全局变量,则将在启动应用程序时分配内存,并在退出时释放内存(这是不使用全局变量的又一个原因!).

如果在函数内声明静态变量,则它们的行为通常类似于全局变量,但是为它们分配所需的内存可能会推迟到首次调用该函数之前.内存将一直保持使用状态,直到应用程序退出.
There are several types of memory, but, as a programmer, you only have to deal with two types: the ''stack'' and the ''heap''.

The stack is memory that your compiler will take care of. Variables such as pointers or ints declared as local parameters in a function will be stored there. This memory usually will be allocated the moment your function gets called (but the compiler may occasonally handle this otherwise), and will be released the moment your function returns. You cannot do much regarding how the compiler treats this, but some compiler settings may subtly change the behaviour.

The heap is the memory that you can access programatically. Any amount of memory that you allocate dynamically using new statements will be allocated there, and it is your responsibility to release that memory later, using delete (or delete []).

So, to answer your question, both the int and pointer variables you mentioned would be allocated on the stack, every time the function is called that declares them, and the memory will be released once your function returns. If the variables are declared global however, the memory will be allocated upon starting the application, and released upon exiting (this is just one more reason not to use global variables!).

If you declare static variables inside a function, they will mostly behave like global variables, but allocating the memory required for them may be postponed until the function is called for the first time. The memory will still remain in use until the application exits.


您好,

当我们声明一个变量时,将在堆栈内存中分配内存,并根据变量的范围自动将其删除

第二种情况是创建对象时,该时间在堆中分配了内存.我们也可以通过编程方式处理


thanx
Hi

When we declare a variable the memory allocated in the stack memory and its removed automaticaly based on the scope of the variable

And the secon case When the object is created that time memory allocated in the heap. its we can handle programmatically also


thanx