The following code creates an object on the stack: Object o; When creating an object on the heap we can use: Object* o; o = new Object(); rather than: Object* o = new Object(); When we split the heap object-creation over two lines and call the constructor on the second line ( o = new object() ), does this mean in the first line ( Object* o ) the pointer was created on the stack? So Object
以下代码在堆栈上创建一个对象: Object o; 在堆上创建对象时,我们可以使用: Object* o; o = new Object(); 而不是: Object* o = new Object(); 当我们将堆对象创建分成两行并在第二行调用构造函数( o = new object() )时,这是否意味着在第一行( Object* o )指针是在堆栈上创建的? 所以Object o把对象放在堆栈上,而Object* o把指针放在堆栈上的未来对象上? 我的第二个问题涉及两行代码是否在类之外被调用。
In C++11 (N3485) 10.1.4 [class.mi] it says: For each distinct occurence of a non-virtual base class in the class lattice of the most derived class, the most derived object shall contain a corresponding distinct base class subobject of that type. For each distinct base class that is specified virtual, the most derived class shall contain a single base class object of that type. Consider the
在C ++ 11(N3485)10.1.4 [class.mi]中说: 对于大多数派生类的类网格中的非虚拟基类的每个不同的发生,最多派生的对象将包含该类型的相应的不同基类子对象。 对于每个指定为虚拟的不同基类,最派生类应包含该类型的单个基类对象。 考虑下面的C ++ 11代码: struct B {}; struct BV : virtual B {}; struct BN : B {}; struct C1 : BV, BN {}; struct C2 : BV, BN {}; struct D : C1, C2 {}; 首先,为了清楚起见,D
I've come to love lambdas, and a while ago I wrote a simple wrapper which takes a lambda and starts it on a new thread. // // Starts a task on a separate thread, when passed a lambda expression // template<typename T> smart_ptrs::w32handle StartTask(T f) { // Make a copy of the task on the heap T* pTask = new T(f); // Create a new thread to service the task smart_ptrs:
我爱上了lambda,而前一段时间,我写了一个简单的包装,它需要一个lambda并在新线程上启动它。 // // Starts a task on a separate thread, when passed a lambda expression // template<typename T> smart_ptrs::w32handle StartTask(T f) { // Make a copy of the task on the heap T* pTask = new T(f); // Create a new thread to service the task smart_ptrs::w32handle hThread(::CreateThread(
Recently I've bumped into a realization/implementation of the Singleton design pattern for C++. It has looked like this (I have adopted it from the real life example): // a lot of methods are omitted here class Singleton { public: static Singleton* getInstance( ); ~Singleton( ); private: Singleton( ); static Singleton* instance; }; From this declaration I
最近我遇到了C ++的Singleton设计模式的实现/实现。 它看起来像这样(我从现实生活的例子中采纳了它): // a lot of methods are omitted here class Singleton { public: static Singleton* getInstance( ); ~Singleton( ); private: Singleton( ); static Singleton* instance; }; 从这个声明中,我可以推断实例字段是在堆上启动的。 这意味着有一个内存分配。 对于我来说完全不清楚
In C/C++ we can store variables, functions, member functions, instances of a class either on a stack or a heap. How is each implemented? How is it managed (high level)? Does gcc preallocates a chunk of memory to be used for the stack and heap, and then doles out on request? Is original memory coming from RAM? Can a function be allocated on the heap instead of a stack? Clarification I a
在C / C ++中,我们可以将变量,函数,成员函数,类的实例存储在堆栈或堆中。 各自如何实施? 它如何管理(高级别)? gcc是否预先分配了一堆用于栈和堆的内存,然后根据请求进行分配? 原始内存来自RAM吗? 函数可以分配在堆而不是堆栈上吗? 澄清 我真的在询问关于堆栈和堆栈内存的实现和管理。 在阅读引用的问题后,我没有发现任何解决该问题的内容......感谢链接 现代操作系统不能直接访问硬件RAM,而是将其抽
so in C++ you can instantiate objects using the new keyword or otherwise... Object o = new Object(); but you can also just do Object o = Object(); what exactly is the difference b/w the two and why would I use one over the other? You can't do Object o = new Object(); The new operator returns a pointer to the type. It would have to be Object* o = new Object(); The Object instance will
所以在C ++中,您可以使用new关键字实例化对象,否则... Object o = new Object(); 但你也可以做 Object o = Object(); 两者之间的差异究竟是什么,为什么我会用另一种呢? 你不能做Object o = new Object(); new运算符返回一个指向该类型的指针。 它必须是Object* o = new Object(); Object实例将在堆上 。 Object o = Object()将在堆栈上创建一个Object实例。 我的C ++是生锈的,但我相信即使这个天真的看起来像一
In C++ (I use QT) I can create an instance of QString class two ways: method 1 QString str = "my string"; method 2 QString *str = new QString("my string"); I know this is to do with pointers. So my questions are: what is the difference between the two? which method should I stick to? when is it correct to use method 1 and when is it correct to use method 2? in method 2 I can destro
在C ++中(我使用QT)我可以通过两种方式创建一个QString类的实例: 方法1 QString str = "my string"; 方法2 QString *str = new QString("my string"); 我知道这是用指针做的。 所以我的问题是: 两者有什么区别? 我应该坚持哪种方法? 什么时候使用方法1正确?何时使用方法2正确? 在方法2中,我可以通过调用delete str;来销毁对象delete str; 。 如何在使用方法1时删除str变量? 谢谢 主要是它们有不
I understand there are three, not two areas of memory in C++: stack, heap AND the area for static-assigned features. I have two questions Why is the heap so much slower than the stack? Surely it should just be one extra level of indirection? Is the area of memory allocated for static "features" (variables, functions, classes) provide faster performance than the heap? A couple of
我知道在C ++中有三个,而不是两个内存区域:堆栈,堆和静态指定功能的区域。 我有两个问题 为什么堆比堆栈慢得多? 当然,它应该只是一个额外的间接水平? 分配给静态“特性”(变量,函数,类)的内存区域是否比堆提供更快的性能? 首先有几个旁注。 正确的术语是自动而不是堆栈,动态而不是堆。 另一个是用C ++ 11,现在有四种而不是三种类型的内存。 C ++ 11为混合添加了线程本地内存。 自动存储是快速的,因为它
This question already has an answer here: Why does the use of 'new' cause memory leaks? 9 answers These are not two separate things. There's not a new and a *new . The second is simply applying unary * to the result of new Cat() . new Cat() is a new-expression. It dynamically allocates and initialises a Cat object and evaluates to a pointer to that object. If you then apply
这个问题在这里已经有了答案: 为什么使用'new'导致内存泄漏? 9个答案 这些不是两个单独的事情。 没有new和*new 。 第二个是简单地应用一元*到new Cat()的结果。 new Cat()是一种新的表达方式。 它动态分配并初始化一个Cat对象,并计算一个指向该对象的指针。 如果您然后将一元*应用于该指针,则将其解引用以获取Cat对象。 将一元*立即应用于新表达式并不是一个很好的理由。 原因是因为你立即跟随指针,
I'm relatively new to some of the more advanced aspects of C++ programming and I'm having some trouble understanding if it is ever truly necessary to allocate memory in C++ (whether it be through malloc, new, etc.). For example in C, I understand you need to allocate memory to have a dynamically sized array or other tasks. In C++, it seems to me that's not the case, you can just use
我对C ++编程的某些更高级的方面比较陌生,因此我很难理解是否真的有必要在C ++中分配内存(无论是通过malloc,new等)。 例如在C中,我知道你需要分配内存来获得动态调整大小的数组或其他任务。 在C ++中,在我看来情况并非如此,您可以使用std :: vector,std :: string或其他内置方法,这些方法已经通过设计动态调整大小。 我也明白,访问分配的内存比堆栈慢。 那么考虑到,是否有时候你必须用C ++分配内存,如果是这样