错误分配内存矩阵C?

时间:2015-10-26 08:37:14

标签: c matrix dynamic-memory-allocation

我正在开发一个程序,该程序从CSV文件读取并使用方法计算分数" calculateMLpa"。该方法接收char数组和10 float的数组,并在matrix 3x3中转换float数组。从数组中读取位置第3个数字时,在第4个数字中插入第4个数字相同。 I.E.

array value[]={0.000000;123.814934;234.000000;100.000000;166.000000; 203.086639;383.000000;186.000000;338.000000;173.098419 }
array traj[]={"0-0";"0-1";"0-2";"1-0";"1-1";"1-2";"2-0";"2-1";"2-2"}
Xn_val[]={"0","1","2"}

当在矩阵中变换时,结果为:

123.814934 234.000000 166.000000 
166.000000 203.086639 186.000000 
186.000000 338.000000 173.098419

虽然[0; 2]的预期值是100.000000而[1; 2] = 383.000000,但是当打印当前值的traj时它是正确的。 我该如何解决这个问题?

代码全在这里:

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include <stdbool.h>
#include <ctype.h>

#define ARRAYSIZE(x)  (sizeof(x)/sizeof(*(x)))
int csv_parse ( char *line, int size )
{
    char *p;
    char *dp;
    int inquote;
    int na;
    int nTo_comma;
    char prevc = ',';
    char *list[256];

    dp = NULL;
    // inquote = 0;
    na = 0;
    prevc = ';';

    nTo_comma=0;
    for ( p = line; *p != '\n'; p++ )
    {
        nTo_comma++;
        list[nTo_comma] = p;
        if(*p == prevc)
        {
            printf("%s\t", list);

            return na;
        }

    }

    printf("\n");

    return na;
}


double calculateMLpa(const char *Xn_val[], char *traj[], float value[], double alphaxixj, double tauxi, int sz, int dim) {
    double mlx = 0;
    double v;
    double alphaxi;
    char *state;
    int i;
    int p;
    int j;
    int k;
//  int sz = sizeof(Xn_val) / sizeof(int);
//  int dim = sizeof(traj) / sizeof(char);
    double trns[sz][sz];
    double m[sz];
    char *trat="-";



    // m[xi] values: the number of transitions leaving the state xi
    printf("%d %d \n",sz,dim);
    int cont=0;
    for (i = 0; i <= sz; i++) {
        m[i] = 0.0;
        for (j = 0; j <= sz; j++) {
            v = 0.0;
            int newlength = strlen(Xn_val[i])+strlen(trat)+strlen(Xn_val[j])+1;
            state = malloc(sizeof(char)*newlength);
            if(state != NULL){
                state[0] = '\0';
                strcat(state,Xn_val[i]);
                strcat(state,trat);
                strcat(state,Xn_val[j]);
                printf("%s ",state);
            }else {
                printf(stderr,"malloc failed!\n");
            }
            //          for (k=0; k<=dim;++k){
            if (traj[cont] != NULL ){
                if (strcmp(traj[cont],state)==0){
                    v = value[cont+1];
                    printf("%f \n",v);
                }
            }

            trns[i][j] = v;

            printf("%f - \n",trns[i][j]);
            if (strcmp(Xn_val[i],Xn_val[j])!=0)
                m[i] = m[i] + v;

            cont++;
        }
    }
    for (i=0;i<=sz;++i){
        for(j=0;j<=sz;++j){

            printf("%f ",trns[i][j]);
        }
        printf("\n");
    }


    for (p=0;p<=sz;++p){
        printf("%f - \n",m[p]);
    }
    printf("%f %f\n",trns[0][1],trns[0][2]);
    alphaxi = alphaxixj * (((double) sz) - 1.0);
    alphaxi = alphaxixj;
    printf("%d ",sz);
    for (i = 0; i <= sz; i++) {
        for (j = 0; j <= sz; j++) {
            // xi!=xj
            if (strcmp(Xn_val[i], Xn_val[j])!=0) {
                mlx = mlx + lgamma(alphaxixj + trns[i][j]) - lgamma(alphaxixj);
            }
            // xi
            else {
                mlx = mlx + lgamma(alphaxi) - lgamma(alphaxi + m[i]);
                mlx = mlx + lgamma(alphaxi + m[i] + 1.0)+ (alphaxi + 1.0) * log(tauxi);
                mlx = mlx - lgamma(alphaxi + 1.0)- (alphaxi + m[i] + 1.0) * log(tauxi + trns[i][j]);
            }
        }
    }

    return (mlx);
}


#define MAXFLDS 200     /* maximum possible number of fields */
#define MAXFLDSIZE 32   /* longest possible field + 1 = 31 byte field */

void parse(char *record, char *delim, char arr[][MAXFLDSIZE], int *fldcnt) {
    char*p = strtok(record, delim);
    int fld = 0;
    while (p) {
        strcpy(arr[fld], p);
        fld++;
        p = strtok('\0', delim);
    }
    *fldcnt = fld;
}

void main() {
    printf("inizio\n");
    FILE *pf;
    int N=20;
    bool first=true;
    const char *a[]={"0","1","2"};
    char *traject[]={"0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2"};
    double bs=0;
    char *trat="-";


    pf=fopen("//home//user//prova.csv","r");

    float array[10][10];
    float *t;
    char *str= "hello";
    char *state;
    t = (float *)malloc(N * sizeof(float));
    int f=0;
    if (pf)
    {

        size_t i, j, k;
        char buffer[BUFSIZ], *ptr;
        /*
         * Read each line from the file.
         */
        for ( i = 0; fgets(buffer, sizeof buffer, pf); ++i )
        {
            /*
             * Parse the comma-separated values from each line into 'array'.
             */
            for ( j = 0, ptr = buffer; j < ARRAYSIZE(*array); ++j, ++ptr )
            {
                array[i][j] = strtof(ptr, &ptr);
            }
        }
        fclose(pf);}

    else /* fopen() returned NULL */
    {
        perror(pf);
    }

    for(f=0; f<10; ++f){
        if(f==0){}
        else if(f==1 && array[f][8]==0)
            array[f][8]=123.8149353;
        t[f]=array[f][8];
        //printf("%f \n",t[f]);
    }
    for (f=0;f<10; ++f){
        printf("%f - ",t[f]);
    }
    //printf("%s, %s, %s \n",a[0],a[1],a[2]);

    printf("start\n");
    int sz = sizeof(a) / sizeof(char);
    int dim = sizeof(traject) / sizeof(char);
    printf("%d , %d \n",sz,dim);
    bs=calculateMLpa(a,traject,t,1.0,0.1,sz,dim);
    printf("done \n");
    printf("%f ",bs);


}

EDIT 我尝试传递数组大小

sz=sizeof(a)/sizeof(char) 
dim = sizeof(traject) / sizeof(char);

但它们的值分别为24和72,执行停止在0-2值100.000000

2 个答案:

答案 0 :(得分:2)

一个主要问题是,当您将数组传递给函数时,它们会衰减为指针,而用于获取数组大小的sizeof技巧将无效。

您需要将实际的数组大小作为参数传递。

答案 1 :(得分:2)

传递给函数的数组衰减到指向数组开头的指针。所以

#define ARRAYSIZE(x)  (sizeof(x)/sizeof(*(x)))

在这种情况下检查其大小时,不会返回任何有意义的内容

要修复,请将数组大小作为附加参数传递。