简体   繁体   English

PThread Malloc上的分段错误

[英]Segmentation Fault on PThread Malloc

First let me start by posting the code: 首先,让我开始发布代码:

#include <pthread.h>
#include<stdlib.h>

//64 BITS -ALL 32 bit Arch
//32 BIT - UNIX 64 bit arch
//64 BIT - WINDOWS 64 bit arch
long long sum = 0;
static enum turn
{
  PING,
  PONG
}def;

struct threads_util
{
  pthread_t *t_id;
  pthread_attr_t *attr;
  void (*init_attr)(pthread_attr_t *);
}; 

void init_attr_fn(pthread_attr_t *attr)
{
  pthread_attr_init(&attr);
}

void* sum_runner(void* arg)
{
    long long* limit_ptr = (long long *) arg;
    long long limit = *limit_ptr;//Derefrencing
    for(long long i=0; i<=limit; i++)
        sum += i;
    printf("Sum is %lld \n", sum);
    pthread_exit(0);
}

void ping()
{
  puts("Ping");
  def = PONG;
}

void pong()
{
  puts("Pong");
  def = PING;
}

pthread_t * all_thread(pthread_t *t_id)
{
  t_id = malloc(sizeof(pthread_t));
  return t_id;
}

int main(int argc, char **argv)
{
    if(argc<2)
    {
    puts("Usage ./objFile <num 1> <num 2> .. <num n>"); 
        exit(1);
    }

    int args = argc-1;
    long long limit = atoll(argv[1]);
    def = PING;

   struct threads_util *threads[args];

   for (int i=0; i<args; i++)
    threads[i]->t_id = all_thread(threads[i]->t_id);
    puts("In");    
   for(int i=0; i<args; i++)
   {
      threads[i]->init_attr = init_attr_fn;

      threads[i]->init_attr(threads[i]->attr);
      pthread_create(threads[i]->t_id,threads[i]->attr,sum_runner,&limit);
   }

    //Begin -Main Functions
    for(int i=0; i<= 10; i++)
    {
    if(def == PING)
           ping();
        else if(def == PONG)
           pong();
        else
           puts("UNKOWN PI-PO");           
    }
    //End - Main Functions

    for(int i=0; i<args; i++)
    {
      pthread_join(threads[i]->t_id,NULL);
    }
}

You can see i have a puts("In"), in the main function, just after the for loop when i call all_thread args times. 您可以看到在我调用all_thread args次的for循环之后,在主函数中有一个puts(“ In”)。 Well calling the function argc times with the for loop according to my debugging skills, is the problem. 根据我的调试技巧,用for循环很好地调用argc times函数是问题。 And also before we did all the allocation strategy, I had a problem on calling the thread function, of course resulting in a Segmentation Fault. 而且在执行所有分配策略之前,我在调用线程函数时遇到了问题,当然会导致分段错误。 threads[i]->init_attr(threads[i]->attr); . Help would be very much appreciated. 帮助将不胜感激。

struct threads_util *threads[args];

means that you define an array of pointers to struct threads_util . 表示您定义了一个指向 struct threads_util指针的数组。 But you don't actually create any struct threads_util so this line: 但是您实际上并没有创建任何struct threads_util因此此行:

threads[i]->t_id = all_thread(threads[i]->t_id);

is illegal as it writes to memory not allocated. 由于它写入未分配的内存,因此是非法的。

You need to allocate memory for struct threads_util first: 您需要首先为struct threads_util分配内存:

for (int i=0; i<args; i++)
    threads[i] = malloc(sizeof(struct threads_util));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM