我有一个包含ptrs深度嵌套的结构到相关的结构,这些结构也包含指针。我在初始化时遇到问题,因为ptrs数组的大小必须传递给init函数。请参阅以下代码:
的typedef:
typedef struct tuple Tuple;
typedef struct dict Dict;
int findKey(Dict *d, char *check);
struct tuple {
char *key; //named loc
void *val; //data
};
struct dict {
unsigned int size; //max size
unsigned int counter; //# tuples
Tuple **entries; //list of tuples
};
init func:
Dict *initDict(const unsigned int size) {
Dict data = {.size = size, .counter = 0, .entries = (Tuple**) malloc(sizeof(Tuple*) * size)};
for (unsigned int i = 0; i < size; i++) {
data.entries[i] = (Tuple*) malloc(sizeof(Tuple));
data.entries[i]->key = '\0'; /* initially null */
data.entries[i]->val = '\0'; /* initially null */
}
Dict *d = (Dict*) malloc(sizeof(Dict));
d = &data;
return d;
}
Dict ops:
short setTuple(Dict *d, char *key, void* val) {
int i;
if ((i = findKey(d, key)) != -1) { /* found key */
d->entries[i]->val = val;
return 0;
}
else { /* new entry */
if (d->counter < d->size) { /* find first null */
for (unsigned int i = 0; i < d->size; i++) {
if (d->entries[i]->key == NULL) break;
} /* then replace null slot */
d->entries[i]->key = (char *) malloc(sizeof(char) * strlen(key));
d->entries[i]->key = key;
d->entries[i]->val = (void *) malloc(sizeof(&val));
d->entries[i]->val = val;
d->counter++;
return 0;
}
return -1; /* no room */
}
}
short setTuples(Dict *d, char *json) {
}
void* getTuple(Dict *d, char *key) {
int i;
if ((i = findKey(d, key)) != -1) {
void *val = d->entries[i]->val;
return val;
} return (void *) -1; /* not found */
}
void* getIndex(Dict *d, unsigned int index) {
if (index < d->counter && d->entries[index]->key != NULL) {
void *val = d->entries[index]->val;
return val;
} return (void *) -1; /* not found */
}
/* TODO: add checks for NULL ptr prior to freeing? */
int removeTuple(Dict *d, char *key) {
int i;
if ((i = findKey(d, key)) != -1) {
free(d->entries[i]->key);
free(d->entries[i]->val);
free(d->entries[i]);
d->entries[i]->key = '\0';
d->entries[i]->val = '\0';
d->counter--;
return 0;
} return -1; /* no room */
}
void destroyDict(Dict *d) {
for (unsigned int i = 0; i < d->counter; i++) {
free(d->entries[i]->key);
free(d->entries[i]->val);
free(d->entries[i]);
d->entries[i]->key = '\0';
d->entries[i]->val = '\0';
d->entries[i] = '\0';
d->counter--;
}
free(d->entries);
free(d);
}
/* return index of tuple in dict or -1 if DNE */
int findKey(Dict* d, char* check) {
unsigned int i;
for (i = 0; i < d->counter; i++) {
if (d->entries[i]->key != NULL && strcmp(d->entries[i]->key, check) == 0) {
return i;
}
} return -1; /* not found */
}
答案 0 :(得分:1)
Dict *initDict(const unsigned int size) {
Dict data = { /*...*/ };
// ...
Dict *d = (Dict*) malloc(sizeof(Dict));
d = &data;
return d;
}
这里存在两个明显的问题:
d = &data;
会立即泄漏您使用malloc
分配的内存。return d;
继续(1),返回局部变量的地址。因此,您的程序行为未定义。如果要使用在局部变量中设置的值复制初始化新分配的内存,则需要取消引用该指针,然后指定指针:
Dict *initDict(const unsigned int size) {
Dict data = { /*...*/ };
// ...
Dict *d = malloc(sizeof(Dict));
if(d)
*d = data;
else {
// Everything you allocated for data must be cleaned here.
}
return d;
}
我还添加了一张支票,只要原始代码使用malloc
,您就会非常遗漏。检查指针是否为NULL!
答案 1 :(得分:0)
只是想发布我的实现来完善这个问题。我实际上使用了一个更动态的方法,使用了2个独立的&#34;块&#34;,其中一个保存表数据,另一个块保存我的ptr数组。以这种方式链接数据允许动态操作和职责分离/冗余,灵活性,更不用说不创建依赖关系来重新分配内存。 @teppic在这里有一个惊人的演练:dynamic ptrs to structs。
编辑:我添加了一些代码清理(错过了几个旧语句)以及完整的表的一些数据隐藏/抽象,现在所有测试都成功,享受!声明:
typedef struct tuple Tuple;
typedef struct dict Dict;
int findKey(Dict *d, char *check);
struct tuple {
char *key; //named loc
void *val; //data
};
struct dict {
unsigned int size; //max size
unsigned int counter; //# tuples
Tuple *table; //table of tuples
Tuple **entries; //arr of ptrs
};
初始化:
Dict *initDict(const unsigned int size) {
Dict *d = (Dict *) malloc(sizeof(Dict));
if (d) {
d->size = size;
d->counter = 0;
d->table = malloc(sizeof(Tuple*) * size);
d->entries = malloc(sizeof(Tuple*) * size);
if (d->table && d->entries) {
for (unsigned int i = 0; i < size; ++i) {
d->entries[i] = d->table + i;
}
return d;
} /* Failed to init: */
else { /* Data must be cleaned here. */
free(d->entries);
free(d->table);
free(d);
} exit(-1); /* fall through */
} exit(-1); /* fail safe */
}
setTuple(FIXED:o)
short setTuple(Dict *d, char *key, void* val) {
int i;
if ((i = findKey(d, key)) != -1) { /* found key */
d->entries[i]->val = val;
return 0;
}
else { /* new entry */
if (d->counter < d->size) { /* find first null */
unsigned int i = 0; /* reset */
for (i; i < d->size; i++) {
/* getting segv on break statement? */
if (! d->entries[i] || ! d->entries[i]->key) break;
} /* then replace null slot */
d->entries[i]->key = key;
d->entries[i]->val = val;
d->counter++;
return 0;
} /* no room left in dict */
} return -1;
}
除了简洁之外还有其他一些......这是内存清理:
int removeTuple(Dict *d, char *key) {
int i;
if ((i = findKey(d, key)) != -1) {
d->entries[i]->key = '\0';
d->entries[i]->val = '\0';
d->counter--;
return 0;
} return -1; /* no room */
}
void destroyDict(Dict *d) {
for (unsigned int i = 0; i < d->counter; i++) {
d->entries[i] = '\0';
d->counter--;
}
free(d->entries);
free(d->table);
free(d);
}
一旦块被释放然后就可以了,链接列表FTW :) 注意在dict中实现的计数器,提供了在执行&#34; get&#34;时检查的参考。元组。除了将字段显式设置为null之外,还可以使搜索速度更快(与#34;黑名单相比;#34;)我们知道的密钥没有我们想要的密钥。