Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 计数阵列中已分配内存时出现意外输出_C_Pointers_Multidimensional Array_Struct_Dynamic Data - Fatal编程技术网

C 计数阵列中已分配内存时出现意外输出

C 计数阵列中已分配内存时出现意外输出,c,pointers,multidimensional-array,struct,dynamic-data,C,Pointers,Multidimensional Array,Struct,Dynamic Data,我正在进行一个动态分配项目,我得到了一个测试用例的持续的意外答案。输出总是打印出“尺寸测试:11”,我不知道为什么 getSize遍历所有值并将其添加到计数中,如果实际值不为NULL,则对数组中的所有有效元素进行计数 如果ArrayList size中的变量输出不正确,我将使用getSize作为后备方法。此外,使用calloc创建并引用到test的数组也很古怪。如果我做一个for循环来打印所有的值,它会中途停止,在大小为25的数组中崩溃,它总是在索引7之后停止。然而,如果我打印索引if seg

我正在进行一个动态分配项目,我得到了一个测试用例的持续的意外答案。输出总是打印出“尺寸测试:11”,我不知道为什么

getSize遍历所有值并将其添加到计数中,如果实际值不为NULL,则对数组中的所有有效元素进行计数

如果ArrayList size中的变量输出不正确,我将使用getSize作为后备方法。此外,使用calloc创建并引用到test的数组也很古怪。如果我做一个for循环来打印所有的值,它会中途停止,在大小为25的数组中崩溃,它总是在索引7之后停止。然而,如果我打印索引if seg faults on与循环,它就可以完美地工作。逻辑是错误的,还是我必须冲洗一些东西

如果我将测试用例更改为数组的大小越来越大,同样的事情也会发生在打印出常量int的情况下

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;

int main(void){
    struct ArrayList *test;
    test=createArrayList(25);
    int i=getSize(test);
    printf("test of size: %d", i);
    return 0;
}

//creates the array list and allocated memory for it
ArrayList *createArrayList(int length){
    struct ArrayList *r = malloc(sizeof(*r));
    if (r == NULL)//Returns null if malloc does not work
        return NULL;
    length=(length<DEFAULT_INIT_LEN) ? DEFAULT_INIT_LEN: length;//figures which value is         greater and keeps it
    r->array=calloc(sizeof(char), (length+1));
    if (r->array == NULL){//returns null if malloc does not work
            printf("error\n");
            return NULL;
    }
    r->size = 0;
    r->capacity = length;
    printf("Created new ArrayList of size %d.\n", length);
    return r;
}
//the function im having trouble with
int getSize(ArrayList *list){
    int i=0, count=0;//index variables
    if (list->array==NULL)
        return -1;
    for (i=0; i<(list->capacity-1); i++){//goes through all indexs of internal array and     conuts valid elements. this is where im having trouble specifically
        if (list->array[i]!=NULL)
            count++;
    }
    return count;
}
这是错误的:

r->array=calloc(sizeof(char), (length+1));
它应该是sizeofchar*,因为您正在为指向char的指针数组分配空间。或者,更好的是,根本不硬编码数组元素的类型,而是使用*r->array:


分配length+1元素,然后在getSize中只增加到capacity-1,这也有点奇怪。我想您只需要长度。

在我看来,这部分试图索引不存在的元素:

for (i = 0; i < (list->capacity - 1); i++)
{
    if(list->array[i] != NULL)
        count++;
}

你又忘了在calloc失败时释放r…我告诉过你sizeofchar。你为什么不至少分析一下与原始版本的差异?可能是重复的我对编程非常陌生,谢谢你耐心地对待我。我还没有深入讨论解除分配,这是我第一次尝试。第一次很好,第二次崩溃,因为r->array尚未分配,所以*r->array指向nowhere@esskar:错了。sizeof*r->array在编译时计算;它仅用于推断类型。这是绝对正确的。像这样的一件小事竟然能改变整个程序的功能,真是不可思议。我想当一切都完美运转的时候,这只会让人更加兴奋。谢谢如果我错了,请纠正我,但是while循环不会考虑非NULL元素不连续的情况?@Hammad:你没有错。对我来说,假设他们是真的似乎很自然。
for (i = 0; i < (list->capacity - 1); i++)
{
    if(list->array[i] != NULL)
        count++;
}
while (list->array[i++] != NULL)
{
    count++;
}