相关疑难解决方法(0)


'new'关键字和类存储

与此问题并行:我何时应该在C++中使用new关键字?

假设我有以下代码结构:

class Foo{
private:
    int a;
    int b;
    /* ect */
};

class Bar{
private:
    Foo A;
    /* ect */
};

int main() {
    Bar *b;
    b = new Bar();

    // call b->methods()

    delete b;
};
Run Code Online (Sandbox Code Playgroud)

我知道从上面的链接b是堆(免费存储)分配.但是A内部类的内容b呢?假设A堆分配是否安全?

c++ heap class

2
推荐指数
1
解决办法
137
查看次数

从函数返回指向对象的指针,而不使用new来分配指针

从线程中

我什么时候应该在C++中使用new关键字?

答案讨论了如果需要从函数返回指向对象的指针,必须使用"new"来创建指向对象的指针.

但是,我的代码工作正常.我使用本地指针而不是为新指针分配一些内存.

node* queue::dequeue(){
  if(head==0){
    cout<<"error: the queue is empty, can't dequeue.\n";
    return 0;
  }
  else if(head->next !=0){
    node *tmp=head;
    head=head->next;
    tmp->next=0;
    return tmp;
  }
  else if(head->next ==0){
    node *tmp=head;
    head=0;
    tmp->next=0;
    return tmp;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的dequeue()操作.我的tmp是一个本地指针.但我还是回来了.

感谢Mahesh

我在main()中有以下语句

node a8(8); //node constructor with the value
Run Code Online (Sandbox Code Playgroud)

因此tmp指向head指向的内容,并指向不同的节点,如a8.

由于a8在main()中有效,因此tmp在main()中也是有效的

c++ new-operator

2
推荐指数
1
解决办法
1万
查看次数

对象修改是否创建新对象?

我对C++中的对象生命周期有点困惑.假设我有以下代码.首先,我创建指针MyObject(第1行).然后我创建一个对象并将指针指向它(第2行).然后我修改对象,并将指针指向结果对象(第3行).最后,我删除了对象,以便避免内存泄漏(第4行).

MyClass * MyObject;
MyObject= new MyClass(x, y); 
*MyObject= MyObject-> ModifyObject(z);
delete MyObject;
Run Code Online (Sandbox Code Playgroud)

第2行的原始对象是否仅在第3行进行了修改?(这意味着上面的代码是安全的).或者是第3行创建的第二个对象,这意味着第2行的第一个对象永远不会被删除,从而造成内存泄漏?

编辑:这是ModifyObject(z)的样子

MyClass MyClass::ModifyObject(int z) {
    int a = z;
    int b = z;
    return MyClass(a, b);
}
Run Code Online (Sandbox Code Playgroud)

c++ oop object delete-operator

2
推荐指数
1
解决办法
64
查看次数

为什么重载的new运算符正在调用构造函数,即使我在重载函数中使用malloc?

我试图了解重载新运算符.我编写了如下代码.

#include <iostream>
#include <cstdlib>
#include <new>

using namespace std;

class loc
{
    int lo, la;

    public:
        loc()
        {
        }

        loc(int x, int y)
        {
            cout << "In constructor\n";
            lo = x;
            la = y;
        }

        void show()
        {
            cout << lo << " ";
            cout << la << endl;
        }

        void *operator new(size_t sz);
        void operator delete(void *p);
};

void *loc::operator new(size_t sz)
{
    cout << "in Overloaded new\n";
    void *p = malloc(sz);

    return p;
}

void loc::operator delete(void …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
1970
查看次数

从函数返回数组时是否需要删除

在函数内部的堆上创建数组时,是否有必要在主函数中删除该数组?考虑一下,这个程序:

#include "stdafx.h"
#include <iostream>

using namespace std;

int * return_array() {
    int* my_array = new int[10];
    my_array[0] = 10;
    return my_array;
}

int main()
{
    int * returned_array = return_array();
    cout << returned_array[0];
    delete[] returned_array;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这行:int* my_array = new int[10];是否需要在主程序中删除?我发现每次执行此操作都很难维护和正确。

c++ c++17

2
推荐指数
1
解决办法
124
查看次数

在堆栈上创建类实例

我正在尝试使用 C++ 中的内存,我为自己定义了一个类,然后在堆内创建了该类的实例。

#include <iostream>

class mojeTrida {
  public:
  
  void TestPrint()
  {
    std::cout << "Ahoj 2\n";
  }
};

int main() {
  mojeTrida *testInstance = new mojeTrida();
  
  testInstance->TestPrint();
  
  std::cout << "Hello World!\n";
}
Run Code Online (Sandbox Code Playgroud)

如果我正确理解了 c++,每当我调用关键字“new”时,我都会要求操作系统给我一定数量的字节来在堆内存储类的新实例。

有什么办法可以将我的类存储在堆栈中吗?

c++

2
推荐指数
1
解决办法
63
查看次数

为什么在删除对象之前调用复制构造函数?

我有以下类,其中包括一个复制构造函数:

头文件:Fred.h

namespace foo {


class Fred {
private:
    int _x;
    int _y;


public:
    Fred();                         // Default constructor
    Fred(Fred const &other);        // Copy constructor
    Fred(int x, int y);             // Regular parameters
    ~Fred();                        // Destrcutor

};


}
Run Code Online (Sandbox Code Playgroud)

实现文件:Fred.cpp

#include "Fred.h"
#include <iostream>

foo::Fred::Fred(){
    _x = 0;
    _y = 0;

    std::cout << "Calling the default constructor\n";
}

foo::Fred::Fred(Fred const &other){

    _x = other._x;
    _y = other._y;

    std::cout << "Calling the copy constructor";
}

foo::Fred::Fred(int x, int y){
    _x = x;
    _y = …
Run Code Online (Sandbox Code Playgroud)

c++ constructor destructor memory-management copy-constructor

1
推荐指数
1
解决办法
667
查看次数

C++ 新运算符作用域

因此,我正在编写一段代码,其中使用 new 运算符在本地作用域函数内创建一个对象,并将引用作为指针返回。

A* operator+(A that){
    int sumA = a + that.getA();
    int sumB = b + that.getB();
    return new A(sumA, sumB);
}
Run Code Online (Sandbox Code Playgroud)

在我看来,这是行不通的,因为该对象是在本地范围内创建的,并且应该是临时的,但它已编译并运行。有人可以向我解释一下吗?我确信还有其他东西有能力打破范围之类的东西。如果可能的话,你能给我一些例子吗?赞赏!

c++ new-operator

1
推荐指数
1
解决办法
6432
查看次数

C++:何时使用“new”,何时不使用?| 添加:不重复

我是 C++ 新手,想知道什么时候应该使用 new,什么时候不应该使用,例如“int x = 5”或“int * x = new int(5)”。我知道新在堆中保留内存,因此在块结束时不会被删除,但由于保存地址的变量将在块之外变得不可访问,我看不到任何好处。

例子:

if(x) {
 int * z = new int(5);
 // Do something
}
// At this point the 5 is saved somewhere but since z is unaccessible I can't use it.
Run Code Online (Sandbox Code Playgroud)

补充:这个问题没有重复,因为其他问题只解释了什么是堆,而没有描述它的好处。

c++ heap

1
推荐指数
1
解决办法
171
查看次数

C++ 无法理解析构函数调用

该程序是用自定义类设计的,A该类1在调用复制构造函数和0调用析构函数时输出。

我复制了两次,删除了两次,但析构函数被调用了 3 次,导致输出为11000. 为什么会这样呢?

#include <iostream>
using namespace std;

class A {
public:
    float v;
    A() {v = 1.0;}
    A(A &a) {A::v = a.v; cout << "1";}
    ~A() { cout << "0";}
    float set(float v){
        A::v = v;
        return v;
    }
    float get(float v){
        return A::v;
    }
};

int main(){
    A a, *b = new A(a), *c = new A(*b); // output 110 (Not sure why the destructor is called in …
Run Code Online (Sandbox Code Playgroud)

c++ destructor class

-1
推荐指数
1
解决办法
78
查看次数

返回指针的函数是否要求我删除返回的值?

假设我有一个执行某些任务的函数。该函数返回一个指向int的指针。我的问题是:我是否必须取消分配内存,还是可以使用这种通用格式?


int *do_something()
{
    int *local{ new int };
    //do_something_here

    return local;
    delete local;
}

int main()
{
   int *result{ new int };

   result = do_something();
   delete result;
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers function

-2
推荐指数
1
解决办法
73
查看次数

在运行时分配内存

我想知道如何在不使用和函数的情况下在C或C++中在运行时分配内存块.malloccalloc

c c++ memory-management

-8
推荐指数
1
解决办法
1161
查看次数