通过引用传递时动态结构数组调整大小
dynamic struct array resizing when passed by reference
我不明白是什么导致了段错误在以下第一轮之后:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct query_param {
char *key;
char *val;
};
void extract_params(struct query_param **query_params, char *query_string,
size_t *query_params_len) {
char *token, *key;
while (query_string != NULL) {
token = strsep(&query_string, "&");
key = strsep(&token, "=");
*query_params = realloc(*query_params, (*query_params_len + 1) *
sizeof(struct query_param));
query_params[*query_params_len]->key = malloc(strlen(key));
query_params[*query_params_len]->val = malloc(strlen(token));
memcpy(query_params[*query_params_len]->key, key, strlen(key));
memcpy(query_params[*query_params_len]->val, token, strlen(token));
(*query_params_len)++;
}
}
int main(int argc, char **argv) {
char *query_string = "foo=bar&baz=boo&zip=zap";
size_t query_params_len = 0;
struct query_param *query_params = NULL;
extract_params(&query_params, query_string, &query_params_len);
return 0;
}
将第一个键值对添加到结构中工作正常,但 第二个 malloc 导致 麻烦。
valgrind 信息:
==15319== Memcheck, a memory error detector
==15319== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==15319== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==15319== Command: ./a.out
==15319==
==15319==
==15319== Process terminating with default action of signal 11 (SIGSEGV)
==15319== Bad permissions for mapped region at address 0x4008DF
==15319== at 0x4EC1A0B: strsep (in /lib64/libc-2.23.so)
==15319== by 0x4006C0: extract_params (foo.c:15)
==15319== by 0x400848: main (foo.c:36)
==15319==
==15319== HEAP SUMMARY:
==15319== in use at exit: 0 bytes in 0 blocks
==15319== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==15319==
==15319== All heap blocks were freed -- no leaks are possible
==15319==
==15319== For counts of detected and suppressed errors, rerun with: -v
==15319== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
在 运行 valgrind 之后,仍然没有解决这个问题。特别是堆摘要对我来说没有任何意义。 运行 malloc 3次后怎么分配0字节?
干杯!
query_params[*query_params_len]->blah
query_params
不是 指针数组 ,也不是指向 指针数组 的第一个元素的指针.它是指向 结构数组 的第一个元素的指针。你想要这个
(*query_params)[*query_params_len].blah
除@n.m 中指出的问题外,还有几个问题。答案(query_params
是 "a pointer to a pointer to the first element of an array of structures.")。
行
char *query_string = "foo=bar&baz=boo&zip=zap";
将query_string
声明为指向const字符串字面量的指针,但后来由于使用strsep()
,程序不得不修改指向的内存。您应该声明一个数组作为常量字符串的副本:
char query_string[] = "foo=bar&baz=boo&zip=zap";
此外,当您尝试复制令牌时,您应该遵循@John Bollinger 的建议并使用 strdup()
或至少考虑 '[=16=]'
终止符。
此外,您应该检查所有使用的库函数的 return 值,并在最后释放分配的内存。
我不明白是什么导致了段错误在以下第一轮之后:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct query_param {
char *key;
char *val;
};
void extract_params(struct query_param **query_params, char *query_string,
size_t *query_params_len) {
char *token, *key;
while (query_string != NULL) {
token = strsep(&query_string, "&");
key = strsep(&token, "=");
*query_params = realloc(*query_params, (*query_params_len + 1) *
sizeof(struct query_param));
query_params[*query_params_len]->key = malloc(strlen(key));
query_params[*query_params_len]->val = malloc(strlen(token));
memcpy(query_params[*query_params_len]->key, key, strlen(key));
memcpy(query_params[*query_params_len]->val, token, strlen(token));
(*query_params_len)++;
}
}
int main(int argc, char **argv) {
char *query_string = "foo=bar&baz=boo&zip=zap";
size_t query_params_len = 0;
struct query_param *query_params = NULL;
extract_params(&query_params, query_string, &query_params_len);
return 0;
}
将第一个键值对添加到结构中工作正常,但 第二个 malloc 导致 麻烦。
valgrind 信息:
==15319== Memcheck, a memory error detector
==15319== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==15319== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==15319== Command: ./a.out
==15319==
==15319==
==15319== Process terminating with default action of signal 11 (SIGSEGV)
==15319== Bad permissions for mapped region at address 0x4008DF
==15319== at 0x4EC1A0B: strsep (in /lib64/libc-2.23.so)
==15319== by 0x4006C0: extract_params (foo.c:15)
==15319== by 0x400848: main (foo.c:36)
==15319==
==15319== HEAP SUMMARY:
==15319== in use at exit: 0 bytes in 0 blocks
==15319== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==15319==
==15319== All heap blocks were freed -- no leaks are possible
==15319==
==15319== For counts of detected and suppressed errors, rerun with: -v
==15319== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
在 运行 valgrind 之后,仍然没有解决这个问题。特别是堆摘要对我来说没有任何意义。 运行 malloc 3次后怎么分配0字节?
干杯!
query_params[*query_params_len]->blah
query_params
不是 指针数组 ,也不是指向 指针数组 的第一个元素的指针.它是指向 结构数组 的第一个元素的指针。你想要这个
(*query_params)[*query_params_len].blah
除@n.m 中指出的问题外,还有几个问题。答案(query_params
是 "a pointer to a pointer to the first element of an array of structures.")。
行
char *query_string = "foo=bar&baz=boo&zip=zap";
将query_string
声明为指向const字符串字面量的指针,但后来由于使用strsep()
,程序不得不修改指向的内存。您应该声明一个数组作为常量字符串的副本:
char query_string[] = "foo=bar&baz=boo&zip=zap";
此外,当您尝试复制令牌时,您应该遵循@John Bollinger 的建议并使用 strdup()
或至少考虑 '[=16=]'
终止符。
此外,您应该检查所有使用的库函数的 return 值,并在最后释放分配的内存。