C ++动态分配内存 [英] C++ dynamically allocated memory

查看:76
本文介绍了C ++动态分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白动态分配的内存,我希望你们能为我做更清楚的事情。



首先,我们每次分配内存我们只需要一个指向该内存的指针。

  int * dynInt = new int; 

那么,做我上面做的事情和:

  int someInt; 
int * dynInt =& someInt;

据我所知,在这两种情况下,内存都分配给一个int,记忆。



这两者有什么区别。



更多为什么我需要释放内存

  delete dynInt;在第一种情况下,



我的猜测是:


  1. 当为一个对象动态分配内存时,而如果你做类似于第二种情况,对象get被初始化。如果这是唯一的区别,除了动态分配内存更快之外,是否有任何动机。


  2. 我们不需要的原因对第二种情况使用delete是因为对象被初始化的事实创建了某种自动销毁例程。




<

解决方案

不同之处在于

具有自动存储持续时间的对象是您的正常存储持续时间



创建 int someInt ;



您可能已经听说过它们是堆栈对象,虽然我 / li>

  • 具有动态存储持续时间的对象有一些手动生命周期;您必须使用 delete 自行销毁它们,并使用关键字 new 创建它们。



    你可能听说过他们是堆对象,虽然我也反对这一点。




  • 指针的使用实际上并不与其中任何一个严格相关。您可以有一个指向自动存储持续时间的对象的指针(您的第二个示例),您可以有一个指向动态存储持续时间的对象的指针(您的第一个示例)。



    但是很少需要一个指向自动对象的指针,因为:


    1. 默认情况下没有 ;

    2. 对象不会持续很长时间,因此您可以使用此类指针进行操作。

    相比之下,动态对象通常通过指针访问,只是因为语法接近强制它。 new 返回一个指针供您使用,您必须传递一个指向 delete 的指针, )实际上没有其他方法来访问对象。



    因为这个原因,指针的使用是有时与动态存储的使用混淆,但实际上前者与后者不是因果关系。


    I don't quite get the point of dynamically allocated memory and I am hoping you guys can make things clearer for me.

    First of all, every time we allocate memory we simply get a pointer to that memory.

    int * dynInt = new int;
    

    So what is the difference between doing what I did above and:

    int someInt;
    int* dynInt = &someInt;
    

    As I understand, in both cases memory is allocated for an int, and we get a pointer to that memory.

    So what's the difference between the two. When is one method preferred to the other.

    Further more why do I need to free up memory with

    delete dynInt;
    

    in the first case, but not in the second case.

    My guesses are:

    1. When dynamically allocating memory for an object, the object doesn't get initialized while if you do something like in the second case, the object get's initialized. If this is the only difference, is there a any motivation behind this apart from the fact that dynamically allocating memory is faster.

    2. The reason we don't need to use delete for the second case is because the fact that the object was initialized creates some kind of an automatic destruction routine.

    Those are just guesses would love it if someone corrected me and clarified things for me.

    解决方案

    The difference is in storage duration.

    • Objects with automatic storage duration are your "normal" objects that automatically go out of scope at the end of the block in which they're defined.

      Create them like int someInt;

      You may have heard of them as "stack objects", though I object to this terminology.

    • Objects with dynamic storage duration have something of a "manual" lifetime; you have to destroy them yourself with delete, and create them with the keyword new.

      You may have heard of them as "heap objects", though I object to this, too.

    The use of pointers is actually not strictly relevant to either of them. You can have a pointer to an object of automatic storage duration (your second example), and you can have a pointer to an object of dynamic storage duration (your first example).

    But it's rare that you'll want a pointer to an automatic object, because:

    1. you don't have one "by default";
    2. the object isn't going to last very long, so there's not a lot you can do with such a pointer.

    By contrast, dynamic objects are often accessed through pointers, simply because the syntax comes close to enforcing it. new returns a pointer for you to use, you have to pass a pointer to delete, and (aside from using references) there's actually no other way to access the object. It lives "out there" in a cloud of dynamicness that's not sitting in the local scope.

    Because of this, the usage of pointers is sometimes confused with the usage of dynamic storage, but in fact the former is not causally related to the latter.

    这篇关于C ++动态分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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