我给一个int指针分配一个从sscanf
中提取的值。然后我想把它传递给另一个文件counters_add
中的方法。尽管我可以打印出存储在指针中的值及其地址,但只要我将其传递给此方法,程序就会抛出seg错误。从测试中我知道程序在seg错误发生之前甚至没有进入这个方法。
该方法采用(counters_t *ctrs, const int key)
的参数。counters_t
对象是我先前在文件中定义的结构。
我没有过早地释放任何东西,并已验证ctrs
和key
都不是NULL
。为什么我会有分割错误?
int *key = malloc(sizeof(int));
//check if key is null here
sscanf(line, "%i", key);
printf("key: %i\n", *key); //this prints out the value
printf("key: %p\n", (void *)key); //this prints out the address
counters_add(ctrs, *key);//seg fault here, without even getting inside of method
ctrs初始化:
counters_t *ctrs = count_malloc(sizeof(counters_t));
if (ctrs == NULL) {
return NULL; // error allocating set
} else {
// initialize contents of set structure
ctrs->head = NULL;
}
其余代码:
void
counters_add(counters_t *ctrs, const int key)
{
if (key >= 0 && ctrs != NULL) {
// allocate a new node to be added to the list if the key is not already in the set
if(counters_get(ctrs,key) == 0) {//if it doesnt already exist
printf("after first if statement");
counternode_t *new = counternode_new(&key);//create it
printf("aftermaking new node");
new->next = ctrs->head;//add it to the head of the list
ctrs->head = new;
} else {
// increment the count
for(counternode_t *curr = ctrs->head; curr != NULL; curr = curr->next){
if (*(curr->key) == key){
*(curr->count) = *(curr->count) + 1;
}
}
}
}
}
int
counters_get(counters_t *ctrs, const int key)
{
printf("in counters_get");
if (ctrs == NULL) {
return 0; // null counter
} else if (ctrs->head == NULL) {
return 0; // set is empty
}//remove this in set
else {
for(counternode_t *curr = ctrs->head; curr != NULL; curr = curr->next)
{
if (*(curr->key) == key)
return *(curr->count);
printf("in loop");
}
return 0;
}
}
static counternode_t // not visible outside this file
*counternode_new(const int *key)
{
counternode_t *node = count_malloc(sizeof(counternode_t));
int *newkey = (int*)malloc(sizeof(int));
newkey = (int*) memcpy(newkey, key, (50 * sizeof(char)));
//make sure key is not over 50 ints
if (node == NULL || newkey == NULL) {
// error allocating memory for node or new key; return error
return NULL;
} else {
node->key = newkey;
*(node->count) = 1;
node->next = NULL;
return node;
}
}
以下是计数器结构:
typedef struct counters {
struct counternode *head; // head of the list of items in set
} counters_t;
这里是counternode:
typedef struct counternode {
int *key;
int *count; //pointer to counter for this node
struct counternode *next; // link to next node
} counternode_t;
最佳答案
我在counternode_new
中看到了问题:
static counternode_t // not visible outside this file
*counternode_new(const int *key)
{
counternode_t *node = count_malloc(sizeof(counternode_t));
int *newkey = (int*)malloc(sizeof(int));
newkey = (int*) memcpy(newkey, key, (50 * sizeof(char)));
...
}
在
counters_add
中,向counternode_new
传递指向变量d
的指针。然后在
counternode_new
中,您想用newkey
作为源头。但是
key
是指向单个整数的指针,因此您将读取49字节超出界限,这会导致未定义的行为,这可能会导致
塞格法特。另外,您只为
key
的单个int
分配空间。另外,你复制的是50个字节,而不是50个整数。我不知道50是从哪里来的。所以你的呼叫毫无意义,首先你必须
为
newkey
数组分配空间并将其传递给counternode_new
。关于c - Int指针不为null,可以打印但不能在不引发段错误的情况下传递给函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48454839/