【问题标题】:Max size to allocate to matrix in c在c中分配给矩阵的最大大小
【发布时间】:2015-03-04 21:49:32
【问题描述】:

我正在做关于矩阵乘法的作业。我想找到我可以处理(分配)的最大矩阵的问题。于是我写了如下代码:

int n = 1;
while(1){
n++;
A=malloc(sizeof(double)*n*n);                                                                                                                           
B=malloc(sizeof(double)*n*n);
C=malloc(sizeof(double)*n*n);
if (C==NULL) {printf("Error No more size to allocate! n=%d\n",n); return 1;} 
// and the rest of the code including freeing the allocate

结果:

Error No more size to allocate! n=21785

现在我想使用另一种方法:使用 A 作为结果而不是 C。 所以我只需要 2(n**2)+n 而不是 3(n**2)。
所以新代码应该是这样的:

int n = 1;
while(1){
n++;
A=malloc(sizeof(double)*n*n);                                                                                                                           
B=malloc(sizeof(double)*n*n);
C=malloc(sizeof(double)*n);
if (C==NULL) {printf("Error No more size to allocate! n=%d\n",n); return 1;} 
// and the rest of the code including freeing the allocated space.

当我运行此代码时它不会停止递增 n 但如果我将条件从 (C==NULL) 更改为 (A==NULL||B==NULL||C==NULL) 结果是:

Error No more size to allocate! n=21263

有什么想法吗??

编辑
Do I cast the result of malloc? PS:我的教授告诉我们总是在 malloc 中使用 cast !

【问题讨论】:

  • 这可能是因为在第二个示例中,您为 A 和 B 分配二维数组,但为 C 分配一维数组。(另外,don't cast the result of calls to malloc().
  • 真的很高兴在每个与 malloc 相关的帖子中看到 stackoverflow.com/questions/605845/…
  • 您刚刚编辑了您的帖子以使C 的分配更大......这将改变运行程序的行为
  • 另外,你怎么知道“它不会停止递增 n”?
  • @MattMcNabb 我并不是说它不会停止递增 n,而是我的意思是 n 与第一个代码的结果相比太大了

标签: c matrix malloc


【解决方案1】:

您的程序在分配 C 失败之前很久就未能分配 A 或 B,这要小得多。当 n 约为 21263 时,n*n 是 n 的 21263 倍。循环将持续大约 10000 次重复。如果在成功分配后释放 C,循环甚至会持续几亿次重复,直到 n 达到大约 21263*21263。您只需等待足够长的时间让您的程序退出。

【讨论】:

  • 我明白这一点,但问题是当尝试运行第二个代码时,检查三个矩阵 a 得到的结果比第一个代码小。在这种情况下我应该获得更好的结果??
  • 可用内存量不同。在多任务系统上,其他进程可能会占用更多或更少的内存,具体取决于您检查的确切时间。
【解决方案2】:
there are a few things to note

1) the main consideration is that memory, once allocated (by malloc and not free'd) means the amount of available heap to allocate goes down, even as 'n' goes up.

2) any call to malloc() can fail, including the first call
   this means the failure could be any of the calls to malloc()

3) in C, the returned value from malloc (and family) should not be cast.

4) the returned value from malloc() should always be checked, 
   not just one in three calls to malloc()

regarding the posted code. 
1) all the above considerations should be implemented
2) in the second example of posted code, just because a 
   larger memory request (n*n) fails  does not mean a 
   smaller request (n) would fail.
   that is why the modification catches the failure of 'A'

3) the heap address and the size of the heap are normally 
   available at compile time, so there is no need
   for the kind of code you posted

【讨论】:

  • 对于第一个注释,我正在释放分配的内存。但我不明白关于堆的最后一个注释。如果堆已满,操作系统不会提供更多内存吗??
【解决方案3】:

您可以尝试进行单次分配,然后将 B 和 C 的指针分配为相对于 A 的偏移量。与其从一个小值开始,不如从一个在第一个循环中会失败的大值开始,这样当分配完成时成功了,堆就不会分片了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-08
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    相关资源
    最近更新 更多