为什么我用C多维动态分配不工作? [英] Why is my multi-dimensional dynamic allocation in C not working?

查看:188
本文介绍了为什么我用C多维动态分配不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图与我的分配和使用多维动态分配的数组在C弄清楚这个问题我真的AP preciate任何帮助。

I have been trying to figure out the problem with my allocation and use of a multidimensional dynamically allocated array in C. I'd really appreciate any help.

我试过两种方法。第一:

I've tried two approaches. The first:

cdr = (double ***) malloc(NUM_REGIONS * sizeof(double **));
for(i=0; i<NUM_REGIONS; i++){
   cdr[i] = (double **) malloc(numRatings * sizeof(double *));
   for(j=0; j<numRatings; j++){
       cdr[i][j] = (double *) malloc(remQuarters * sizeof(double));
   }
}  

和第二个:

tempPtr1 = (double *) malloc(NUM_REGIONS * numRatings * remQuarters * sizeof(double) );
tempPtr2 = (double **) malloc (NUM_REGIONS * numRatings * sizeof(double *));
cdr = (double ***) malloc(NUM_REGIONS * sizeof(double **));
for(i=0; i< NUM_REGIONS; i++){
    cdr[i] = tempPtr2 + i;
    for(j=0; j < numRatings; j++) cdr[i][j] = tempPtr1 + i * NUM_REGIONS + j;
}

无论是工作。在这两种情况下,每个CDR [I]最终指向同一个地方。我第一次踏入'我'循环中,所有的CDR [I](即CDR [0],CDR [1],CDR [2]等)进入设置为相同的值。随后的循环再不要改变任何人。

Neither is working. In both cases, each cdr[i] ends up pointing to the same place. The first time I step into the 'i' loop, all cdr[i] (i.e. cdr[0], cdr[1], cdr[2], etc.) get set to the same value. Subsequent loops then don't change any of them.

我怀疑有一些与运营商precedence回事,不然我提领错了,但我一直没能弄明白。

I suspect there's something going on with operator precedence or I'm dereferencing wrong, but I haven't been able to figure it out.

感谢。

更新

我总结了以下简单的code,这似乎很好地工作。不过,虽然输出完全如预期,我仍然得到同样怪异的行为,因为我通过它在调试步骤。我开始思考我的code中的根本问题可能是在其他地方,而我只是被问题与调试器(只是我的输出误解或可能)转移。是否存在已知原因上的手表CDR [0]','CDR [1]等在Visual Studio中不会显示什么,我期待它显示?

I put together the following simplified code, which seems to work fine. But while the output is completely as expected, I'm still getting the same weird behaviour as I step through it in the debugger. I'm starting to think the fundamental problem with my code might be elsewhere, and I've just been diverted by issues with the debugger (or probably just with my misunderstanding of the output). Is there a known reason why a watch on 'cdr[0]', 'cdr[1]', etc. in Visual Studio wouldn't show what I'm expecting it to show?

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"

#define NUM_REGIONS 50


void printArray(double *inVec, int len){
    int i;
    for(i=0; i<len; i++) printf("%f\t",inVec[i]);
    printf("\n");
}

int main(array<System::String ^> ^args){

    int numRatings = 25, remQuarters = 100, i, j, k;
    double ***cdr;
    char dummy;

    cdr = (double ***) malloc(NUM_REGIONS * sizeof(double **)); 
    for(i=0; i<NUM_REGIONS; i++){ 
        cdr[i] = (double **) malloc(numRatings * sizeof(double *)); 
        for(j=0; j<numRatings; j++){ 
            cdr[i][j] = (double *) malloc(remQuarters * sizeof(double)); 
        } 
    }

    for(i=0; i<NUM_REGIONS; i++){
        for(j=0; j<numRatings; j++){
            for(k=0; k<remQuarters; k++){
                cdr[i][j][k] = 100*i + 10*j +k;
            }
        }
    }

    for(i=0; i<5; i++) printf("%f\t",cdr[1][1][i]);
    printf("\n");
    for(i=0; i<5; i++) printf("%f\t",cdr[3][1][i]);
    printf("\n");
    for(i=0; i<5; i++) printf("%f\t",cdr[1][3][i]);
    printf("\n");
    for(i=0; i<5; i++) printf("%f\t",cdr[i][i][i]);
    printf("\n");
    printArray(cdr[1][1], 5);
    printArray(cdr[3][3], 5);

    scanf("%c", &dummy);
    return 0;
}

对于所有的反馈意见再次感谢。

Thanks again for all the feedback.

推荐答案

原来,作为写在code的工作(虽然它确实有空间风格的改进,有几个人指出的)。我找到了真正的问题在我的code到为缓冲区太大(这是在错误检查了我一个教训)某些文件的输入。

It turns out that the code as written worked (though it did have room for style improvements, as several people pointed out). I tracked the real problem in my code to some file input that was too big for the buffer (which is a lesson for me in error checking).

调试器给了我曾专注于code的这一部分,一些奇怪的输出。我还是不明白,为什么我的调试器这样当别人都没有,但我会处理的新的一天。

The debugger gave some weird output that had me focusing on this part of the code. I still don't understand why my debugger is doing this when others aren't, but I'll deal with that another day.

这篇关于为什么我用C多维动态分配不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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