Frage

I have been working on this program that accomplishes this:

counts the number of occurrences of a specific integer value in a 2D array (matrix). Each position of the matrix must first be initialized to an integer value between 0 and n. Once initialized, program will search and count the total number of occurrences of a specific value. The program is run by taking in the parameters as command line arguments:

programName rows cols n c

rows – number of rows of the matrix

cols – number of columns of the matrix

n – the upper bound of the random values of the matrix, values can be 0–(n-1)

c – the value to search for in the matrix, note c must be between 0–(n-1)

After this, the program implements the search using 1 to 10 threads and displays the execution time and number of occurrences.

I seem to have all of this working how I wish, however the problem is that whenever I enter a value over 4 in the command line for rows, I keep getting the segment fault error. I am at a loss as to what is causing this. Please help me understand what error in my coding may be contributing to this? Thank you.

#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);

}
War es hilfreich?

Lösung 2

The allocation of the rows should be like this

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

Because the sizeof datatypes can vary. But the sizeof a pointer will be constant in a particular machine architecture. In a 64 bit machine the sizeof a pointer will be 8 bytes. But sizeof int will be usually 4 bytes (gcc). So here you will be having allocated only 4 blocks. That why when you try to pass more than 4, it's crashing because there's an invalid memory read.

Also your program will cause memory leak, as you are not freeing the allocated memory. Use like this at the end.

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

Andere Tipps

Here is one problem:

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

should be:

arr = (int**)malloc(rows * sizeof(int *));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top