简体   繁体   English

使用C中的线程进行分段错误

[英]Segmentation fault using threads in C

I'm getting a seg fault within this code, but I can't find the problem anywhere. 我在这段代码中遇到段错误,但在任何地方都找不到问题。 It compiles just fine with -lpthread, but it just won't run. 它可以使用-lpthread进行编译,但是不会运行。 This program takes in an integer from the command line and then creates a new thread to calculate the collatz conjecture using that value. 该程序从命令行获取一个整数,然后创建一个新线程以使用该值计算collat​​z猜想。 This is my code: 这是我的代码:

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

void print_con();
void calc_con(int *n);

int * values[1000];

int main(int argc, char * argv[])
{
        int* num;
        *num = 15;
        pthread_t thread;
        pthread_create(&thread,(pthread_attr_t*)NULL, (void *)&calc_con, (void *)num);
        pthread_join(thread, NULL);
        print_con();
        return 0;

} }

void calc_con(int *n)
{
        int i = 0;
        int * x;
        *x = *n;
        *values[0] = *x;
        while(*x > 1)
        {
                if(*x % 2 == 0)
                        *x /= 2;
                else if(*x % 2 == 1)
                {
                        *x *= 3;
                        *x++;
                }
                i++;
                *values[i] = *x;
        }
        pthread_exit(0);
}

void print_con()
{
        int i;
        for(i = 0; i < 1000; i++)
        {
                if(*values[i] > 0)
                        printf("%d", *values[i]);
        }
}

okay you need to pass a void * as an argument to pthread_create , but you still need to respect the basics: 好的,您需要void *作为参数传递给pthread_create ,但是您仍然需要尊重基本知识:

int* num;
*num = 15;
pthread_t thread;
pthread_create(&thread,(pthread_attr_t*)NULL, (void *)&calc_con, (void *)num);

Here *num = 15; 这里*num = 15; you're writing 15 to an uninitialized pointer . 您正在将15写入未初始化的指针 That's undefined behaviour . 那是未定义的行为

I would do: 我会做:

int num = 15;
pthread_t thread;
pthread_create(&thread,(pthread_attr_t*)NULL, &calc_con, &num);

note that you don't have to cast to void * from pointers on non-void. 请注意,您不必从非void的指针强制转换为void * Since num is declared in the main routine, you can pass a pointer on it to your threads safely. 由于num是在main例程中声明的,因此您可以将指针安全地传递给线程。

note that as pointed by dasblinkenlight, you also have to fix the recieving end, in calc_con , which has the same issue: 请注意,正如dasblinkenlight指出的那样,您还必须在calc_con修复接收端,它具有相同的问题:

int * x;  // uninitialized pointer
*x = *n;  // copy data "in the woods"

just dereference into a local variable and you have your value: 只需取消引用到局部变量,您就可以拥有自己的价值:

int x = *((int *)n);

and another one: 还有一个:

int * values[1000];

is an uninitialized array of integer pointers, not an array of integers like you're intending. 是未初始化的整数指针数组,而不是您想要的整数数组。 It should be 它应该是

int values[1000];

then 然后

values[0] = x;

(it's not because there's a lot of * operators that it's good code) (并不是因为有很多*运算符才是好代码)

You are passing an int to your thread using a void* . 您正在使用void*int传递给线程。 This will work on many platforms, but there is no guarantees that the number would "round-trip" correctly. 这将在许多平台上运行,但是不能保证该数字将正确“往返”。 Once you get pointer back, you save it in a dereferenced uninitialized pointer, which is incorrect. 返回指针后,将其保存在取消引用的未初始化指针中,这是不正确的。

Pass a pointer to num instead, and copy the pointer into x directly: 将指针传递给num ,然后将指针直接复制到x

void calc_con(void *n);
...
void calc_con(void *n) {
        int i = 0;
        int * x = n;
        *values[0] = *x;
        while(*x > 1) {
                if(*x % 2 == 0) {
                        *x /= 2;
                } else if(*x % 2 == 1) {
                        *x *= 3;
                        *x++;
                }
                i++;
                *values[i] = *x;
        }
        pthread_exit(0);
}
...
int num = 15;
pthread_create(&thread,(pthread_attr_t*)NULL, calc_con, (void *)&num);

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

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