简体   繁体   English

C-包含字符串的表的动态内存分配

[英]C - Dynamic memory allocation for table that contains strings

I try to make the memory allocation dynamically for table that contains the strings. 我尝试为包含字符串的表动态地进行内存分配。
In my case, I have to use it dynamically, because I don't know how rows and columns the program will get. 就我而言,我必须动态使用它,因为我不知道程序将如何获得行和列。
Here is my code for two function: 这是我的两个功能的代码:
1.The first is allocating the memory for the table only. 1.首先是仅为表分配内存。
2.The second should free all allocated memory. 2.第二个应该释放所有分配的内存。
3.In the main function I'm allocating the memory for the string and copy the part of the predefined string (it's dummy code, for example only) 3.在主要功能中,我为字符串分配内存并复制预定义字符串的一部分(仅是虚拟代码,仅作为示例)

The result is "Runtime error" What am I doing wrong in the code? 结果是“运行时错误”。我在代码中做错了什么?

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

char *** buildOptions(int rows, int columns) {
    int i=0;
    printf("1...\n");
    char ***options = (char ***)malloc(rows * sizeof(char **));
    printf("2...\n");
    for(i=0; i<rows; i++) {
        printf("3...\n");
        options[i] = (char **)malloc(columns * sizeof(char *));
        printf("4...\n");
    }
    return options;
}

void freeOptions(char ****options, int rows, int columns) {
    int i, j;
    for(i=0; i<rows; i++) {
        for(j=0; j<columns; j++) {
            if(*options[i][j])
                free(*options[i][j]);
        }
        if(*options[i]) {
            free(*options[i]);
        }
    }
    free(*options);
}

int main(void) {
    int i, j, k;
    char * str = "123456789abcdefghjkl\0";
    char ***options = buildOptions(5, 3);
    printf("Starting...\n");
    for(i=0; i<5; i++) {
        for(j=0; j<3; j++) {
            options[i][j] = (char *)calloc((i+j+2)+1, sizeof(char));
            strncpy(options[i][j], str, i+j+2);
        }
    }

    for(i=0; i<5; i++) {
        for(j=0; j<3; j++) {
            printf(">>options[%d][%d]=%s<<\n", i,j, options[i][j]);
        }
    }

    freeOptions(&options, 5, 3);

    //here I want to check if the memory is freed
    for(i=0; i<5; i++) {
        for(j=0; j<3; j++) {
            printf(">>options[%d][%d]=%s<<\n", i,j, options[i][j]);
        }
    }


    return 0;
}

Change the declaration of freeOptions to freeOptions的声明更改为

void freeOptions(char ***options, int rows, int columns)

the call to 致电

 freeOptions(options, 5, 3);

and the call to free() inside the freeOptions to 以及在freeOptions内对free()的调用

  free(options[i][j]);

The cause of your segmentation fault is in how you access the array in freeOptions : 分段错误的原因在于您如何在freeOptions访问数组:

*options[i][j]

is the same as: 是相同的:

*(options[i][j])

bot options is the address of your options in main , so you should dereference options` first: BOT options is the address of your选择in, so you should dereference options`第一:

(*options)[i][j]

There's actually no need to pass in the char ***options from main by reference unless you plan to set *options to NULL in freeOptions . 实际上,除非您打算在freeOptions中将*options设置为NULL ,否则实际上不需要通过引用传递mainchar ***options A better way to implement freeOptions has been pointed out in the other Michael's answer. 其他迈克尔的答案中指出了实现freeOptions更好方法。

//here I want to check if the memory is freed

You can't find out whether the free has succeeded by accessing now invalid memory and causing undefined behaviour. 你不能找出是否free已通过访问,现在无效的内存,并导致未定义的行为成功。 The data may still be there, but it might as well contain garbage. 数据可能仍然存在,但其中也可能包含垃圾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM