在动态数组中获取错误数量的元素
Getting the wrong number of elements in a dynamic array
我是 C 的初学者,我正在尝试构建一个非常简单的动态数组。我的代码可以编译,但在返回数组大小时输出错误的大小。例如,当我测试它的最终数组大小为 0 时,它输出 13744632839234567870,这非常大而且非常错误。
我认为我的 array_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;
}
array_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()
不清楚。为什么在索引超出范围时暗示返回 pointer?
#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];
}
或
代码是否返回数组元素的地址?
//int indexget(struct array *v, long index) {
int *indexget(struct array *v, size_t index) {
if (index >= v->size) {
return NULL;
}
// return v->data[index];
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。
我是 C 的初学者,我正在尝试构建一个非常简单的动态数组。我的代码可以编译,但在返回数组大小时输出错误的大小。例如,当我测试它的最终数组大小为 0 时,它输出 13744632839234567870,这非常大而且非常错误。
我认为我的 array_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;
}
array_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()
不清楚。为什么在索引超出范围时暗示返回 pointer?
#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];
}
或
代码是否返回数组元素的地址?
//int indexget(struct array *v, long index) {
int *indexget(struct array *v, size_t index) {
if (index >= v->size) {
return NULL;
}
// return v->data[index];
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。