1

I'm creating a program with multiple threads where one of threads creates 2 matrices based on input. These inputs are n and seed. Input specifies the square size of the matrix (n x n size) while seed fills the matrices with the values starting from seed incremementally. The problem I'm having is that this function runs fine for inputs n = 1,2,3,4 but 5 and above causes a segmentation fault.

void *creatematrices(void *arg)
{

   pthread_mutex_lock(&mutex1); 
   pthread_mutex_lock(&mutex2); 
   pthread_mutex_lock(&mutex3);
   pthread_mutex_lock(&mutex4);  

   int seed;

   printf("To create two nxn matrices, A and B,first please enter the value for n\n");
   scanf("%d", &n);
   printf("Now please enter the seed integer for the first element of matrix A\n");
   scanf("%d", &seed);

   A = (int**)malloc(n * sizeof(int));
   for(int i = 0; i < n; i++)
   {
       A[i] = (int*)malloc(n * sizeof(int*));
   }

   B = (int**)malloc(n * sizeof(int));
   for(int i = 0; i < n; i++)
   {
       B[i] = (int*)malloc(n * sizeof(int*));
   }

   C = (int**)malloc(n * sizeof(int));
   for(int i = 0; i < n; i++)
   {
       C[i] = (int*)malloc(n * sizeof(int*));
   }

   for(int i = 0; i < n; i++)
   {
       for(int j = 0; j < n; j++)
       {
           A[i][j] = seed++;
       }
   }
   for(int i = 0; i < n; i++)
   {
       for(int j = 0; j < n; j++)
       {   
           B[i][j];
       }
   }

   pthread_mutex_unlock(&mutex1);
   pthread_mutex_unlock(&mutex2);

   pthread_exit(0);
}

I'm using dynamic allocation for the 2d Arrays. I've realized this is probably a stack overflow error so I've already entered ulimit -s unlimited to no avail.

9
  • Can you show us how you call this function? It's not clear why the declaration takes void* arg, among other things. Commented Oct 24, 2019 at 1:00
  • it's a separate thread so pthread_create(&th1, &attr, creatematrices, NULL); is used to call it. If this were implemented not as a thread it would probably take the pointers A, B, and C as arguments. Commented Oct 24, 2019 at 1:02
  • 1
    You've got the malloc backwards. The first malloc should be allocating pointers using sizeof(int*), and the second malloc should be allocating integers using sizeof(int). Commented Oct 24, 2019 at 1:05
  • You're also locking four mutexes, but only releasing two. Commented Oct 24, 2019 at 1:07
  • 1
    Thank you you are 100% correct. Very silly of me. Do you have any idea why this would result in correct answers for smaller values though? Like I said this was working for n = 4. Commented Oct 24, 2019 at 1:13

1 Answer 1

2

Presumably A is of int **A

then you made a mistake in the mallocs:

A = (int**)malloc(n * sizeof(int)); // << wrong. It has to be sizeof (int*)
for(int i = 0; i < n; i++)
{
   A[i] = (int*)malloc(n * sizeof(int*)); // << wrong. It has to be sizeof (int)
}

In the above A[i] points to a memory pool which contains n pointers to int, so it must be dealt accodingly.

Same story with all other allocations.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.