标签: memory-management

如何使用智能指针包装数组?

shared_ptr复制到数组:是否应该使用它?


根据这篇文章,使用smart_ptr包装数组的好方法是定义一个删除函数,并将删除函数单独传递给带有原始数组的smart_ptr.


我将重构我的代码,比如使用smart_ptr包装原始数组.这是一个例子:

原始代码:

    class MyList{
    public:
       MyList(int size = 0);
       void Resize(int size);
       ~MyList();
    private:
       int* myArray; 
       int* head_;
       size_t size_;
    }

    MyList::MyList(int size){
        myArray = new int[size]; //allocated memory when object is being created 
        head_ = list_;
        size_ = size;
    }

    void MyList::Resize(int size) {
        if (!list_) {
             delete myArray;
             size_ = 0;
        }
        myArray = new int[size];
        head_ = list_;
        size_ = size;
    }

    MyList::~MyList(){
       delete myArray;
       head …
Run Code Online (Sandbox Code Playgroud)

c++ pointers memory-management smart-pointers dynamic-memory-allocation

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

设置数组元素时出现MSVC访问冲突

我一直在努力寻找对以下代码中出现的错误的解释:

#include <stdlib.h>

int main() {
    int m=65536;
    int n=65536;
    float *a;

    a = (float *)malloc(m*n*sizeof(float));

    for (int i = 0; i < m; i++){
       for (int j = 0; j < n; j++){
            a[i*n + j] =  0;  
        }    
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么执行此程序时出现"访问冲突"错误?

内存分配是成功的,问题是在一些迭代计数的嵌套for循环中.我尝试使用较小的m&n值并且程序正常运行.

这是否意味着我内存不足?

c c++ memory-management

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

如何使用智能指针防止双重对象删除?

我有一个课程,它拥有它的孩子的所有权:

class Child
{
public:
    Child() {}
    ~Child() {}
};

class Parent : public QObject
{
    Q_OBJECT
public:
    explicit Parent(QObject *parent = 0): QObject(parent) {}
    ~Parent()
    {
        qDeleteAll(m_children);
    }
    void addChild(Child *ch)
    {
        m_children.append(ch);
    }    
private:
    QList<Child *> m_children;    
};
Run Code Online (Sandbox Code Playgroud)

Child删除后,Parent使用addChild方法添加到类中的实例将Parent被删除.

以下使用将导致双子女破坏:

int main()
{
    {
        Parent father;
        Child child;
        father.addChild( &child );
    }
    //child is out of scope now and gets destroyed
    //father gets destroyed too
    //father's destructor deletes child AGAIN!
    //crash! …
Run Code Online (Sandbox Code Playgroud)

c++ qt pointers memory-management smart-pointers

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

转换成Mat后我是否需要释放IplImage?

我在代码中的几个地方执行以下操作;

cv::Mat cv_proc_image = ...
IplImage ipl_img = cv_proc_image;
cvSmooth(&ipl_img, &ipl_img, smooth_type, smooth_param1);
cv_proc_image = cv::cvarrToMat(&ipl_img);
Run Code Online (Sandbox Code Playgroud)

在观察到此操作的变化后,我观察到内存膨胀.你认为,我应该在最后一次分配后释放IplImages分配的内存吗?

c++ opencv memory-leaks memory-management

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

realloc bug - 递增数组的最后一个元素

我试图实现一个dinamically增加数组realloc.我创建数组malloc,然后调用我的add函数,这将数组大小增加1.这是代码:

#include <stdio.h>
#include <stdlib.h>

int *foo;
int quantity;

void add(int number) {
    foo = (int*) realloc(foo, sizeof(foo) + sizeof(int));
    foo[quantity] = number;
    quantity++;
}

void debugFoo() {
    for (int i = 0; i < quantity; i++) {
        printf("foo[%i] = %i\n", i, foo[i]);
    }
    printf("\n");
}

int main() {
    quantity = 3;
    foo = (int*) malloc(quantity * sizeof(int));

    foo[0] = 1;
    foo[1] = 2;
    foo[2] = 3;

    debugFoo();

    add(20);
    debugFoo();
    add(2);
    debugFoo();

    return 0; …
Run Code Online (Sandbox Code Playgroud)

c memory-management realloc

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

在C中调整2D数组的大小

目前我正在尝试使用此代码片段在C中调整2D数组的大小

array = (int**) realloc(array, s * 2 * sizeof(int));
Run Code Online (Sandbox Code Playgroud)

其中s是行和列中数组的大小.但是,当试图像这样访问数组的新区域时,

array[3][0] = x;
Run Code Online (Sandbox Code Playgroud)

我只得到一个段错误.阵列的旧区域工作正常.我该如何解决这个问题?

c arrays memory-management multidimensional-array

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

operator new []在MSVC调试时非常慢

在我的应用程序启动中,3-ary指针结构的内存分配很少.

#define N_1 1024
#define N_2 32
#define N_3 1024

#include <boost/date_time/posix_time/posix_time.hpp>

typedef uint16_t counts;
typedef counts * spectra;
typedef spectra * line;
typedef line * pline;
typedef pline * cube;

void foo()
{
    cube cb = new pline[N_3];

    for (int n3 = 0; n3 < N_3; n3++)
    {
        boost::posix_time::ptime tic = boost::posix_time::microsec_clock::local_time();

        line ln = new spectra[N_2];

        for (int n2 = 0; n2 < N_2; n2++)            
        {
            ln[n2] = new counts[N_1]();
        }

        cb[n3] = &(ln);

        boost::posix_time::ptime toc = boost::posix_time::microsec_clock::local_time(); …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management new-operator

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

C++ 4d数组内存释放很慢

我的代码中有一个4D矩阵用于解决数学问题

int**** Sads = new int***[inputImage->HeightLines];
for (size_t i = 0; i < inputImage->HeightLines; i++)
{
    Sads[i] = new int**[inputImage->WidthColumns];
    for (size_t j = 0; j < inputImage->WidthColumns; j++)
    {
        Sads[i][j] = new int*[W_SIZE];
        for (size_t k = 0; k < W_SIZE; k++)
         {
              Sads[i][j][k] = new int[W_SIZE];
         }
    }
 }

//do something with Sads...

for (int i = 0; i < inputImage->HeightLines; i++)
        {
            int*** tempI = Sads[i];
            for (int j = 0; j < inputImage->WidthColumns; j++)
            {
                int** …
Run Code Online (Sandbox Code Playgroud)

c++ optimization memory-management

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

C++ OpenGL内存泄漏

首先,我是c ++的初学者.我正在编写自己的游戏引擎.代码如下.

Sprite.cpp

#include "Sprite.h"


Sprite::Sprite(GLfloat x, GLfloat y, GLfloat width, GLfloat height, Shader& shader, char *texturePath)
{
    init(x, y, width, height, shader, texturePath, STBI_rgb_alpha);
}

Sprite::Sprite(GLfloat x, GLfloat y, GLfloat width, GLfloat height, Shader& shader, char *texturePath, int reg_comp)
{
    init(x, y, width, height, shader, texturePath, reg_comp);
}
void Sprite::init(GLfloat x, GLfloat y, GLfloat width, GLfloat height, Shader& shader, char *texturePath, int reg_comp)
{
    this->shader = &shader;
    GLfloat vertices[] = {
        width/2+x,  height/2+y, 0.0f,  /* Top Right */                  1.0f, …
Run Code Online (Sandbox Code Playgroud)

c++ memory opengl memory-leaks memory-management

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

如何分配具有可变大小元素的向量(在C++中)?

我编写了以下代码来接受竞争性编程网站上的测试用例.它使用input结构的向量case来同时存储给定测试用例的输入,然后一次处理一个(我省略了接受输入的循环并计算输出,因为它们与问题无关.)

#include<iostream>
#include<vector>
using namespace std;

struct case{
    int n, m;
    vector<int> jobsDone;
};

int main(){
    int testCase;
    cin>>testCase;
    vector<case> input;
    input.reserve(testCase); 

    //The rest of the code is supposed to be here
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编写这段代码时,我意识到input.reserve(t)在元素大小可变的情况下工作(因为结构的每个实例case也有一个可变大小的向量)将是困难的.事实上,即使我没有明确写出reserve()声明,矢量仍然会保留最小数量的elemtns.

对于这种特殊情况,我对矢量有以下问题input:

  • 在这种情况下,O(1)时间内的随机访问是不可能的,因为每个元素的起始位置都是未知的?
  • input当无法计算每个元素的起始位置时,向量如何管理元素访问?它会将所有条目填入最大条目的大小吗?
  • 我是否应该cases使用指向每个实例的指针向量来实现case?我正在考虑这个问题,因为如果向量将每个元素填充到一个大小并浪费空间,或者它保持每个元素的位置,并且随机访问在时间上不是恒定的,因此无论如何都不会使用向量.

c++ memory-management stl vector

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