Visual Studio Express 中 C++ 中的内存分配问题

Posted

技术标签:

【中文标题】Visual Studio Express 中 C++ 中的内存分配问题【英文标题】:Memory allocation issue in C++ in visual studio express 【发布时间】:2014-06-06 15:02:25 【问题描述】:

我正在创建一个虚拟图像并将图像的值作为一维数组返回。但是由于图像的尺寸很大。我想自己分配内存并释放它。从一个帖子开始。我得到了内存分配和解除分配的代码。我做了一些改变以适应我的要求。但是输出意外终止。任何建议都会有很大帮助!

Memory_Allocator 类

class Allocate_Memory

public:

    /*Allocate and de-allocate memory for 3D arrays*/
    double*** Allocate3D(int Color, int Height, int Width);
    void Deallocate3D(double*** arr3D, int Height, int Width);

    /*Allocate and de-allocate memory for 1D array*/
    double* Allocate1D(int Total_Size);
    void Deallocate1D(double* arr1D);
;

实施

/*Allocate memory for 3D array*/
double*** Allocate_Memory::Allocate3D(int Color, int Height, int Width)

    double ***arr3D;
    int i, j;

    arr3D = new double**[Color];

    for (i = 0; i<Height; ++i)
    
        arr3D[i] = new double*[Height];
        for (j = 0; j<Width; ++j)
        
            arr3D[i][j] = new double[Width];
        
    

    return arr3D;


/*De-allocate memory for 3D array*/
void Allocate_Memory::Deallocate3D(double*** arr3D, int Color, int Height)

    int i, j;

    for (i = 0; i<Color; ++i)
    
        for (j = 0; j<Height; ++j)
        
            delete arr3D[i][j];
        
        delete [] arr3D[j];
    
    delete [] arr3D;


/*Allocate memory for 1D array*/
double* Allocate_Memory::Allocate1D(int Total_Size)

    double *arr1D;
    arr1D = new double[Total_Size];
    return arr1D;


void Allocate_Memory::Deallocate1D(double *arr1D)

    delete [] arr1D;

用法

 void Create_Dummy_Marvin_Image::Create_Marvin()


    Marvin_Image_1 = Allocate_Memory.Allocate3D(2, 5, 5);
    Marvin_Single_Image_1 = Allocate_Memory.Allocate1D(50);
    int x = 0; //Random value initialisation
    for (int c = 0; c < Color; ++c)                  //Iterating through color values
    
        for (int h = 0; h < Height; ++h)             //Iterating through height of the image
        
            for (int w = 0; w < Width; ++w)          //Iterating through width of the image
            
                ++x;                                 //Incrementing the value of x
                Marvin_Image_1[c][h][w] = (double)x;   //Setting the value in the arrays to form marvin image with random values
                Marvin_Single_Image_1[c*Height*Width + h*Width + w] = Marvin_Image_1[c][h][w]; //Setting the data to a single dimensional array of marvin image
                //cout << "\n Marvin image :" << int(Marvin_Single_Image_1[c*Height*Width + h*Width + w]); //Displaying the data in each array element
                cout << "\n Marvin image :" << int(Marvin_Single_Image_1[c*Height*Width + h*Width + w]); //Displaying the data in each array element
            
        
    
    Allocate_Memory.Deallocate3D(Marvin_Image_1, Height, Width);
    Allocate_Memory.Deallocate1D(Marvin_Single_Image_1);

调试

在尝试调试时,我在代码中的任何一点都没有问题,但是执行会因

而停止
'Image_Conversion.exe' (Win32): Loaded 'C:\Users\padmanab\Documents\VPadmanabhan\Marvin_To_UnsignedChar\Release\Image_Conversion.exe'. Symbols loaded.
'Image_Conversion.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded.
'Image_Conversion.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'Image_Conversion.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded.
'Image_Conversion.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120.dll'. Symbols loaded.
'Image_Conversion.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120.dll'. Symbols loaded.
The program '[4972] Image_Conversion.exe' has exited with code 0 (0x0).

【问题讨论】:

为什么在 C++ 中使用malloc?此外,您应该使用智能指针(至少)或使用现有的库容器。 @crashmstr 您好,感谢您的回复。如果您能指导我使用智能指针的类似示例,那就太好了。 【参考方案1】:

我不明白您使用 3D 阵列的推理。你为什么要让你的生活变得不必要地困难? 如果你得到一个带有 with 和 height 的图像,只需分配内存:

const unsigned size = width*height*colorChannels;
    char* imageBuffer = new char[size];
//iterate over it
for( unsigned i = 0; i < size; ++i )
  imageBuffer[i] = 0;

如果你真的想使用 3D 数组:

int main()

    unsigned x = 2, y = 2, z = 2;
    int colorVal = 1;
    char*** arr = new char**[x];
    for( unsigned w = 0; w < x; ++w )
    
        arr[w] = new char*[y];
        for( unsigned h = 0; h < y; ++h )
        
            arr[w][h] = new char[z];
            for( unsigned s = 0; s < z; ++s )
            
                arr[w][h][s] = colorVal++;
            
        
    

std::cout << "value: " << (int)arr[0][0][0] << std::endl;
std::cout << "value: " << (int)arr[0][0][1] << std::endl;

std::cout << "value: " << (int)arr[0][1][0] << std::endl;
std::cout << "value: " << (int)arr[0][1][1] << std::endl;

std::cout << "value: " << (int)arr[1][0][0] << std::endl;
std::cout << "value: " << (int)arr[1][0][1] << std::endl;

std::cout << "value: " << (int)arr[1][1][0] << std::endl;
std::cout << "value: " << (int)arr[1][1][1] << std::endl;

//deletion
for( unsigned w = 0; w < x; ++w )

    for( unsigned h = 0; h < y; ++h )
       delete arr[w][h];
    delete [] arr[w];

    delete [] arr;

【讨论】:

嘿。非常感谢您的友好回复。这正是我在我编辑的问题中所做的和更新的。然而执行意外停止 我无法在问题中评论您的代码,但您在 for 循环中的删除运算符(所以我猜)不正确!正确的顺序是:删除、删除[]、删除[]。存储在数组中的项目必须在没有“[]”的情况下删除 我根据你的建议改了。我得到了同样的错误,就像我之前没有改变一样。

以上是关于Visual Studio Express 中 C++ 中的内存分配问题的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio Express 2013 至 2017

使用 MinGW C 编译器代替 Microsoft Visual Studio Express for Python

C++ Express 版缺少哪些 Visual Studio 2008 生产力功能?

C++ Visual Studio Express 2010 无法启动程序错误

Visual Studio Express 2013 中面向初学者的 C++ 设置

Visual Studio 2010 Express 限制