任务创建灵活(动态内存分配)数组

时间:2012-05-03 15:46:22

标签: c

我们已经完成了在C中编写动态数据结构的任务。我对C仍然是一个令人难以置信的新手,我真的很感激,如果有人可以给我一些关于我做错的指示(lol) /我应该多读些什么。

这是我到目前为止所拥有的:

flexarray.h

#ifndef FLEXARRAY_H
#define FLEXARRAY_H

typedef struct flexarrayrec flexarray;

flexarray *flexarray_new();
extern void flexarray_add(flexarray *a, char item);
extern void flexarray_set(flexarray *a, char item, int index);
extern char flexarray_get(flexarray *a, int index);
extern void flexarray_trim(flexarray *a);
extern void flexarray_print(flexarray *a);

#endif  /* FLEXARRAY_H */

flexarray.c

#include "flexarray.h"
#include "mylib.h"

#define DEFAULT_CAPACITY 10

typedef struct flexarray{
    int size;
    int capacity;
    char *array;
};

flexarray *flexarray_new(){
    flexarray a;
    a->capacity = DEFAULT_CAPACITY;
    a->size = 0;
    a->array = (char*) erealloc (a ,a->capacity * sizeof(char)); 
    return a;
}

void grow(flexarray a){
    a->capacity = a->capacity * 2;
    a->array = (int*) erealloc (a ,a->capacity * sizeof(int)); 
}

void flexarray_add(flexarray a, char item){
    if(a->size == a->capacity) grow(a);
    a->array[a->size - 1] = item;
    a->size++;    
}

void set(flexarray a, char c, int index){
    if(index < 0 || index  > a->size) return;
    a->array[index] = c;
}

char get(flexarray a, int index){
    if(index < 0 || index  > a->size) return;
    return a->array[index];
}


void flexarray_trim(flexarray a){
    if(a->size == a->capacity) return;
    a->capacity = a->size;
    a->array = (char*) erealloc (a, a->capacity * sizeof(char)); 
}

void flexarray_print(flexarray a){
    int i;
    printf("[");
    for(i = 0; i < a->size - 1; i++){
        printf("%c,", a->array[i]);
    }
    printf("%c]\n", a->array[i + 1]);
}

谢谢

编辑当前编译日志:

flexarray.c:10:1: warning: useless storage class specifier in empty declaration [enabled by default]
flexarray.c: In function 'flexarray_new':
flexarray.c:13:15: error: storage size of 'a' isn't known
flexarray.c: At top level:
flexarray.c:20:21: error: parameter 1 ('a') has incomplete type
flexarray.c:25:30: error: parameter 1 ('a') has incomplete type
flexarray.c:31:20: error: parameter 1 ('a') has incomplete type
make[2]: *** [build/Debug/MinGW_Actual-Windows/flexarray.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 1s)

3 个答案:

答案 0 :(得分:4)

void flexarray_add(flexarray a, char item){
    if(a->size == a->capacity) grow(a);
    a->array[a->size - 1] = item;
    a->size++;    
}

应该是:

void flexarray_add(flexarray *a, char item){
    if(a->size == a->capacity) grow(a);
    a->array[a->size] = item;
    a->size++;    
}

void flexarray_print(flexarray a){
    int i;
    printf("[");
    for(i = 0; i < a->size - 1; i++){
        printf("%c,", a->array[i]);
    }
    printf("%c]\n", a->array[i + 1]);
}

可能是:

void flexarray_print(flexarray *a){
    int i;
    printf("[");
    for(i = 0; i < a->size; i++){
        printf("%c,", a->array[i]);
    }
    printf("]\n" );
}

其他样式提示:对于大小和索引,请尝试尽可能多地使用 unsigned 类型。

struct buff {
     size_t size;
     size_t used;
     char *data;
     };

这样做的好处是尝试使用负指数(b->data[b->used - 1] = item;)会失败并且速度快(而不是慢慢破坏你的记忆)。

另外:你应该测试realloc()

的成功/失败

UPDATE2:似乎还有一些星号缺失。

答案 1 :(得分:1)

flexarray *flexarray_new(){
    flexarray a;
    return a; // not a flexarray *
}

..和

a->array = (char*) erealloc (a ,a->capacity * sizeof(char)); 

'erealloc()'做什么?

答案 2 :(得分:0)

你的typedef错了 - 你要么想写

typedef struct flexarray { ... } flexarray;

并从标题中删除typedef,或

 struct flexarrayrec { ... }; // no typedef

并将tyepdef struct flexarrayrec flexarray;保留在标题中。