【问题标题】:Segmentation fault - C program using Pthreads and matrix分段错误 - 使用 Pthreads 和矩阵的 C 程序
【发布时间】:2014-06-17 14:44:05
【问题描述】:

我一直在做这个程序来完成这个:

计算二维数组(矩阵)中特定整数值的出现次数。必须首先将矩阵的每个位置初始化为一个介于 0 和 n.初始化后,程序将搜索并计算特定值的出现总数。 通过将参数作为命令行参数来运行程序:

programName rows cols n c

rows – 矩阵的行数

cols – 矩阵的列数

n——矩阵随机值的上界,取值可以是0-(n-1)

c - 在矩阵中搜索的值,注意 c 必须在 0-(n-1) 之间

之后,程序使用 1 到 10 个线程实现搜索,并显示执行时间和出现次数。

我似乎已经按照我的意愿完成了所有这些工作,但是问题是,每当我在命令行中为行输入超过 4 的值时,我都会不断收到段错误错误。 我不知道是什么原因造成的。请帮助我了解我的编码中的哪些错误可能导致此问题?谢谢。

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

#define NUM_THREADS 10

int **arr;
int rows, cols, n, c, totalOccurrence, done, numThreads;
int threadCounter[10];

void *matrixThread (void *threadid)
{
  long tid;
  tid = (long)threadid;
  long lowBound = tid * (rows / numThreads);
  long highBound = lowBound + (rows / numThreads);

  int localcount = 0;
   if (tid == numThreads - 1)
     {
       highBound = rows;
     }

  long i;
  int ic;
  for (i = lowBound; i < highBound; i++)
    {
      for (ic = 0; ic < cols; ic++)
        {
          if (arr[i][ic] == c)
            {
              localcount++;
            }
        }
    }
  threadCounter[tid] = localcount;

  pthread_exit(NULL);
   }

int main (int argc, char *argv[])
{
  pthread_t threads[NUM_THREADS];

  if (argc != 5)
    {
      printf("Error: Invalid number of arguments\n");
    }
  else
    {
      rows = strtol(argv[1], NULL, 10);
      cols = strtol(argv[2], NULL, 10);
      n = strtol(argv[3], NULL, 10);
      c = strtol(argv[4], NULL, 10);

      int r, cl;
      arr = (int**)malloc(rows * sizeof(int));
      for (r = 0; r < rows; r++)
        {
          arr[r] = malloc(cols * sizeof(int));
        }

      int randomNum;
      srand(time(NULL));

      for (r = 0; r < rows; r++)
        {
          for (cl = 0; cl < cols; cl++)
            {
              randomNum = rand() % n;
              arr[r][cl] = randomNum;
            }
        }
      long rc, t;

      for (numThreads = 1; numThreads <=  10; numThreads++)
        {
          struct timeval start,end;
          double elapsed_time;

          gettimeofday(&start, NULL);

          for (t = 0; t < numThreads; t++)
            {
              rc = pthread_create(&threads[t], NULL, matrixThread, (void *)t);
              if (rc)
                {
                  printf ("Error: Thread could not be created; return %d", rc);
                  exit(-1);
                }
            }

          for (t = 0; t < numThreads; t++)
            {
              pthread_join(threads[t], NULL);
            }

          totalOccurrence = 0;
          int q;
          for (q = 0; q < numThreads; q++)
            {
              totalOccurrence += threadCounter[q];
            }

          gettimeofday(&end, NULL);
          elapsed_time = (end.tv_sec + end.tv_usec/1000000.10000) - (start.tv_sec + start.tv_usec/1000000.10000);
          printf("\nNumber of threads: %d  "  , numThreads);
          printf("Total Occurrences of %d: %d  "  ,c, totalOccurrence);
          printf("Elapsed time: %.8f\n" , elapsed_time);
          totalOccurrence = 0;

        }
    }
  pthread_exit(NULL);

}

【问题讨论】:

    标签: c matrix segmentation-fault pthreads command-line-arguments


    【解决方案1】:

    这里有一个问题:

    arr = (int**)malloc(rows * sizeof(int));
    

    应该是:

    arr = (int**)malloc(rows * sizeof(int *));
    

    【讨论】:

    • 这可能是原因,因为 sizeof(int) 在大多数平台上是 4 个字节,而 sizeof(int*) 是指针的大小,(通常)与 CPU 的大小相同寄存器(如操作系统所见),即在 32 位操作系统上为 4 个字节,在 64 位操作系统上为 8 个字节。如果您运行 64 位操作系统,则分配错误,因为您只为指针分配 4 个字节。
    【解决方案2】:

    行的分配应该是这样的

    arr = (int**)malloc(rows * sizeof(int*));
    

    因为数据类型的大小可以变化。但是指针的大小在特定的机器架构中是恒定的。在 64 位机器中,指针的大小为 8 个字节。但是 sizeof int 通常是 4 个字节(gcc)。所以在这里你将只分配 4 个块。这就是为什么当您尝试超过 4 时,它会崩溃,因为读取的内存无效。

    您的程序也会导致内存泄漏,因为您没有释放分配的内存。最后这样使用。

        for (r = 0; r < rows; r++)
        {
            free (arr[r]);
        }
        free (arr);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多