动态存储器和“普通”存储器之间的差异记忆 [英] Differences between dynamic memory and "ordinary" memory

查看:84
本文介绍了动态存储器和“普通”存储器之间的差异记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 new 运算符分配的内存与通过简单变量声明分配的内存之间的一些技术差异,例如 int var ? c ++有任何形式的自动内存管理?



特别是,我有几个问题。首先,由于使用动态内存,您必须声明一个指针来存储您使用的实际内存的地址,动态内存是否使用更多内存?我不明白为什么指针是必要的,除非你声明一个数组。



其次,如果我做一个简单的函数,如: / p>


  int myfunc(){int x = 2; int y = 3; return x + y; } 


...并且调用它,一旦它的存在范围已经结束,该函数就被释放?

注意:这个回答是方式 >太长。我会把它下来一段时间。




>



要回答您的问题,我们首先需要定义称为堆栈的两个区域。



堆栈



假设堆栈是一堆盒子。每个框表示函数的执行。开始时,当调用 main 时,有一个盒子坐在地板上。



一个简单的例子



  int main(int argc,char * argv [])
{
int a = 3;
int b = 4;
return a + b;
}

在这种情况下, c $ c> argc (整数), argv (指向char数组的指针), a (整数)和 b (整数)。



h2>

  int main(int argc,char * argv [])
{
int a = 3;
int b = 4;
return do_stuff(a,b);
}

int do_stuff(int a,int b)
{
int c = a + b;
c ++;
return c;
}

现在,你在地板上有一个盒子> , code>和 b 。在该框顶部,您还有另一个框( do_stuff a b



此示例说明了两个有趣的效果。


  1. 您可能知道, a b 通过值传递。这就是为什么 do_stuff 的框中有这些变量的复制


  2. 请注意,您不必免费 delete 或任何这些变量。当您的函数返回时,该函数的框被销毁。




框溢出



  int main(int argc,char * argv [])
{
int a = 3;
int b = 4;
return do_stuff(a,b);
}

int do_stuff(int a,int b)
{
return do_stuff(a,b);
}

这里有一个盒子> main ,如前所述)。然后,你有一个框( do_stuff a b 。然后,你有另一个框( do_stuff 调用自己),再次使用 a b 。然后另一个。很快,你有一个堆栈溢出



堆栈汇总



把栈看成一堆盒子。每个框表示一个函数执行,并且该框包含在该函数中定义的局部变量。



更多技术资料




  • 每个框被正式称为堆栈框架

  • 注意你的变量有随机默认值吗?当旧的堆栈帧被销毁时,它只是停止相关。它不会被清零或任何类似的东西。下一次堆栈框架使用该段内存时,您会在本地变量中看到旧堆栈框架的位。



/ h1>

这是动态内存分配发挥作用的地方。



假设堆是一个无尽的绿色草地。当调用 malloc new 时,会在堆中分配一块内存。您将获得访问此内存块的指针。

  int main(int argc,char * argv [])
{
int * a = new int;
return * a;
}

这里,在堆上分配一个新的整数内存。您将获得一个指向该内存的 a 指针。




  • a 是一个局部变量,因此它在 main 的框中



    • 动态内存分配的原理



      当然,使用动态分配的内存似乎浪费了几个字节的指针。但是,有些事情你不能(很容易)没有动态内存分配。



      返回数组



        int main(int argc,char * argv [])
      {
      int * intarray = create_array
      return intarray [0];
      }

      int * create_array()
      {
      int intarray [5];
      intarray [0] = 0;
      return intarray;
      }

      这里会发生什么?在 create_array 中返回数组。实际上,你返回一个指针,它只指向包含数组的 create_array 框的部分。当 create_array 返回时会发生什么?



      而是使用动态分配的内存。

        int main(int argc,char * argv [])
      {
      int * intarray = create_array
      int return_value = intarray [0];
      delete [] intarray;
      return return_value;
      }

      int * create_array()
      {
      int * intarray = new int [5]
      intarray [0] = 0;
      return intarray;
      }


      $ b $ p

      因为函数返回不会修改堆,所以你的宝贵 intarray 转义不受影响。


      请记住 delete []

      What are some of the technical differences between memory that is allocated with the new operator and memory that is allocated via a simple variable declaration, such as int var? Does c++ have any form of automatic memory management?

      In particular, I have a couple questions. First, since with dynamic memory you have to declare a pointer to store the address of the actual memory you work with, doesn't dynamic memory use more memory? I don't see why the pointer is necessary at all unless you're declaring an array.

      Secondly, if I were to make a simple function such as this:

      int myfunc() { int x = 2; int y = 3; return x+y; }
      

      ...And call it, would the memory allocated by the function be freed as soon as it's scope of existence has ended? What about with dynamic memory?

      解决方案

      Note: This answer is way too long. I'll pare it down sometime. Meanwhile, comment if you can think of useful edits.


      To answer your questions, we first need to define two areas of memory called the stack and the heap.

      The stack

      Imagine the stack as a stack of boxes. Each box represents the execution of a function. At the beginning, when main is called, there is one box sitting on the floor. Any local variables you define are in that box.

      A simple example

      int main(int argc, char * argv[])
      {
          int a = 3;
          int b = 4;
          return a + b;
      }
      

      In this case, you have one box on the floor with the variables argc (an integer), argv (a pointer to a char array), a (an integer), and b (an integer).

      More than one box

      int main(int argc, char * argv[])
      {
          int a = 3;
          int b = 4;
          return do_stuff(a, b);
      }
      
      int do_stuff(int a, int b)
      {
          int c = a + b;
          c++;
          return c;
      }
      

      Now, you have a box on the floor (for main) with argc, argv, a, and b. On top of that box, you have another box (for do_stuff) with a, b, and c.

      This example illustrates two interesting effects.

      1. As you probably know, a and b were passed-by-value. That's why there is a copy of those variables in the box for do_stuff.

      2. Notice that you don't have to free or delete or anything for these variables. When your function returns, the box for that function is destroyed.

      Box overflow

          int main(int argc, char * argv[])
          {
              int a = 3;
              int b = 4;
              return do_stuff(a, b);
          }
      
          int do_stuff(int a, int b)
          {
              return do_stuff(a, b);
          }
      

      Here, you have a box on the floor (for main, as before). Then, you have a box (for do_stuff) with a and b. Then, you have another box (for do_stuff calling itself), again with a and b. And then another. And soon, you have a stack overflow.

      Summary of the stack

      Think of the stack as a stack of boxes. Each box represents a function executing, and that box contains the local variables defined in that function. When the function returns, that box is destroyed.

      More technical stuff

      • Each "box" is officially called a stack frame.
      • Ever notice how your variables have "random" default values? When an old stack frame is "destroyed", it just stops being relevant. It doesn't get zeroed out or anything like that. The next time a stack frame uses that section of memory, you see bits of old stack frame in your local variables.

      The heap

      This is where dynamic memory allocation comes into play.

      Imagine the heap as an endless green meadow of memory. When you call malloc or new, a block of memory is allocated in the heap. You are given a pointer to access this block of memory.

      int main(int argc, char * argv[])
      {
          int * a = new int;
          return *a;
      }
      

      Here, a new integer's worth of memory is allocated on the heap. You get a pointer named a that points to that memory.

      • a is a local variable, and so it is in main's "box"

      Rationale for dynamic memory allocation

      Sure, using dynamically allocated memory seems to waste a few bytes here and there for pointers. However, there are things that you just can't (easily) do without dynamic memory allocation.

      Returning an array

      int main(int argc, char * argv[])
      {
          int * intarray = create_array();
          return intarray[0];
      }
      
      int * create_array()
      {
          int intarray[5];
          intarray[0] = 0;
          return intarray;
      }
      

      What happens here? You "return an array" in create_array. In actuality, you return a pointer, which just points to the part of the create_array "box" that contains the array. What happens when create_array returns? Its box is destroyed, and you can expect your array to become corrupt at any moment.

      Instead, use dynamically allocated memory.

      int main(int argc, char * argv[])
      {
          int * intarray = create_array();
          int return_value = intarray[0];
          delete[] intarray;
          return return_value;
      }
      
      int * create_array()
      {
          int * intarray = new int[5];
          intarray[0] = 0;
          return intarray;
      }
      

      Because function returning does not modify the heap, your precious intarray escapes unscathed. Remember to delete[] it after you're done though.

      这篇关于动态存储器和“普通”存储器之间的差异记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆