尝试将struct传递给函数时保持segfaulting [英] Keep segfaulting when trying to pass struct into function

查看:122
本文介绍了尝试将struct传递给函数时保持segfaulting的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  void createQueue(struct pqueue * queue)

我试图将一个指向队列的指针传递给createQueue函数: ){
queue = malloc(sizeof(struct pqueue));
queue-> root = malloc(sizeof(struct node));
queue-> root-> next = 0;
queue-> root-> taskID = 12;
queue-> root-> priority = 5000;
}

我也尝试添加新创建的队列,如下所示:

  void add(struct pqueue * queue,int taskID,int priority){
struct node * conductor;
conductor = queue-> root;
if(conductor!= 0){
while(conductor-> next!= 0)
{
conductor = conductor-> next;
}
}
conductor-> next = malloc(sizeof(struct node));
conductor = conductor->下;
if(conductor == 0)
{
printf(Out of memory);
}
/ *初始化新内存* /
conductor-> next = 0;
conductor-> taskID = taskID;
conductor-> priority = priority;

来自主要功能:

  int main()
{
struct pqueue * queue;

createQueue(queue);
add(队列,234093,9332);
}

...但我继续保留段落。任何理由为什么这种情况继续发生?



编辑:



pqueue和node的结构如下所示:

  struct node {
int taskID;
int优先级;
struct node * next;
};

struct pqueue {
struct node * root;
};


解决方案

在C中,所有东西都是按值传递的。因此,当您调用 createQueue(queue)时,您正在将指针的副本传递给该函数。然后,在函数内部,当您说 queue = malloc(...)时,您将指针的 copy 设置为等于您的新值分配内存 - 保持该指针的 main()的副本不变。



你想做这样的事情:

  void createQueue(struct pqueue ** queue)
{
(* queue)= malloc( ...);
}

int main(void)
{
struct pqueue * queue;

createQueue(& queue);
}

这个问题更详细地描述了你的错误。


I'm trying to pass a pointer to a queue into the createQueue function:

void createQueue(struct pqueue *queue){
    queue = malloc( sizeof(struct pqueue) );  
    queue->root = malloc(sizeof(struct node));
    queue->root->next = 0;   
    queue->root->taskID = 12;
    queue->root->priority = 5000;
}

I also try to add to the newly created queue like this:

void add(struct pqueue *queue, int taskID, int priority){
struct node *conductor;
conductor = queue->root;
if ( conductor != 0 ) {
        while ( conductor->next != 0)
        {
                conductor = conductor->next;
        }
}
 conductor->next = malloc( sizeof(struct node) );  
 conductor = conductor->next;
 if ( conductor == 0 )
  {
      printf( "Out of memory" );
  }
  /* initialize the new memory */
  conductor->next = 0;         
  conductor->taskID = taskID;
  conductor->priority = priority;
}

from the main function:

int main()
{
    struct pqueue *queue;       

    createQueue(queue);
    add(queue, 234093, 9332);
}

...but I keep segfaulting. Any reason why this keeps happening?

EDIT:

The structs for pqueue and node are like this:

struct node {
  int taskID;
  int priority;
  struct node *next;
};

struct pqueue{
  struct node *root;
};

解决方案

In C, everything is passed by value. Therefore, when you call createQueue(queue), you are passing a copy of the pointer to the function. Then, inside the function, when you say queue = malloc(...), you are setting that copy of the pointer equal to your newly allocated memory - leaving main()'s copy of that pointer unchanged.

You want to do something like this:

void createQueue(struct pqueue **queue)
{
    (*queue) = malloc( ... );
}

int main(void)
{
    struct pqueue *queue;

    createQueue(&queue);
}

This question has a more detailed description of what's going wrong for you.

这篇关于尝试将struct传递给函数时保持segfaulting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆