在尝试实现字符串数组时,我做错了什么?

时间:2013-05-27 18:53:17

标签: c memory dynamic struct allocation

typedef struct ArrayList
{
    // We will store an array of strings (i.e., an array of char arrays)
    char **array;

    // Size of list (i.e., number of elements that have been added to the array)
    int size;

    // Length of the array (i.e., the array's current maximum capacity)
    int capacity;

} ArrayList;

以下函数应该为包含结构的头文件支持的字符串数组动态分配内存(参见上文):

void panic(char *s)
{
    fprintf(stderr, "%s", s);
    exit(1);
}

ArrayList *createArrayList(int length){
    ArrayList *n = malloc(sizeof(ArrayList));

    int initial = 0, i;

    if (length > DEFAULT_INIT_LEN)
    {
        n->array = (char **)malloc(length * sizeof(int*));
        n->capacity = length;

        for (i = 0; i< n->capacity; i++)
        {
            n->array[i] = NULL;
        }
    }
    else
    {
        n->array = (char **)malloc(DEFAULT_INIT_LEN * sizeof(int*));
        n->capacity = DEFAULT_INIT_LEN;

        for (i = 0; i< n->capacity; i++)
        {
            n->array[i] = NULL;
        }
    }

    if (n->array == NULL)
        panic("ERROR: out of memory in Mylist!\n");

    n->size = initial;

    printf("-> Created new ArrayList of size %d\n", n->capacity);
    return n;
}

然后我有另一个函数应该打印当前在createArrayList函数创建的新分配数组中的所有字符串:

void printArrayList(ArrayList *list)
{
    int i;

    for(i=0; i<list->capacity; i++)
    {
        if (list->array[i] == NULL)
            printf("(empty list)\n");
        else
            printf("%s\n",list->array[i]);

    }
}

当我在main函数中实现printArrayList函数(上面)时,输出为:

-> Created ArrayList of size 10 
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)

但是,如果我在createArrayList函数中插入strcpy(n->array[1], "apple");作为测试2D数组保存字符串的能力的方法,则输出为:

-> Created ArrayList of size 10 

...然后崩溃

所以我的问题是我做错了什么?我错误地为我的阵列分配了记忆吗?我想得到它所以输出是:

-> Created ArrayList of size 10 
(empty list)
apple
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)
(empty list)

2 个答案:

答案 0 :(得分:1)

除了为ArrayList分配内存外,还需要为每个字符串分配存储空间。如果要设置数组的第二个元素,可以使用类似

的方法
void insert_at(ArrayList* arraylist, const char* str, int index)
{
    arraylist->array[index] = malloc(strlen(str)+1);
    if (arraylist->array[index] != NULL) {
        strcpy(arraylist->array[index], str);
    }
}

并将其称为

insert_at(n, 1, "apple");

顺便说一下,你的代码就像

n->array = (char **)malloc(length * sizeof(int*));

应该是

n->array = malloc(length * sizeof(char*));

(它是一个指向char而不是int的指针数组,你不应该从C malloc转换回来

答案 1 :(得分:0)

3个问题

1)小:改变

n->array = (char **)malloc(length * sizeof(int*));

n->array = (char **)malloc(length * sizeof(char*));

2)在if (n->array == NULL)

之后进行n->array = (char **)malloc(... direclty的测试
n->array = (char **)malloc(DEFAULT_INIT_LEN * sizeof(int*));
if (n->array == NULL)
   panic("ERROR: out of memory in Mylist!\n");

3)最重要的。如果不先为strcpy(n->array[1], "apple");分配内存,则无法n->array[1]。像

这样的东西
n->array[1] = malloc(strlen("Fred")+1);
strcpy(n->array[1], "Fred");
相关问题