I have the following two structures.
我有下面两个结构。
struct string_counter {
char *string;
int count;
};
struct container{
struct string_counter *counters;
int size;
};
And the following functions.
和下面的函数。
void add_string(struct *container, char *string){
struct string_counter *counter_ptr;
counter_ptr = new_count(string);
container->size++;
container->counters = realloc(container->counters, sizeof(struct string_counter)*container->size);
container->counters[container->size-1] = *counter_ptr;
print_string_counter(&container->counters[container->size-1]);
}
struct counter *new_count(char *string){
struct string_counter *counter_ptr;
counter_ptr = malloc(sizeof(struct string_counter));
counter_ptr->string = string;
counter_ptr->count = 1;
return counter_ptr;
}
In my main function I am looping over several times callingadd_string(container_ptr, string)
with the same container pointer and a different string each time, attempting to add the new string to the end of the string_counter *counters
. Later on in my code when I try printing the contents of the counters using the following two functions:
在我的main函数中,我将多次使用相同的容器指针和一个不同的字符串来循环多次callingadd_string(container_ptr, string),试图将新字符串添加到string_counter *计数器的末尾。稍后在我的代码中,当我尝试使用以下两个函数打印计数器的内容时:
void print_container(struct container *container_ptr){
int i;
for(i = 0; i < container_ptr->size; i++){
print_string_counter(&container_ptr->counters[i]);
}
}
void print_string_counter(struct string_counter *counter_ptr){
printf("%s : %d\n", counter_ptr->string, counter_ptr->count);
}
I get extremely incorrect results such as my output below. But earlier when I printed with :
我得到了非常不正确的结果,如下面的输出。但是在我打印的时候:
print_string_counter(&container->counters[container->size-1])
just below :
下面:
container->counters[container->size-1] = *counter_ptr;
in my add_string()
function, I got the correct results.
在add_string()函数中,我得到了正确的结果。
Initially my container values are
最初我的容器值是。
struct_counter = NULL;
int size = 0;
Why is my output using the loop later on different from originally when I printed it?
为什么在我打印的时候,我的输出与原来的循环不同?
1 个解决方案
#1
0
The main problem is likely that your string_counter
struct does nat actually store the strings -- it just stores pointers, which will be point at whatever you pass in. In new_count
, if that string you pass in is in a temporary buffer (say the buffer you use to read input in main
), then when that buffer goes away, the pointer in the string_counter
will be left dangling and when you later go to print, you'll get whatever garbage happens to be in memory where your original buffer was.
主要的问题很可能是,您的string_counter结构实际上是nat存储了字符串——它只是存储指针,它将指向您传入的任何东西。new_count,如果你传入的字符串是一个临时缓冲区中(比如缓冲主要用于读取输入),当缓冲区消失,指针的string_counter稍后将左晃来晃去的,当你去打印,你会得到任何垃圾恰好是在内存中原始缓冲区在哪里。
Related (but indpendent) your container
struct maintains an array of string_counter
objects, not pointers, so when you call add_string
, it extends the array AND calls new_count
to allocate a new string_counter
. It then copies that newly allocated object and leaks it.
Related(但indpendent)您的容器struct维护一个string_counter对象数组,而不是指针,所以当您调用add_string时,它扩展了数组,并调用new_count来分配一个新的string_counter。然后复制新分配的对象并将其泄漏。
So what you likely need is in new_counter
to copy the string as well. You can also fix the second problem by having new_count
return an object rather than a pointer:
因此,您可能需要在new_counter中复制该字符串。您还可以通过让new_count返回一个对象而不是一个指针来修复第二个问题:
struct counter new_count(char *string){
struct string_counter counter;
counter.string = strdup(string);
counter.count = 1;
return counter;
}
#1
0
The main problem is likely that your string_counter
struct does nat actually store the strings -- it just stores pointers, which will be point at whatever you pass in. In new_count
, if that string you pass in is in a temporary buffer (say the buffer you use to read input in main
), then when that buffer goes away, the pointer in the string_counter
will be left dangling and when you later go to print, you'll get whatever garbage happens to be in memory where your original buffer was.
主要的问题很可能是,您的string_counter结构实际上是nat存储了字符串——它只是存储指针,它将指向您传入的任何东西。new_count,如果你传入的字符串是一个临时缓冲区中(比如缓冲主要用于读取输入),当缓冲区消失,指针的string_counter稍后将左晃来晃去的,当你去打印,你会得到任何垃圾恰好是在内存中原始缓冲区在哪里。
Related (but indpendent) your container
struct maintains an array of string_counter
objects, not pointers, so when you call add_string
, it extends the array AND calls new_count
to allocate a new string_counter
. It then copies that newly allocated object and leaks it.
Related(但indpendent)您的容器struct维护一个string_counter对象数组,而不是指针,所以当您调用add_string时,它扩展了数组,并调用new_count来分配一个新的string_counter。然后复制新分配的对象并将其泄漏。
So what you likely need is in new_counter
to copy the string as well. You can also fix the second problem by having new_count
return an object rather than a pointer:
因此,您可能需要在new_counter中复制该字符串。您还可以通过让new_count返回一个对象而不是一个指针来修复第二个问题:
struct counter new_count(char *string){
struct string_counter counter;
counter.string = strdup(string);
counter.count = 1;
return counter;
}