C - 使用malloc分配的内存太多

时间:2017-04-08 10:59:14

标签: c arrays memory memory-management

我正在为一所学校项目工作,我必须创建并将二叉树放入动态数组中。 我指定我使用GCC(GNU编译器)在Windows上工作。

typedef struct s_node * TN;
struct s_node {
    char * chain; //Name of the tree
    int occ; //Number of occurences
    int ht; //Tree's height
};
typedef struct s_binary_tree * TBA;
struct s_binary_tree {
    TN root;
    TBA stl; //Sub Tree Left
    TBA str; //Sub Tree Right
};

首先,我使用malloc()分配内存来创建一个包含x平方二叉树的数组,然后我为二叉树分配内存。

TBA create_Binary_Tree(char * chain, int occ, int number_letters) {
    TBA tree = malloc(sizeof(TBA));
    tree->root = malloc(sizeof(TN));
    tree->root->chain = malloc(sizeof(char) * (number_letters + 1)); //+1 for the '\0'

    *(tree->root->chain) = '\0';
    strcpy(tree->root->chain,chain);
    tree->root->occ = occ;
    tree->root->ht = 1;

    tree->stl = NULL;
    tree->slr = NULL;

    return tree;
}

TBA * create_Array_Binary_Tree(char * fileName) {
    FILE * file;
    file = fopen(fileName,"r");
    if(!file) {
        exit(EXIT_FAILURE);
    }

    int number_letters;
    fscanf(file,"%d",&number_letters); //number_letters is the number of square that I want to allocate
    TBA * arr = malloc(sizeof(TBA) * number_letters);
    char * letter = malloc(sizeof(char) * 256);
    int occ, i;


    for(i = 0; i < number_letters; i++) { //number_letters = 1 in our example
        fscanf(file,"%s %d",letter,&occ);
        *arr = create_Binary_Tree(letter,occ,number_letters);
    printf("--Adr = %p--\n"arr); //Print the adress
        arr++;
    }
    arr -= i; //Reset the arr pointer

    fclose(file);

    return arr;
}

我的问题是当我计算数组的大小时,程序告诉我数组大小为x + 38平方。

int size_Array_Binary_Tree(TBA * arr) {
    int sz_arr = 0;

    while(*arr != NULL) {
printf("-T = %d\tAdr = %p-\n",sz_arr,arr); //Print the adress
    sz_arr++;
    arr++;
}

return sz_arr;
}

终端:

--Adr = 0000000000951430-- //1 square allocated
-T = 0  Adr = 0000000000951430- //The square allocated
-T = 1  Adr = 0000000000951438- //?
-T = 2  Adr = 0000000000951440- //?
-T = 3  Adr = 0000000000951448- //...
-T = 4  Adr = 0000000000951450-
-T = 5  Adr = 0000000000951458-
-T = 6  Adr = 0000000000951460-
-T = 7  Adr = 0000000000951468-
-T = 8  Adr = 0000000000951470-
-T = 9  Adr = 0000000000951478-
-T = 10 Adr = 0000000000951480-
-T = 11 Adr = 0000000000951488-
-T = 12 Adr = 0000000000951490-
-T = 13 Adr = 0000000000951498-
-T = 14 Adr = 00000000009514A0-
-T = 15 Adr = 00000000009514A8-
-T = 16 Adr = 00000000009514B0-
-T = 17 Adr = 00000000009514B8-
-T = 18 Adr = 00000000009514C0-
-T = 19 Adr = 00000000009514C8-
-T = 20 Adr = 00000000009514D0-
-T = 21 Adr = 00000000009514D8-
-T = 22 Adr = 00000000009514E0-
-T = 23 Adr = 00000000009514E8-
-T = 24 Adr = 00000000009514F0-
-T = 25 Adr = 00000000009514F8-
-T = 26 Adr = 0000000000951500-
-T = 27 Adr = 0000000000951508-
-T = 28 Adr = 0000000000951510-
-T = 29 Adr = 0000000000951518-
-T = 30 Adr = 0000000000951520-
-T = 31 Adr = 0000000000951528-
-T = 32 Adr = 0000000000951530-
-T = 33 Adr = 0000000000951538-
-T = 34 Adr = 0000000000951540-
-T = 35 Adr = 0000000000951548-
-T = 36 Adr = 0000000000951550-
-T = 37 Adr = 0000000000951558-
-T = 38 Adr = 0000000000951560- //?

返回的大小应为1,但返回39。 你能帮我吗?

编辑:我从类型中删除了指针并调整了其余部分,但问题仍然存在。

typedef struct s_node TN;
struct s_node {
    ...
};

typedef struct s_binary_tree TBA;
struct s_binary_tree {
    TN * root;
    TBA * stl;
    TBA * str;
};

TBA * create_Binary_tree(...) {
    TBA * tree = malloc(sizeof(TBA));
    tree->root = malloc(sizeof(TN));
    ...
    return tree;
}

TBA ** create_Array_Binary_Tree(...) {
    ...
    TBA ** arr = malloc(sizeof(TBA *) * number_letters);
    ...
    for(i = 0; i < number_letters; i++) {
        ...
        arr[i] = malloc(sizeof(TBA));
        arr[i] = create_Binary_Tree(...);
        ...
    }
    ...
    return arr;
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

此程序中有几个错误:

TBA create_Binary_Tree(char * chain, int occ, int number_letters) {
    TBA tree = malloc(sizeof(TBA));
    ...
}

TBA是结构的指针。当您分配sizeof(TBA)时,您要求分配与指针需要的内存一样多的内存,这只是4个字节(32位)或8个字节(64位)。您想为 struct 分配空间:

TBA tree = malloc(sizeof(struct s_binary_tree));

同样的问题:

tree->root = malloc(sizeof(TN));

应该是:

tree->root = malloc(sizeof(struct s_node));

只有TBA * arr = malloc(sizeof(TBA) * number_letters);实际上是正确的。

Avoid strcpy

使用第二个变量来存储原始指针或您想要修改的变量,而不是像arr -= i;那样使用凌乱的指针算术。

您不是NULL终止arr。分配后,可能会有垃圾。如果您依赖于作为哨兵的话,最好确保用NULL填充它:

memset(arr, 0, sizeof(TBA) * number_letters);
// Or use `calloc`, `bzero`, ...

这可能是您size_Array_Binary_Tree给您一个奇怪结果的原因。