C 在动态数组中获取错误数量的元素

C 在动态数组中获取错误数量的元素,c,dynamic-arrays,C,Dynamic Arrays,我是C语言的初学者,我正在尝试构建一个非常简单的动态数组。我的代码可以编译,但返回数组大小时输出的大小错误。例如,当我测试它的最终数组大小为0时,它输出13744632839234567870,这是非常大而且非常错误的 我认为我的array\u size函数没有错。我怀疑它在append中出错,但我就是找不到它有什么问题 如果有人愿意帮助我,我将不胜感激 #include <stdlib.h> struct array { long size; long capac

我是C语言的初学者,我正在尝试构建一个非常简单的动态数组。我的代码可以编译,但返回数组大小时输出的大小错误。例如,当我测试它的最终数组大小为0时,它输出13744632839234567870,这是非常大而且非常错误的

我认为我的
array\u size
函数没有错。我怀疑它在
append
中出错,但我就是找不到它有什么问题

如果有人愿意帮助我,我将不胜感激

#include <stdlib.h>

struct array
{
    long size;
    long capacity;
    int* data;
};

struct array* array_init(long initial_capacity) {
    struct array* v = malloc(sizeof(struct array)); 
    if (v==NULL){
        return NULL;
    }
}    

int append(struct array *v, int elem) {
    if (v->size >= v->capacity) {

        v->capacity *= 2;
        v->data = realloc(v->data, sizeof(int) * v->capacity);
    }
    v->data[v->size] = elem;
    v->size++;
    return 0;    
}

int indexget(struct array *v, long index) {
    if (index >= v->size) {
        return NULL;
    }
    return v->data[index];
}

long array_size(struct array *v) {
    return v->size;
}
#包括
结构体数组
{
长尺寸;
长容量;
int*数据;
};
结构数组*数组初始化(长初始容量){
结构数组*v=malloc(sizeof(结构数组));
如果(v==NULL){
返回NULL;
}
}    
int-append(结构数组*v,int元素){
如果(v->尺寸>=v->容量){
v->容量*=2;
v->data=realloc(v->data,sizeof(int)*v->capacity);
}
v->数据[v->大小]=元素;
v->size++;
返回0;
}
int indexget(结构数组*v,长索引){
如果(索引>=v->大小){
返回NULL;
}
返回v->数据[索引];
}
长数组大小(结构数组*v){
返回v->size;
}
数组_init()
未向
.size
.capacity
成员分配任何内容

建议的修改:

struct array {
    // long size;
    // long capacity;
    // `size_t` is the right-size for array indexing.
    // Be mindful that `size_t` is some _unsigned_ type.
    size_t size;
    size_t capacity;
    int* data;
};

// struct array* array_init(long initial_capacity) {
struct array* array_init(size_t initial_capacity) {
    struct array* v = malloc(sizeof(struct array)); 
    if (v == NULL) {
       return NULL;
    }
    v->data = malloc(sizeof(int)*initial_capacity );
    // If initial_capacity is 0, a NULL return does not certainly mean out of memory
    //if (v->data==NULL){
    if (v->data==NULL && initial_capacity != 0){
       free(v); // also free prior allocation
       return NULL;
    }

    // Add
    v->size = 0;
    v->capacity = initial_capacity;

    return v;
}    

v->capacity*=2
较弱,因为不知道
v->capacity>0

int append(struct array *v, int elem) {
    if (v->size >= v->capacity) {
        // v->capacity *= 2;
        v->capacity = v->capacity > 0 ? v->capacity*2 : 1;

indexget()
不清楚。为什么要在索引超出范围时返回指针

#define BAD_VALUE 0  /* or some unused `int` value for the application */
int indexget(struct array *v, long index) {
    // if (index >= v->size) {  incomplete test if `index` is signed
    if (index >= v->size || index < 0) {
        // return NULL;
        return BAD_VALUE;
    }
    return v->data[index];
}

append()
缺少重新分配成功检查

        // v->data = realloc(v->data, sizeof(int) * v->capacity);
        void *p = realloc(v->data, sizeof(int) * v->capacity);
        if (p == NULL) {
          return EXIT_FAILURE; // Handle out-of-memory in some fashion
        }
        v->data = p;

array_init
内部,您应该将
size
capacity
设置为0,否则它们将具有随机值


另外,在
append
内部,在
realloc
之后,您需要检查NULL。

您在哪里初始化
v->size
?请记住
malloc
不会初始化它分配的内存内容。内存的内容是不确定的,看起来像是随机的或垃圾。malloc(0)没有很好的定义。@Lundin
malloc(0)
定义很好,手册页上说:如果大小为0,则malloc()返回NULL或一个唯一的指针值,以后可以成功地传递给free(),但实现是有定义的。忘记man,阅读C17 7.22.3
如果请求的空间大小为零,则行为是实现定义的:要么返回空指针,要么行为就好像大小是某个非零值,除非返回的指针不能用于访问对象。
还建议在适用的情况下,在
数组中设置参数。此外,函数命名不一致,可能它们都应该有
数组
前缀。
        // v->data = realloc(v->data, sizeof(int) * v->capacity);
        void *p = realloc(v->data, sizeof(int) * v->capacity);
        if (p == NULL) {
          return EXIT_FAILURE; // Handle out-of-memory in some fashion
        }
        v->data = p;