C Int指针不为null,可以打印,但如果不抛出分段错误,则无法将其传递到函数中
我正在为int指针分配一个从C Int指针不为null,可以打印,但如果不抛出分段错误,则无法将其传递到函数中,c,pointers,segmentation-fault,C,Pointers,Segmentation Fault,我正在为int指针分配一个从sscanf提取的值。然后我想将它从另一个文件传递到一个方法,counters\u add。虽然我可以打印出指针中存储的值及其地址,但只要我将其传递给此方法,程序就会抛出seg错误。通过测试,我知道程序在seg故障之前甚至没有进入这个方法 此方法采用(计数器\u t*ctrs,常量int键)的参数。counters\t对象是我前面在文件中定义的结构 我不会过早释放任何内容,并且已验证ctrs和key都不是NULL。为什么我会出现分割错误 int *key = mall
sscanf
提取的值。然后我想将它从另一个文件传递到一个方法,counters\u add
。虽然我可以打印出指针中存储的值及其地址,但只要我将其传递给此方法,程序就会抛出seg错误。通过测试,我知道程序在seg故障之前甚至没有进入这个方法
此方法采用(计数器\u t*ctrs,常量int键)
的参数。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
CTR的初始化:
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;
以下是节点:
typedef struct counternode {
int *key;
int *count; //pointer to counter for this node
struct counternode *next; // link to next node
} counternode_t;
我注意到
newkey
只分配了sizeof(int)
,而您似乎将50个字节复制到newkey
int *newkey = (int*)malloc(sizeof(int));
newkey = (int*) memcpy(newkey, key, (50 * sizeof(char)));
我注意到
newkey
只分配了sizeof(int)
,而您似乎将50个字节复制到newkey
int *newkey = (int*)malloc(sizeof(int));
newkey = (int*) memcpy(newkey, key, (50 * sizeof(char)));
我在
counternode\u 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\u add
中,将指向变量d
的指针传递给counternode\u new
。
然后在counternode\u new
中,您要将key
作为
消息来源。但是键
是指向一个单个整数的指针,所以您读取的是49
字节越界,这将导致未定义的行为,这可能导致
赛格断层。此外,您仅为newkey
的单个int
分配空间。此外,您正在复制50个字节,而不是50个整数。我不明白50是从哪里来的
因此,您的counternode\u new
调用counters\u add
毫无意义,首先您必须
为
int[50]
数组分配空间并将其传递给counternode\u new
我在counternode\u 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\u add
中,将指向变量d
的指针传递给counternode\u new
。
然后在counternode\u new
中,您要将key
作为
消息来源。但是键
是指向一个单个整数的指针,所以您读取的是49
字节越界,这将导致未定义的行为,这可能导致
赛格断层。此外,您仅为newkey
的单个int
分配空间。此外,您正在复制50个字节,而不是50个整数。我不明白50是从哪里来的
因此,您的counternode\u new
调用counters\u add
毫无意义,首先您必须
为
int[50]
数组分配空间,并将其传递给counternode\u new
问题很可能在于ctrs
。您是如何申报CTR的?你初始化了吗?请创建一个。计数器是否有原型?“我在以前的代码中声明了CTR,并为它声明了malloc内存。我还检查了它是否为空。”--不要告诉我们,让我们看看。我们应该能够从问题中复制和粘贴您的代码,并自己运行它来重现问题。不要只添加多个断开连接的代码块。从显示问题的程序开始,尽可能缩小范围,使其仍然显示相同的问题。你应该能写下来,比如说,20行左右。然后将完整的程序作为单个代码块发布。正如我所说,我们应该能够复制和粘贴您的整个程序,并自己复制问题。就我个人而言,我不愿意复制和粘贴当前问题中的7段代码,并找出如何将它们重新组合在一起。问题很可能在于ctrs
。您是如何申报CTR的?你初始化了吗?请创建一个。计数器是否有原型?“我在以前的代码中声明了CTR,并为它声明了malloc内存。我还检查了它是否为空。”--不要告诉我们,让我们看看。我们应该能够从问题中复制和粘贴您的代码,并自己运行它来重现问题。不要只添加多个断开连接的代码块。从显示问题的程序开始,尽可能缩小范围,使其仍然显示相同的问题。你应该能写下来,比如说,20行左右。然后将完整的程序作为单个代码块发布。正如我所说,我们应该能够复制和粘贴您的整个程序,并自己复制问题。就我个人而言,我不愿意复制和粘贴当前问题中的7段代码中的每一段,并找出如何将它们重新组合在一起。这似乎是真的,这是一个很大的错误,所以我拿出了50,只留了一个int的空间。然而,这并不能解释为什么counters\u add根本没有被调用。@grasss我不知道,至少从您发布的代码部分来看,应该是这样的。请阅读。这似乎是真的,而且犯了一个很大的错误,所以我去掉了50,只留了一个整数的空间。然而,这并不能解释为什么没有调用counters_add。@Grasss我不知道,至少从您发布的代码部分来看,应该是这样的。请阅读。