【问题标题】:struct pointers in c doesn't return correct valuec中的结构指针不返回正确的值
【发布时间】:2017-03-29 16:25:09
【问题描述】:

我有这个程序:

#include <stdio.h>
#niclude ......

struct sort {
    char  * array;
    int left,right;
};

void quicksortthread(struct sort *s){
    int left, right;
    int i, j, x, tmp;
    pthread_t *th1, *th2;
    char * v;
    struct sort s1;
    struct sort s2;

    left = s->left;
    right = s->right;

    v = s->array;

    printf("\n\n\n\n QT: l=%i, r=%i \n",left,right);

    //sorting algorithm
    if (left >= right)
        return;
    x = v[left];
    i = left - 1;
    j = right + 1;
    while (i < j) {
        while (v[--j] > x);
        while (v[++i] < x);
        if (i < j) {
            char temp = v[i];
            v[i] = v[j];
            v[j] = temp;
        }
    }

    s1.array = v;
    s1.left = left;
    s1.right = j;
    s2.array = v;
    s2.left = j+1;
    s2.right = right;
    printf("1)left = %i; right= %i\n", left,j);
    printf("2)left = %i; right= %i\n", j+1,right);
    pthread_create(&th1, NULL, quicksortthread,&s1);
    pthread_create(&th2, NULL, quicksortthread,&s2);
}

int main (int argc, char ** argv){

    pthread_t *th1;
    int fd, len, pg, i, j;
    int left, right;
    struct stat stat_buf;
    char c, *paddr;
    struct sort s;

    /*..LOTS OF THIGS

      Define right and left as integers and
      paddr as a char *

      ..*/

    if(   (right)   >=  (atoi(argv[2]))  ){

        //printf("filling structure\n");
        s.array = paddr;
        s.left = left;
        s.right = right;
        //printf("creating threads\n");
        pthread_create(&th1, NULL, quicksortthread,&s);
    }

    sleep(100);
}

在两个线程调用后它停止工作。这是因为函数pthread_create 作为最后一个参数传递的结构似乎不正确。它编译并在没有(例如)分段错误的情况下执行。我确信我以正确的方式使用结构和指针。

那么程序就这样返回了:

left = 0; right= 2048
QT: l=0, r=2048 
1)left = 0; right= 0
2)left = 1; right= 2048    

left = 0; right= 0
QT: l=0, r=0 

left = 1037061890; right= 32542
QT: l=1037061890, r=32542 

已解决:好的,我已经解决了这个问题,以这种方式更改线程例程:

nt left, right;
int i, j, x, tmp;
pthread_t th1, th2;
char * v;
struct sort * s1;
s1= malloc(sizeof(struct sort *));
struct sort * s2;
s2= malloc(sizeof(struct sort *));

left =  s->left;
right = s->right;
printf("\n\n\n\nleft = %i; right= %i\n", left,right);
v =  s->array;
printf("QT: l=%i, r=%i \n",left,right);

if (left >= right)
return;
x = v[left];
i = left - 1;
j = right + 1;
while (i < j) {
while (v[--j] > x);
while (v[++i] < x);
if (i < j) {

   char temp = v[i];
   v[i] = v[j];
   v[j] = temp;
     }
 }

 s1->array = v;
 s1->left = left;
 s1->right = j;
 s2->array = v;
 s2->left = j+1;
 s2->right = right;
 printf("1)left = %i; right= %i\n", left,j);
 printf("2)left = %i; right= %i\n", j+1,right);
 pthread_create(&th1, NULL, quicksortthread,s1);
 pthread_create(&th2, NULL, quicksortthread,s2);

【问题讨论】:

  • C 还是 C++?它们不是同一种语言。
  • 这看起来不对i = left - 1; j = right + 1; while (i &lt; j) {应该是while (i+1 &lt; j-1)吗?
  • pthread_t *th1; ==> pthread_t th1;
  • 对不起!在 C...
  • s1= malloc(sizeof(struct sort *)); 可能有点太小了...

标签: c multithreading pointers pthreads


【解决方案1】:

struct sort s 是一个自动变量,您将它的指针传递给线程。这不是一个好习惯。即使在调用pthread_create 的函数返回后,线程仍然存在,在这种情况下,堆栈变量将超出范围。从quicksortthread 函数本身创建线程的方式也存在同样的问题,其中struct sort s1struct sort s2 是quicksortthread 函数的局部变量。最好动态地为这些结构分配内存。

此外,尚不清楚您为什么在主目录中添加了sleep(100)。您应该使用pthread_join 等待创建的线程完成,然后再退出主线程。

pthread_create 只需要 pthread_t * 作为第一个参数,但是,您传递的是 pthread**pthread_t *th1 应该是 pthread_t th1

【讨论】:

  • 我猜 sleep 是为了让局部变量在线程运行时保持活动状态。
  • 感谢您的回答。我正在使用睡眠功能,因为我只处于原型程序阶段。那么,如何将这三个值(char *array、int left、int right)传递给我的线程?
  • 但是,quicksortthread 本身会创建更多线程,并且该函数似乎在创建这些线程后退出。
  • 请为结构排序变量动态分配内存,即设为“struct sort *s1 = malloc(sizeof(struct sort));
  • C 标准没有“堆栈”的概念,并且不需要实现为此类变量使用堆栈。它是一个自动变量,如何分配是一个实现细节,在这里完全不相关。相关的是生命周期,resp。存储期限。
猜你喜欢
  • 1970-01-01
  • 2014-09-09
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 2021-04-13
相关资源
最近更新 更多