gpt4 book ai didi

c - 结构与 ptr 数组嵌套结构分配和内存分配

转载 作者:太空宇宙 更新时间:2023-11-04 03:24:08 25 4
gpt4 key购买 nike

我有一个结构,其中包含指向也包含指针的相关结构的深度嵌套。我在正确初始化时遇到问题,因为 ptrs 数组必须将大小传递给 init 函数。见下面的代码:

类型定义:

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
};

初始化函数:

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;
}

字典操作:

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 */
}

最佳答案

Dict *initDict(const unsigned int size) {
Dict data = { /*...*/ };
// ...
Dict *d = (Dict*) malloc(sizeof(Dict));
d = &data;
return d;
}

这里存在两个明显的问题:

  1. d = &data; 会立即泄漏您使用 malloc 分配的内存。
  2. 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!

关于c - 结构与 ptr 数组嵌套结构分配和内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42633255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com