简体   繁体   English

返回一个指向指针数组的指针

[英]return a pointer to a pointer array

I am having trouble freeing a pointer in a pointer array (values). 我在释放指针数组(值)中的指针时遇到麻烦。

typedef struct MyStruct{  char** values;  }MyStruct;

In C, I create dynamic array. 在C中,我创建了动态数组。

JSDictionary **array = (JSDictionary **)malloc(sizeof(JSDictionary *) * numRows);

The resultSet should be an array of JSDictionary pointers. resultSet应该是JSDictionary指针的数组。 I create the struct like: 我创建如下的结构:

JSDictionary * newJSDictionaryWithSize(unsigned int size)

{
JSDictionary *new = malloc(sizeof(JSDictionary));
    printf("new %p\n", self);
new->_size = size;
new->count = 0;

new->values = (char **) malloc(sizeof(char *) * size);
for(unsigned int i = 0; i < size; i++){
    new->values[i] = (char *)malloc(sizeof(char *));
}

return new;
}

Everything creates and works fine. 一切都会创建并正常运行。 It's the free that gives me a problem. 免费是给我一个问题。

void deallocJSDictionary(struct JSDictionary *self)

{
printf("dealloc %p\n", self);
for(unsigned int i = 0; i < self->_size; i++){
    printf("free %p\n", &self->values[i]);
    free(self->values[i]);
}
free(self->values);
free(self);

} }

I get a pointer being freed was not allocated error. 我得到一个被释放的指针未分配错误。 The pointer being passed in shows the same memory address as the one that I created and added to the array. 传入的指针显示的内存地址与我创建并添加到数组的指针相同。 The values pointer (in debugger) shows the same memory address in the dealloc function as it did when I created it. 值指针(在调试器中)在dealloc函数中显示的内存地址与创建时相同。 The problem is trying to free the first point in the values array. 问题是尝试释放values数组中的第一点。 Any ideas why the first point in the values pointer array is different? 有什么想法为什么值指针数组中的第一点不同?

I see two problems so far. 到目前为止,我看到两个问题。 First: 第一:

new->values[i] = (char *)malloc(sizeof(char *));

This line allocates a single pointer - probably not what you want. 该行分配一个指针 -可能不是您想要的。 Should likely be something like: 应该可能是这样的:

new->values[i] = malloc( MAX_SIZE_OF_VALUE );

Ie a buffer to hold the value. 即一个缓冲区来保存该值。 You can also allocate one big chuck to keep all values and just offset into that memory. 您还可以分配一个大卡盘以保留所有值,并仅偏移到该内存中。

Second: 第二:

printf("free %p\n", &self->values[i]);
free(self->values[i]);

This prints and tries to free(3) two different pointers. 这将打印并尝试free(3)两个不同的指针。 First is the address of the second. 第一个是第二个的地址。

As for the actual deallocation error - do you have assignments like this in the code that manages the dictionary? 至于实际的释放错误-在管理字典的代码中是否有这样的分配?

dict->values[i] = some_value;

If so - you are overriding pointers to (and leaking) allocated memory with pointers to probably stack or static memory. 如果是这样,您将使用指向可能是堆栈或静态内存的指针来覆盖指向(并泄漏)已分配内存的指针。 You have to copy the the value into the "value-buffer". 您必须复制到“值缓冲区”中。 Something like: 就像是:

strlcpy( dict->values[i], some_value, MAX_SIZE_OF_VALUE );

Hope this helps. 希望这可以帮助。

The problem is likely in your memory allocation code. 问题可能出在您的内存分配代码中。 The following line 下一行

new->values = (char **) malloc(sizeof(char *) * size);

allocates an array of char pointers, containing size elements. 分配一个char指针数组,其中包含size元素。 Therefore, this is unnecessary: 因此,这是不必要的:

for(unsigned int i = 0; i < size; i++){
    new->values[i] = (char *)malloc(sizeof(char *));
}

What you are doing here is storing a char ** cast to a char * in each element of the new->values array. 您在这里所做的是在new->values数组的每个元素中存储一个转换为char *char ** That is, you are allocating enough memory to store a char * value and then storing a pointer to that memory in the new->values array. 也就是说,您正在分配足够的内存来存储char *值,然后在new->values数组中存储指向该内存的指针。 I'm guessing you did this in error, because 我猜你做错了,因为

new->values = (char **) malloc(sizeof(char *) * size);

has already allocated enough memory for each element of the new->values array. 已经为new->values数组的每个元素分配了足够的内存。

What I'm guessing you want is: 我猜你想要的是:

for(unsigned int i = 0; i < size; i++){
    new->values[i] = (char *)malloc(sizeof(char) * MAX_STRING_LENTH);
}

to create a char buffer of MAX_STRING_LENGTH elements for each element of the new->values array. new->values数组的每个元素创建一个MAX_STRING_LENGTH元素的char缓冲区。 This allows you to store a string in each element, and your free ing code should work fine. 这使您可以在每个元素中存储一个字符串,并且free代码应该可以正常工作。

Alternatively, if the elements of new->values are set somewhere else, then you probably don't want to be free ing them in your deconstructor, unless they were allocated dynamically and you know no other references to them exist at that point. 或者,如果将new->values的元素设置在其他位置,则您可能不希望在解构new->valuesfree它们,除非它们是动态分配的,并且您知道此时不存在对其的其他引用。

There's nothing inherently wrong with your functions. 您的功能本质上没有错。 They compile fine, albeit after a few syntax errors and they produce output as expected. 尽管出现了一些语法错误,但它们可以很好地编译,并且产生预期的输出。 So as others have pointed out, you're possibly assigning to the allocated pointers, data that wasn't allocated (ie stack data) or you're passing in JSDictionary structures that weren't properly allocated in the first place via newJSDictionaryWithSize . 因此,正如其他人指出的那样,您可能正在将未分配的数据(即堆栈数据)分配给已分配的指针,或者您正在传递的JSDictionary结构最初没有通过newJSDictionaryWithSize正确分配。

As a quick test, do an alloc/dealloc on just one JSDictionary to verify that your functions work. 作为一项快速测试,请仅对一个JSDictionary执行alloc / dealloc,以验证您的函数正常工作。

JSDictionary *ptr = newJSDictionaryWithSize(10);
deallocJSDictionary(ptr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM