动态创建连续的5D阵列? [英] Dynamically creating a contiguous 5D array?

查看:102
本文介绍了动态创建连续的5D阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个非常大的5D数组,我需要将其读入连续内存(另一个5D数组).我不能将数组放在堆栈上,因为它太大了,会产生段错误.我所做的是使用malloc动态创建5D数组,但是我发现它不是连续的内存.是否有一个优雅的解决方案,还是无论如何都会变得凌乱?

I am working with a very large 5D array that I need to read into contiguous memory (another 5D array). I cannot place the array on the stack because it is too large and creates seg faults. What I've done is to create a 5D array dynamically with malloc however I've found that it is not contiguous memory. Is there an elegant solution to this or is it going to be messy no matter what?

推荐答案

摘自Jens Gustedt:

From Jens Gustedt: Don't use fake matrices.

分配一个尺寸为A x B x C x D x E的5维矩阵(不需要在编译时知道尺寸),如下所示:

Allocate a 5-dimensional matrix with dimensions A x B x C x D x E (dimensions aren't required to be known at compile time) like so:

float (*matrix5d)[B][C][D][E] = malloc(sizeof(float[A][B][C][D][E]));

通过一次释放释放内存.

Release the memory with a single call to free.

free(matrix5d);

请注意,以上要求可变长度数组的C99或更高版本.

Note that the above requires C99 or higher for variable-length arrays.

这篇关于动态创建连续的5D阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆