在 C 中将 malloc 的 2d 数组修改为 3d 数组的函数 [英] Modifying function that malloc's a 2d array to a 3d array in C
问题描述
我对 C 很陌生,这是我用它编写的第一个程序.我的教授给了我们一个为二维数组分配内存的函数,称为 malloc2d.我应该修改它来为 3d 数组分配内存,但我对 C 太陌生了,我不知道如何去做.我试过查看 3d 数组的其他 malloc 函数,但没有一个看起来与我得到的相似.同样,我们有一个 free2d 函数,它也需要针对 3d 数组进行修改.下面是需要修改的函数:
void** malloc2D(size_t rows, size_t cols, size_t sizeOfType){void* block = malloc(sizeOfType * rows * cols);void** matrix = malloc(sizeof(void*) * rows);for (int row = 0; row < rows; ++row) {矩阵[行] = 块 + cols * 行 * sizeOfType;}//为了返回矩阵;}//malloc2Dvoid free2D(void*** 矩阵){自由((*矩阵)[0]);自由((*矩阵));矩阵 = NULL;}//free2D
任何帮助或开始将不胜感激.
我发现很难相信这是第一次练习;至少它有点棘手.
修复二维码
第一步应该是清理 malloc2D()
函数,这样它就不会随便使用 GCC 扩展——索引一个 void *
——因为标准 C 会这样做不允许这样做(因为 sizeof(void)
在标准 C 中未定义;GNU C 将其定义为 1).另外,free2D()
中的bug 需要修复;函数的最后一行应该是 *matrix = NULL;
(省略了 *
).该代码也应该进行测试,因为访问矩阵的正确方法并不明显.
这里有一些修改后的代码(为了与 3D 版本保持一致而重命名的变量),用于测试修改后的 2D 代码:
/* SO 4885-6272 */#include #include #include /* 应该在头文件中声明以便在其他文件中使用 */extern void **malloc2D(size_t rows, size_t cols, size_t sizeOfType);extern void free2D(void ***matrix);void **malloc2D(size_t 行,size_t cols,size_t sizeOfType){void *level2 = malloc(sizeOfType * rows * cols);void **level1 = malloc(sizeof(void *) * rows);if (level2 == NULL || level1 == NULL){免费(级别2);免费(级别1);返回空;}for (size_t row = 0; row < rows; ++row){level1[row] = (char *)level2 + cols * row * sizeOfType;}返回级别1;}void free2D(void ***矩阵){自由((*矩阵)[0]);自由((*矩阵));*矩阵= NULL;}静态无效 test2D(size_t m2_rows, size_t m2_cols){printf("rows = %zu; cols = %zu
", m2_rows, m2_cols);void **m2 = malloc2D(m2_rows, m2_cols, sizeof(double));如果(m2 == NULL){fprintf(stderr, "大小为 %zux%zu doubles 的二维数组的内存分配失败
",m2_rows, m2_cols);返回;}printf("m2 = 0x%.12" PRIXPTR "; m2[0] = 0x%.12" PRIXPTR "
",(uintptr_t)m2, (uintptr_t)m2[0]);for (size_t i = 0; i
在运行 macOS High Sierra 10.13.3 的 MacBook Pro 上运行时,使用 GCC 7.3.0 进行编译,我得到输出:
rows = 4;列数 = 5m2 = 0x7F83C04027F0;m2[0] = 0x7F83C040275011 12 13 14 1521 22 23 24 2531 32 33 34 3541 42 43 44 45m2 = 0x0000000000000000行 = 10;列数 = 3m2 = 0x7F83C0402750;m2[0] = 0x7F83C04028C011 12 1321 22 2331 32 3341 42 4351 52 5361 62 6371 72 7381 82 8391 92 93101 102 103m2 = 0x0000000000000000行 = 3;列数 = 10m2 = 0x7F83C04027A0;m2[0] = 0x7F83C04028C011 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 40m2 = 0x0000000000000000
包括怪物分配,跟踪结束:
alloc3d19(8985,0x7fffa5d79340) malloc: *** mach_vm_map(size=2400000000000000000) 失败(错误代码=3)*** 错误:无法分配区域*** 在 malloc_error_break 中设置断点进行调试大小为 300000000x1000000000 的二维数组的内存分配失败
适应 3D 代码
我选择将 3D 数组的前导维度称为平面";每个平面都包含一个带有 r
行和 c
列的二维数组.
对我来说,在我搞砸了几次之后,我给自己画了一张图表来说服自己我的作业是正确的.在前两张表的每个单元格中,第一个数字是包含数组中单元格的索引号(第一个表中的level1
),第二个是下一个单元格的索引号级别(第一个表中的 level2
).level3
表中的数字只是 double
a 数组的索引.
level1(平面:4)╔=======╗║ 0: 00 ║║ 1: 05 ║║ 2: 10 ║║ 3: 15 ║╚=======╝level2(平面:4;行:5)╔========╦=======╦=======╦=======╦=======╗║ 00: 00 ║ 01: 06 ║ 02: 12 ║ 03: 18 ║ 04: 24 ║║ 05:30 ║ 06:36 ║ 07:42 ║ 08:48 ║ 09:54 ║║ … ║ … ║ … ║ … ║ … ║╚=======╩=======╩=======╩=======╩=======╝level3(平面:4;行:5;列:6)╔====╦=====╦=====╦=====╦=====╦=====╗║ 0 ║ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║║ 6 ║ 7 ║ 8 ║ 9 ║ 10 ║ 11 ║║ 12 ║ 13 ║ 14 ║ 15 ║ 16 ║ 17 ║ 平面 0║ 18 ║ 19 ║ 20 ║ 21 ║ 22 ║ 23 ║║ 24 ║ 25 ║ 26 ║ 27 ║ 28 ║ 29 ║╠====╬=====╬=====╬=====╬=====╬=====╣║ 30 ║ 31 ║ 32 ║ 33 ║ 34 ║ 35 ║║ 36 ║ 37 ║ 38 ║ 39 ║ 40 ║ 41 ║ 平面 1║ … ║ … ║ … ║ … ║ … ║ … ║╚====╩=====╩=====╩=====╩=====╩=====╝
有了那个图——或者是纸和笔的版本,上面写着箭头,level1
中平面p
的单元格中的值是p* 行
;p
平面单元格中level2
行r
的值是p * rows + r) * cols
;平面p
,行r
,c
在level3
的单元格中的值为(p* 行 + r) * cols + c
.但这些值不是整数;他们是指针.因此,这些值必须按适当的大小进行缩放,并添加到 level1
、level2
或 level3
空间的基地址.>
这导致了这样的代码:
#include #include #include /* 应该在头文件中声明以便在其他文件中使用 */extern void ***malloc3D(size_t 平面,size_t 行,size_t cols,size_t sizeOfType);extern void free3D(void ****matrix);void ***malloc3D(size_t 平面,size_t 行,size_t cols,size_t sizeOfType){void *level3 = malloc(sizeOfType * planes * rows * cols);void **level2 = malloc(sizeof(void *) * 平面 * 行);void ***level1 = malloc(sizeof(void **) * 平面);//printf("planes = %zu; rows = %zu; cols = %zu; ", planes, rows, cols);//printf("level1 = 0x%.12" PRIXPTR "; level2 = 0x%.12" PRIXPTR "; level3 = 0x%.12" PRIXPTR "
",//(uintptr_t)level1, (uintptr_t)level2, (uintptr_t)level3);fflush(标准输出);if (level3 == NULL || level2 == NULL || level1 == NULL){免费(级别3);免费(级别2);免费(级别1);返回空;}for (size_t plane = 0; plane < planes; plane++){level1[plane] = (void **)((char *)level2 + plane * rows * sizeof(void **));//printf("level1[%zu] = 0x%.12" PRIXPTR "
", plane, (uintptr_t)level1[plane]);for (size_t row = 0; row < rows; ++row){level2[plane * rows + row] = (char *)level3 + (plane * rows + row) * cols * sizeOfType;//printf(" level2[%zu] = 0x%.12" PRIXPTR "
",//平面*行+行,(uintptr_t)level2[平面*行+行]);}}返回级别1;}void free3D(void ****matrix){自由((*矩阵)[0][0]);自由((*矩阵)[0]);自由((*矩阵));*矩阵= NULL;}静态无效 test3D(size_t m3_plns, size_t m3_rows, size_t m3_cols){printf("飞机 = %zu;行 = %zu; cols = %zu
", m3_plns, m3_rows, m3_cols);void ***m3 = malloc3D(m3_plns, m3_rows, m3_cols, sizeof(double));如果(m3 == NULL){fprintf(stderr, "大小为 %zux%zux%zu 的 3D 数组的内存分配失败
",m3_plns, m3_rows, m3_cols);返回;}printf("m3 = 0x%.12" PRIXPTR "; m3[0] = 0x%.12" PRIXPTR "; m3[0][0] = 0x%.12" PRIXPTR "
",(uintptr_t)m3, (uintptr_t)m3[0], (uintptr_t)m3[0][0]);for (size_t i = 0; i
示例输出(具有超大内存分配):
planes = 4;行 = 5;列数 = 6m3 = 0x7FFCC94027F0;m3[0] = 0x7FFCC9402750;m3[0][0] = 0x7FFCC9402850平面 1:111 112 113 114 115 116121 122 123 124 125 126131 132 133 134 135 136141 142 143 144 145 146151 152 153 154 155 156平面 2:211 212 213 214 215 216221 222 223 224 225 226231 232 233 234 235 236241 242 243 244 245 246251 252 253 254 255 256平面 3:311 312 313 314 315 316321 322 323 324 325 326331 332 333 334 335 336341 342 343 344 345 346351 352 353 354 355 356飞机 4:411 412 413 414 415 416421 422 423 424 425 426431 432 433 434 435 436441 442 443 444 445 446451 452 453 454 455 456m3 = 0x0000000000000000飞机 = 3;行 = 4;列数 = 10m3 = 0x7FFCC94027F0;m3[0] = 0x7FFCC9402750;m3[0][0] = 0x7FFCC9402840平面 1:111 112 113 114 115 116 117 118 119 120121 122 123 124 125 126 127 128 129 130131 132 133 134 135 136 137 138 139 140141 142 143 144 145 146 147 148 149 150平面 2:211 212 213 214 215 216 217 218 219 220221 222 223 224 225 226 227 228 229 230231 232 233 234 235 236 237 238 239 240241 242 243 244 245 246 247 248 249 250平面 3:311 312 313 314 315 316 317 318 319 320321 322 323 324 325 326 327 328 329 330331 332 333 334 335 336 337 338 339 340341 342 343 344 345 346 347 348 349 350m3 = 0x0000000000000000飞机 = 4;行 = 3;列数 = 7m3 = 0x7FFCC94027F0;m3[0] = 0x7FFCC9402750;m3[0][0] = 0x7FFCC9402840平面 1:111 112 113 114 115 116 117121 122 123 124 125 126 127131 132 133 134 135 136 137平面 2:211 212 213 214 215 216 217221 222 223 224 225 226 227231 232 233 234 235 236 237平面 3:311 312 313 314 315 316 317321 322 323 324 325 326 327331 332 333 334 335 336 337飞机 4:411 412 413 414 415 416 417421 422 423 424 425 426 427431 432 433 434 435 436 437m3 = 0x0000000000000000飞机 = 4;行 = 9;列数 = 7m3 = 0x7FFCC94027F0;m3[0] = 0x7FFCC9402840;m3[0][0] = 0x7FFCC9802000平面 1:111 112 113 114 115 116 117121 122 123 124 125 126 127131 132 133 134 135 136 137141 142 143 144 145 146 147151 152 153 154 155 156 157161 162 163 164 165 166 167171 172 173 174 175 176 177181 182 183 184 185 186 187191 192 193 194 195 196 197平面 2:211 212 213 214 215 216 217221 222 223 224 225 226 227231 232 233 234 235 236 237241 242 243 244 245 246 247251 252 253 254 255 256 257261 262 263 264 265 266 267271 272 273 274 275 276 277281 282 283 284 285 286 287291 292 293 294 295 296 297平面 3:311 312 313 314 315 316 317321 322 323 324 325 326 327331 332 333 334 335 336 337341 342 343 344 345 346 347351 352 353 354 355 356 357361 362 363 364 365 366 367371 372 373 374 375 376 377381 382 383 384 385 386 387391 392 393 394 395 396 397飞机 4:411 412 413 414 415 416 417421 422 423 424 425 426 427431 432 433 434 435 436 437441 442 443 444 445 446 447451 452 453 454 455 456 457461 462 463 464 465 466 467471 472 473 474 475 476 477481 482 483 484 485 486 487491 492 493 494 495 496 497m3 = 0x0000000000000000
planes = 30000;行数 = 100000;列数 = 100000000alloc3d79(9018,0x7fffa5d79340) malloc: *** mach_vm_map(size=2400000000000000000) 失败(错误代码=3)*** 错误:无法分配区域*** 在 malloc_error_break 中设置断点进行调试大小为 30000x100000x100000000 的 3D 数组的内存分配失败
I'm very new to C, this is the first program I'm writing in it. My professor gave us a function for allocating memory for a 2d array, called malloc2d. I am supposed to modify it to allocate memory for a 3d array, but being so new to C I am not sure how to go about it. I've tried looking at other malloc functions for 3d arrays but none of them look similar to the one I was given. Similarly, we have a free2d function that also needs to be modified for a 3d array. Here are the functions to be modified:
void** malloc2D(size_t rows, size_t cols, size_t sizeOfType){
void* block = malloc(sizeOfType * rows * cols);
void** matrix = malloc(sizeof(void*) * rows);
for (int row = 0; row < rows; ++row) {
matrix[row] = block + cols * row * sizeOfType;
}//for
return matrix;
}//malloc2D
void free2D(void*** matrix){
free((*matrix)[0]);
free((*matrix));
matrix = NULL;
}//free2D
Any help or a start would be greatly appreciated.
I find it difficult to believe this is a first exercise; it is moderately tricky, at least.
Fix the 2D code
The first step should be to clean up the malloc2D()
function so it doesn't casually use a GCC extension — indexing a void *
— because Standard C does not allow that (because sizeof(void)
is undefined in Standard C; GNU C defines it as 1). Also, the bug in free2D()
needs to be fixed; the last line of the function should read *matrix = NULL;
(the *
was omitted). That code should be tested too, because the correct way to access the matrix is not obvious.
Here's some modified code (variables renamed for consistency with the 3D version) that tests the revised 2D code:
/* SO 4885-6272 */
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
/* Should be declared in a header for use in other files */
extern void **malloc2D(size_t rows, size_t cols, size_t sizeOfType);
extern void free2D(void ***matrix);
void **malloc2D(size_t rows, size_t cols, size_t sizeOfType)
{
void *level2 = malloc(sizeOfType * rows * cols);
void **level1 = malloc(sizeof(void *) * rows);
if (level2 == NULL || level1 == NULL)
{
free(level2);
free(level1);
return NULL;
}
for (size_t row = 0; row < rows; ++row)
{
level1[row] = (char *)level2 + cols * row * sizeOfType;
}
return level1;
}
void free2D(void ***matrix)
{
free((*matrix)[0]);
free((*matrix));
*matrix = NULL;
}
static void test2D(size_t m2_rows, size_t m2_cols)
{
printf("rows = %zu; cols = %zu
", m2_rows, m2_cols);
void **m2 = malloc2D(m2_rows, m2_cols, sizeof(double));
if (m2 == NULL)
{
fprintf(stderr, "Memory allocation failed for 2D array of size %zux%zu doubles
",
m2_rows, m2_cols);
return;
}
printf("m2 = 0x%.12" PRIXPTR "; m2[0] = 0x%.12" PRIXPTR "
",
(uintptr_t)m2, (uintptr_t)m2[0]);
for (size_t i = 0; i < m2_rows; i++)
{
for (size_t j = 0; j < m2_cols; j++)
((double *)m2[i])[j] = (i + 1) * 10 + (j + 1);
}
for (size_t i = 0; i < m2_rows; i++)
{
for (size_t j = 0; j < m2_cols; j++)
printf("%4.0f", ((double *)m2[i])[j]);
putchar('
');
}
free2D(&m2);
printf("m2 = 0x%.16" PRIXPTR "
", (uintptr_t)m2);
}
int main(void)
{
test2D(4, 5);
test2D(10, 3);
test2D(3, 10);
//test2D(300000000, 1000000000); /* 2132 PiB - should fail to allocate on sane systems! */
return 0;
}
When run on a MacBook Pro running macOS High Sierra 10.13.3, compiling with GCC 7.3.0, I get the output:
rows = 4; cols = 5
m2 = 0x7F83C04027F0; m2[0] = 0x7F83C0402750
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
m2 = 0x0000000000000000
rows = 10; cols = 3
m2 = 0x7F83C0402750; m2[0] = 0x7F83C04028C0
11 12 13
21 22 23
31 32 33
41 42 43
51 52 53
61 62 63
71 72 73
81 82 83
91 92 93
101 102 103
m2 = 0x0000000000000000
rows = 3; cols = 10
m2 = 0x7F83C04027A0; m2[0] = 0x7F83C04028C0
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
m2 = 0x0000000000000000
With the monster allocation included, the trace ended:
alloc3d19(8985,0x7fffa5d79340) malloc: *** mach_vm_map(size=2400000000000000000) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Memory allocation failed for 2D array of size 300000000x1000000000 doubles
Adapt to 3D code
I chose to call the leading dimension of the 3D array a 'plane'; each plane contains a 2D array with r
rows by c
columns.
For me, I drew myself a diagram to convince myself I was getting the assignments correct — after I'd messed up a couple of times. In each cell in the first two tables, the first number is the index number of the cell in the containing array (level1
in the first table) and the second is the index number of the cell in the next level (level2
in the first table). The numbers in the level3
table are simply the indexes into the array of double
a.
level1 (planes: 4)
╔═══════╗
║ 0: 00 ║
║ 1: 05 ║
║ 2: 10 ║
║ 3: 15 ║
╚═══════╝
level2 (planes: 4; rows: 5)
╔════════╦════════╦════════╦════════╦════════╗
║ 00: 00 ║ 01: 06 ║ 02: 12 ║ 03: 18 ║ 04: 24 ║
║ 05: 30 ║ 06: 36 ║ 07: 42 ║ 08: 48 ║ 09: 54 ║
║ … ║ … ║ … ║ … ║ … ║
╚════════╩════════╩════════╩════════╩════════╝
level3 (planes: 4; rows: 5; cols: 6)
╔════╦═════╦═════╦═════╦═════╦═════╗
║ 0 ║ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║
║ 6 ║ 7 ║ 8 ║ 9 ║ 10 ║ 11 ║
║ 12 ║ 13 ║ 14 ║ 15 ║ 16 ║ 17 ║ Plane 0
║ 18 ║ 19 ║ 20 ║ 21 ║ 22 ║ 23 ║
║ 24 ║ 25 ║ 26 ║ 27 ║ 28 ║ 29 ║
╠════╬═════╬═════╬═════╬═════╬═════╣
║ 30 ║ 31 ║ 32 ║ 33 ║ 34 ║ 35 ║
║ 36 ║ 37 ║ 38 ║ 39 ║ 40 ║ 41 ║ Plane 1
║ … ║ … ║ … ║ … ║ … ║ … ║
╚════╩═════╩═════╩═════╩═════╩═════╝
With that diagram in place — or a paper and pen version with arrows scrawled over it, the values in the cell of plane p
in level1
is p * rows
; the values of in the cell of plane p
, row r
in level2
is p * rows + r) * cols
; the values in the cell of plane p
, row r
, cell c
in level3
is (p * rows + r) * cols + c
. But the values are not integers; they're pointers. Consequently, the values have to be scaled by an appropriate size and added to the base address for the level1
, level2
or level3
space.
That leads to code like this:
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
/* Should be declared in a header for use in other files */
extern void ***malloc3D(size_t planes, size_t rows, size_t cols, size_t sizeOfType);
extern void free3D(void ****matrix);
void ***malloc3D(size_t planes, size_t rows, size_t cols, size_t sizeOfType)
{
void *level3 = malloc(sizeOfType * planes * rows * cols);
void **level2 = malloc(sizeof(void *) * planes * rows);
void ***level1 = malloc(sizeof(void **) * planes);
//printf("planes = %zu; rows = %zu; cols = %zu; ", planes, rows, cols);
//printf("level1 = 0x%.12" PRIXPTR "; level2 = 0x%.12" PRIXPTR "; level3 = 0x%.12" PRIXPTR "
",
// (uintptr_t)level1, (uintptr_t)level2, (uintptr_t)level3);
fflush(stdout);
if (level3 == NULL || level2 == NULL || level1 == NULL)
{
free(level3);
free(level2);
free(level1);
return NULL;
}
for (size_t plane = 0; plane < planes; plane++)
{
level1[plane] = (void **)((char *)level2 + plane * rows * sizeof(void **));
//printf("level1[%zu] = 0x%.12" PRIXPTR "
", plane, (uintptr_t)level1[plane]);
for (size_t row = 0; row < rows; ++row)
{
level2[plane * rows + row] = (char *)level3 + (plane * rows + row) * cols * sizeOfType;
//printf(" level2[%zu] = 0x%.12" PRIXPTR "
",
// plane * rows + row, (uintptr_t)level2[plane * rows + row]);
}
}
return level1;
}
void free3D(void ****matrix)
{
free((*matrix)[0][0]);
free((*matrix)[0]);
free((*matrix));
*matrix = NULL;
}
static void test3D(size_t m3_plns, size_t m3_rows, size_t m3_cols)
{
printf("planes = %zu; rows = %zu; cols = %zu
", m3_plns, m3_rows, m3_cols);
void ***m3 = malloc3D(m3_plns, m3_rows, m3_cols, sizeof(double));
if (m3 == NULL)
{
fprintf(stderr, "Memory allocation failed for 3D array of size %zux%zux%zu doubles
",
m3_plns, m3_rows, m3_cols);
return;
}
printf("m3 = 0x%.12" PRIXPTR "; m3[0] = 0x%.12" PRIXPTR "; m3[0][0] = 0x%.12" PRIXPTR "
",
(uintptr_t)m3, (uintptr_t)m3[0], (uintptr_t)m3[0][0]);
for (size_t i = 0; i < m3_plns; i++)
{
for (size_t j = 0; j < m3_rows; j++)
{
for (size_t k = 0; k < m3_cols; k++)
((double *)m3[i][j])[k] = (i + 1) * 100 + (j + 1) * 10 + (k + 1);
}
}
for (size_t i = 0; i < m3_plns; i++)
{
printf("Plane %zu:
", i + 1);
for (size_t j = 0; j < m3_rows; j++)
{
for (size_t k = 0; k < m3_cols; k++)
printf("%4.0f", ((double *)m3[i][j])[k]);
putchar('
');
}
putchar('
');
}
free3D(&m3);
printf("m3 = 0x%.16" PRIXPTR "
", (uintptr_t)m3);
}
int main(void)
{
test3D(4, 5, 6);
test3D(3, 4, 10);
test3D(4, 3, 7);
test3D(4, 9, 7);
test3D(30000, 100000, 100000000); /* 2132 PiB - should fail to allocate on sane systems! */
return 0;
}
Example output (with outsize memory allocation):
planes = 4; rows = 5; cols = 6
m3 = 0x7FFCC94027F0; m3[0] = 0x7FFCC9402750; m3[0][0] = 0x7FFCC9402850
Plane 1:
111 112 113 114 115 116
121 122 123 124 125 126
131 132 133 134 135 136
141 142 143 144 145 146
151 152 153 154 155 156
Plane 2:
211 212 213 214 215 216
221 222 223 224 225 226
231 232 233 234 235 236
241 242 243 244 245 246
251 252 253 254 255 256
Plane 3:
311 312 313 314 315 316
321 322 323 324 325 326
331 332 333 334 335 336
341 342 343 344 345 346
351 352 353 354 355 356
Plane 4:
411 412 413 414 415 416
421 422 423 424 425 426
431 432 433 434 435 436
441 442 443 444 445 446
451 452 453 454 455 456
m3 = 0x0000000000000000
planes = 3; rows = 4; cols = 10
m3 = 0x7FFCC94027F0; m3[0] = 0x7FFCC9402750; m3[0][0] = 0x7FFCC9402840
Plane 1:
111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130
131 132 133 134 135 136 137 138 139 140
141 142 143 144 145 146 147 148 149 150
Plane 2:
211 212 213 214 215 216 217 218 219 220
221 222 223 224 225 226 227 228 229 230
231 232 233 234 235 236 237 238 239 240
241 242 243 244 245 246 247 248 249 250
Plane 3:
311 312 313 314 315 316 317 318 319 320
321 322 323 324 325 326 327 328 329 330
331 332 333 334 335 336 337 338 339 340
341 342 343 344 345 346 347 348 349 350
m3 = 0x0000000000000000
planes = 4; rows = 3; cols = 7
m3 = 0x7FFCC94027F0; m3[0] = 0x7FFCC9402750; m3[0][0] = 0x7FFCC9402840
Plane 1:
111 112 113 114 115 116 117
121 122 123 124 125 126 127
131 132 133 134 135 136 137
Plane 2:
211 212 213 214 215 216 217
221 222 223 224 225 226 227
231 232 233 234 235 236 237
Plane 3:
311 312 313 314 315 316 317
321 322 323 324 325 326 327
331 332 333 334 335 336 337
Plane 4:
411 412 413 414 415 416 417
421 422 423 424 425 426 427
431 432 433 434 435 436 437
m3 = 0x0000000000000000
planes = 4; rows = 9; cols = 7
m3 = 0x7FFCC94027F0; m3[0] = 0x7FFCC9402840; m3[0][0] = 0x7FFCC9802000
Plane 1:
111 112 113 114 115 116 117
121 122 123 124 125 126 127
131 132 133 134 135 136 137
141 142 143 144 145 146 147
151 152 153 154 155 156 157
161 162 163 164 165 166 167
171 172 173 174 175 176 177
181 182 183 184 185 186 187
191 192 193 194 195 196 197
Plane 2:
211 212 213 214 215 216 217
221 222 223 224 225 226 227
231 232 233 234 235 236 237
241 242 243 244 245 246 247
251 252 253 254 255 256 257
261 262 263 264 265 266 267
271 272 273 274 275 276 277
281 282 283 284 285 286 287
291 292 293 294 295 296 297
Plane 3:
311 312 313 314 315 316 317
321 322 323 324 325 326 327
331 332 333 334 335 336 337
341 342 343 344 345 346 347
351 352 353 354 355 356 357
361 362 363 364 365 366 367
371 372 373 374 375 376 377
381 382 383 384 385 386 387
391 392 393 394 395 396 397
Plane 4:
411 412 413 414 415 416 417
421 422 423 424 425 426 427
431 432 433 434 435 436 437
441 442 443 444 445 446 447
451 452 453 454 455 456 457
461 462 463 464 465 466 467
471 472 473 474 475 476 477
481 482 483 484 485 486 487
491 492 493 494 495 496 497
m3 = 0x0000000000000000
planes = 30000; rows = 100000; cols = 100000000
alloc3d79(9018,0x7fffa5d79340) malloc: *** mach_vm_map(size=2400000000000000000) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Memory allocation failed for 3D array of size 30000x100000x100000000 doubles
这篇关于在 C 中将 malloc 的 2d 数组修改为 3d 数组的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!