C int指针分配大小

时间:2014-05-15 18:10:07

标签: c gcc

这是一个非常非常令人讨厌的问题。我的问题是,gcc似乎没有为我的int指针分配足够的空间。

以下是代码:

fns = (int*)calloc(c,sizeof(int));

所以,之后我用一个简单的循环填充这个和0:

offset = seekToFirstParam(fnString,n);
i = 0;
while(i<c) {
    tmp[i] = readNextParam(fnString,n,offset,&s);

    if (isFunctionString(tmp[i])) {
        fns[i] = 1;
    } else {
        fns[i] = 0;
    }

    i++;
}

所以这是一个&#34;标志&#34;数组,但是当我调试它,并打印我得到的元素:

156212102,0,0,0,1,1

或者是som。像这样。我没有得到它,因为如果在calloc方法中我写这样的1000:

fns = (int*)calloc(1000,sizeof(int));

工作正常。

好的,这是一个洞功能:

char **readFnParams(char *fnString, int n, int *count, int **func) {
    char **tmp;
    int  *fns = NULL;
    int c,i = 0,offset,s;

    c = getParamsCount(fnString,n);
    if (!c) {
        return NULL;
    }

    tmp = (char**)calloc(c,sizeof(char));
    fns = (int*)calloc(c,sizeof(int*));

    offset = seekToFirstParam(fnString,n); 
    while(i<c) {
        tmp[i] = readNextParam(fnString,n,offset,&s);

        if (isFunctionString(tmp[i])) {
            tmp[i]  = readNextFunctionParam(fnString,n,offset,&s);
            offset = seekToNextParam(fnString,n,offset + s - 1);
            fns[i] = 1;
        } else {
            fns[i] = 0;
            offset = seekToNextParam(fnString,n,offset);
        }

        i++;
    }
    *func = fns;
    *count = c;

    return tmp;
}

:)好的,这是一个孔.c文件。是的我以前的q。结束这个联系,因为它是一个功课。

#ifndef exccel_builder_source
#define exccel_builder_source

#include "exccel_builder.h"
#include "exccel_utils.h"
#include "exccel_function.h"

#include<stdlib.h>
#include<stdio.h>
#include<string.h>

table* _processing;

//A végére fűzi az új elemeket
void addProcess(cell_process **LIST, cell_process *new) {
    if (*LIST == NULL) {
        new->next = NULL;
        *LIST = new;
        return;
    }

    new->next = *LIST;
    *LIST = new;
}

void build(table* table) {
    int col = table->matrix->col;
    int row = table->matrix->row;
    int i,j;

    table_cell *cellTemp;
    _processing = table;

    for (i = 1; i<=row; i++) {
        for (j = 1; j<=col; j++) {
            cellTemp = getCell(table,i,j);
            if (cellTemp != NULL) {
                buildCell(cellTemp);
            }
        }
    }
}

void buildCell(table_cell *cell) {
    //Begins with '='
    if (isFunction(cell)) {
        buildCellWithFunction(cell);
    }
}

void printProcesses(cell_process *LIST, int tab) {
    cell_process *tmp = NULL;
    int i = 0;
    tmp = LIST;
    while(tmp != NULL) {
        i = 0;
        while(i++<tab) printf(" ");
        printf("%s, %d, paramPos: %i\n",tmp->func->name,tmp->func->paramsCount,tmp->paramPos);
        if (tmp->childs != NULL) {
            i = 0;
            while(i++<tab + 3) printf(" ");
            printf("Childs\n");
            printProcesses(tmp->childs, tab + 3);
        }
        tmp = tmp->next;
    }
}

void buildCellWithFunction(table_cell *cell) {
    cell_process *HEAD = NULL;
    buildCellProcessList(cell,&HEAD);
    cell->cp = HEAD;

    printf("%d,%d - cella:\n",cell->row,cell->col);
    printProcesses(HEAD,0);
}

void buildCellProcessList(table_cell *cell, cell_process **HEAD) {
    char *fnString;
    int  size;

    fnString = getCellStringValue(cell, &size);
    readFn(fnString,size,1,cell,HEAD,-1);
}

int readFn(char *fnString, int n, int offset, table_cell *cell, cell_process **LIST, int paramPos) {
    char *fnName, *fnParam;
    int  fnNameLength;
    int  *fnSig;
    int  fnSigN;
    int  fnSigI;
    int  sig;
    exccel_var *vtmp;
    exccel_function *ftmp;
    cell_process *ptmp;
    char **parameters;
    int  *fnIndexes;
    int paramsCount;
    int paramI;
    int i;

    fnName = readFnName(fnString,n,offset,&fnNameLength);
    ftmp   = getExccelFunction(fnName);
    if (ftmp == NULL) {
        return 0;
    }

    ptmp = (cell_process*)malloc(sizeof(cell_process));
    ptmp->cell = cell;
    ptmp->func = ftmp;
    ptmp->paramPos = paramPos; 
    ptmp->t    = _processing;
    ptmp->childs = NULL;

    addProcess(LIST,ptmp);

    parameters = readFnParams(fnString,n,&paramsCount,&fnIndexes);



    allocParams(ptmp->func,paramsCount);

    paramI     = 0;

    fnSig  = ftmp->signature;
    fnSigN = fnSig[0];
    fnSigI = 1;

    while(fnSigI <= fnSigN) {
        sig = fnSig[fnSigI];
        if (sig == FN_SIG_RANGE) {

            fnParam = parameters[paramI];
            vtmp    = createExccelRangeVarFromString(fnParam);
            //addParamToFunction(ftmp,vtmp);
            addParamToFunctionAtPosition(ftmp,vtmp,paramI);
            paramI++;

        } else if (sig == FN_SIG_LITERAL) {

            fnParam = parameters[paramI];
            if (fnIndexes[paramI] == 1) {
                readFn(fnParam,strlen(fnParam),0,cell,&((*LIST)->childs),paramI);
            } else {
                vtmp = createExccelVarFromString(fnParam);
                //addParamToFunction(ftmp,vtmp);
                addParamToFunctionAtPosition(ftmp,vtmp,paramI);
            }
            paramI++;

        } else if (sig == FN_SIG_LIST) {

            while(paramI<paramsCount) {
                fnParam = parameters[paramI];
                if (fnIndexes[paramI] == 1) {
                    readFn(fnParam,strlen(fnParam),0,cell,&((*LIST)->childs),paramI);
                } else {
                    vtmp = createExccelVarFromString(fnParam);
                    //addParamToFunction(ftmp,vtmp);
                    addParamToFunctionAtPosition(ftmp,vtmp,paramI);
                }
                paramI++;
            }

        } else {
            printf("Invalid signature %d\n",sig);
            exit(1);
        }

        fnSigI++;
    }

    return 1;
}

char *readFnName(char *fnString, int n, int offset, int *size) {
    char *fnName;
    int nameBuffer, i, j;

    i = offset;
    j = 0;

    nameBuffer = 8;
    fnName = (char *)calloc(nameBuffer,sizeof(char));    
    while(*(fnString + i) != '(' && i<n) {
        *(fnName + j++) = *(fnString + i++);
        if (j>=nameBuffer) {
            nameBuffer += 8;
            fnName = (char *)realloc(fnName, nameBuffer);
        }
    }

    *(fnName + j++) = '\0';
    *size = j;

    return fnName;
}

char **readFnParams(char *fnString, int n, int *count, int **func) {
    char **tmp;
    int  *fns = NULL;
    int c,i = 0,offset,s;

    c = getParamsCount(fnString,n);
    if (!c) {
        return NULL;
    }

    tmp = (char**)calloc(c,sizeof(char));
    fns = (int*)calloc(c,sizeof(*fns));

    offset = seekToFirstParam(fnString,n); 
    while(i<c) {
        tmp[i] = readNextParam(fnString,n,offset,&s);

        if (isFunctionString(tmp[i])) {
            tmp[i]  = readNextFunctionParam(fnString,n,offset,&s);
            offset = seekToNextParam(fnString,n,offset + s - 1);
            fns[i] = 1;
        } else {
            fns[i] = 0;
            offset = seekToNextParam(fnString,n,offset);
        }

        i++;
    }
    *func = fns;
    *count = c;

    return tmp;
}

int getParamsCount(char *fnString, int n) {
    int i = 0, c = 0, jump = 0;
    while(i<n) {

        if (fnString[i] == '(') {
            jump++;
        } else if (fnString[i] == ',') {
            if (jump == 1) c++;
        } else if (fnString[i] == ')') {
            jump--;
        }

        i++;
    }

    if (c > 0) {
        return c + 1;
    } else {
        return 1;
    }
}

int seekToFirstParam(char *fnString, int n) {
    int i = 0;
    while(fnString[i++] != '(' && i<n);

    return i;
}

int seekToNextParam(char *fnString, int n, int offset) {
    int i = offset;

    while(fnString[i++] != ',' && i<n);

    return i;
}

char *readNextParam(char *fnString, int n, int offset, int *size) {
    char *params, c;
    int paramBuffer, i, j;

    i = offset;
    j = 0;
    paramBuffer = 8;
    params = (char*)calloc(paramBuffer,sizeof(char));

    while((c = fnString[i++]) != ',' && c != ')' && c != '(' && i<n) {
        params[j++] = c;
        if (j >= paramBuffer) {
            paramBuffer += 8;
            params = (char*)realloc(params,paramBuffer);
        }
    }

    params[j] = '\0';
    *size = j;

    return params;
}

//Megfelelő számú nyitó ( - hez kell hogy legyen ugyanannyi )
char *readNextFunctionParam(char *fnString, int n, int offset, int *size) {
    char *fn, c;
    int  fnBf, i, j, fnStarted = 0, fnClosed = 0;

    i    = offset;
    j    = 0;
    fnBf = 8;

    fn = (char*)calloc(fnBf, sizeof(char));
    while((fnStarted != fnClosed || fnStarted == 0) && i<n) {
        c = *(fnString + i++);

        if (c == '(')
            fnStarted++;
        else if (c == ')')
            fnClosed++;

        *(fn + j++) = c;
        if (j >= fnBf) {
            fnBf += 8;
            fn = (char*)realloc(fn, sizeof(char) * fnBf);
        }

    }
    //*(fn + j++) = ')';
    *(fn + j++) = '\0';
    *size = j;

    return fn;
}

#endif

输入如下: = SZORZAT(MDETERM(A1:D4),NAGY(A1:D4,0),10,20,30)

1 个答案:

答案 0 :(得分:2)

在我看来,你没有正确分配,你有:

tmp = (char**)calloc(c,sizeof(char));

第一行tmp正在分配大小为c的{​​{1}}个元素(1个字节的c个元素),我想你想要char个大小为c的元素( c元素大小为每个元素4或8个字节,具体取决于您是32位还是64位平台)。由于您的例程char *正在返回readNextParam()以存储在此数组中,因此您需要将char *的calloc sizeof更改为:

tmp

因此,我相信当你写入流入你的其他数组的tmp = calloc(c,sizeof(char*)); 数组时,你会有内存覆盖。通过制作两个“1000”元素,你已经填充了第一个tmp,足以使覆盖仍然存在于同一块内存中。