呼叫类型结构功能和返回指针不运行 [英] Calling Function with type struct and returning pointer does not run

查看:123
本文介绍了呼叫类型结构功能和返回指针不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行程序的麻烦,有没有在$ C $个cblocks弹出警告或错误。麻烦的是当我创造指针类型的ArrayList结构,并尝试用malloc动态分配内存。我使用的语法' - >'。我已经在这了,没有真正的线索小时。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义DEFAULT_INIT_LEN 10 //这是在另一个头文件typedef结构的ArrayList {//这也是另外一个头文件
    字符数组**;
    INT大小;
    INT能力;
} ArrayList的;诠释主要(无效){
    ArrayList的*测试= createArrayList(12);
    的printf(大小:%d个容量数:%d \\ n,测试 - >的大小,测试 - >能力);
}ArrayList中* createArrayList(INT长度){
    INT I = 0; //索引变量
    ArrayList的* R;
    如果(长度GT; DEFAULT_INIT_LEN){
        R->阵=的malloc(sizeof的(字符)*(长+ 1)); //创建内部数组的内存
        R->容量=长度;
        R->大小= 0;
        对于(i = 0; I<长度;我++)//设置成员数组为NULL
            R->阵列[我] = NULL;
        的printf(创建大小%d个\\ n新的ArrayList,长度);
    }
    其他{
        R->阵=的malloc(sizeof的(字符)*(DEFAULT_INIT_LEN + 1)); //创建内部数组的内存
        R->容量= DEFAULT_INIT_LEN;
        R->大小= 0;
        对于(i = 0; I< D​​EFAULT_INIT_LEN;我++)//设置成员数组为NULL
            R->阵列[我] = NULL;
        的printf(创建大小%d个新的ArrayList,DEFAULT_INIT_LEN);
    }
    返回ř;
}


解决方案

 的ArrayList *
createArrayList(INT长度)
{
    ArrayList的* R =的malloc(sizeof的(* R));
    如果(R == NULL)返回NULL;    长度= MAX(长度,DEFAULT_INIT_LEN); //不重复code。永远。    R->阵=释放calloc(长+ 1的sizeof(R 34 GT;阵[0]));
    如果(R 34 GT;阵== NULL){免费(R);返回NULL; } //经常检查指针    R->大小= 0;
    R->容量=长度;    的printf(创建大小%d个\\ n新的ArrayList,长度);    返回ř;
}

和你可能不希望分配长+ 1 元素,因为你有 R->能力

I'm having trouble running the program, there are no warnings or errors popping up in CodeBlocks. The trouble comes when I create an ArrayList struct of pointer type and try to dynamically allocate memory using malloc. I'm using the syntax '->.' I've been at this for hours with no real leads.

#include <stdio.h>
#include <string.h>
#define DEFAULT_INIT_LEN 10//this is in another header file

typedef struct ArrayList{//this is also in another header file
    char **array;
    int size;
    int capacity;
} ArrayList;

int main(void){
    ArrayList *test=createArrayList(12);
    printf("size: %d capacity: %d\n", test->size, test->capacity);
}

ArrayList *createArrayList(int length){
    int i=0;//index variables
    ArrayList *r;
    if (length>DEFAULT_INIT_LEN){
        r->array=malloc(sizeof(char)*(length+1));//create memory for internal array
        r->capacity=length;
        r->size=0;
        for (i=0; i<length; i++)//sets members in the array to NULL
            r->array[i]=NULL;
        printf("Created new ArrayList of size %d\n", length);
    }
    else{
        r->array=malloc(sizeof(char)*(DEFAULT_INIT_LEN+1));//create memory for internal    array
        r->capacity=DEFAULT_INIT_LEN;
        r->size=0;
        for (i=0; i<DEFAULT_INIT_LEN; i++)//sets members in the array to NULL
            r->array[i]=NULL;
        printf("Created new ArrayList of size %d", DEFAULT_INIT_LEN);
    }
    return r;
}

解决方案

ArrayList *
createArrayList(int length)
{
    ArrayList *r = malloc(sizeof(*r));
    if (r == NULL) return NULL;

    length = MAX(length, DEFAULT_INIT_LEN); // do not duplicate code. ever.

    r->array = calloc(length + 1, sizeof(r->array[0]));
    if (r->array == NULL) { free(r); return NULL; } // always check pointers

    r->size = 0;
    r->capacity = length;

    printf("Created new ArrayList of size %d\n", length);

    return r;
}

And you probably do not want to allocate length + 1 elements, because you have r->capacity.

这篇关于呼叫类型结构功能和返回指针不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆