0

I have an assembly segment of the program that does a huge malloc (typically of the order of 8Gb), populates it and does computations on it.

For debugging purposes I want to be able to convert this allocated and pre-filled memory as a 3-D array in C/C++. I specifically do not want to allocate another 8 GB because declaring unsigned char* debug_arr[crystal_size][crystal_size][crystal_size] and doing an element-by-element copy will result in a stack overflow.

I would ideally love to type cast the memory pointer to an 3D array pointer ... Is it possible ?

Objective is to verify the computation results done in Assembly segment.

My C/C++ knowledge is average. I mostly use 64-bit assembly, so request give me the C++ typecasting in some detail, please?

Env : Intel Core i7 2600K @4.4 GHz with 16 GB RAM, 64 bit assembly programming on 64 bit Windows 7, Visual Studio Express 2012

Thanks...

4

4 回答 4

2

如果您想像从 3D 数组中一样访问单个unsigned char条目,您显然需要相关的维度(为了参数而称它们为nXDim, ) nYDimnZDim并且您需要知道在编写过程中假定的维度顺序。

如果我们假设z更改的频率低于yy低于x那么您可以通过如下函数访问您的数组:

unsigned char* GetEntry(int nX, int nY, int nZ)
{
    return &pYourArray[(nZ * nXDim * nYDim) + (nY * nXDim) + nX];
}
于 2013-05-11T14:04:16.913 回答
1

首先检查你的记忆中做了什么orderin。有两种类型 raw major orderin 或 column major

For row major ordering
Address = Base + ((depthindex*col_size+colindex) * row_size + rowindex) * Element_Size
For column major ordering
Address = Base + ((rowindex*col_size+colindex) * depth_size + depthindex) * Element_Size
于 2013-05-11T14:24:21.910 回答
0

如果您在编译时知道尺寸,那么像这样

void * crystal_cube = 0; // set by asm magic;
typedef unsigned char * DEBUG_CUBE[2044][2044][2044];
DEBUG_CUBE debug_cube = (DEBUG_CUBE) crystal_cube;
于 2013-05-11T14:44:10.067 回答
0

这是一个供您扩展的示例:

char array[10000];    // One dimensional array
char * mat[100];      // Matrix for 2D array
for ( int i = 0; i < 100; i++ )
    mat[i] = array + i * 100;

现在,您将矩阵作为 100x100 元素的 2D 数组存储在与数组相同的内存中。

于 2013-05-11T14:00:49.880 回答