【发布时间】: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 您好,感谢您的回复。如果您能指导我使用智能指针的类似示例,那就太好了。
标签: c++ arrays visual-c++ memory-management