Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:40:12+00:00 2026-05-27T00:40:12+00:00

I am getting a Segmentation Fault error when running my program in GCC. This

  • 0

I am getting a Segmentation Fault error when running my program in GCC. This is quite a long program so I am only posting the parts which I believe are relevant; please let me know if more is required.

void *RelaxThread(void *para) {
    printf("Entering RelaxThread...\n");
    struct Parameter *parameters;
    parameters = (struct Parameter *) para;
    /*Some code here*/
    pthread_exit(NULL);
}

int relax(double **matrix, int size, int threads, double precision) {
    keepRunning = 0;
    int rowAllocation = (size-2) / threads;     
    struct Parameter para[threads];
    pthread_t threadcount[threads];
    int current;
    int startRow = 1;
    long t;    
    for(t=0; t<threads; t++){       
        para[t].matrix = matrix;
        para[t].size = size;
        para[t].threads = threads;
        para[t].precision = precision;
        para[t].allocation = rowAllocation;
        para[t].threadId = t;
        para[t].startRow = startRow;
        startRow += rowAllocation;
        printf("DebugMsg1\n");
        current = pthread_create(&threadcount[t], NULL, RelaxThread, (void *) &para[t]);
        printf("DebugMsg2\n");
    }
    void *status;
    int rc;
    for(t=0; t<threads; t++) {
        rc = pthread_join(threadcount[t], &status);
        if (rc) {
            exit(-1);
        }
    }
    if (keepRunning) {
        printf("Iterating...\n");
        relax(matrix, size, threads, precision); 
    }
}

int main(int argc, char *argv[])
{
    int const size = 8;
    int const threads = 2;
    double const precision = 1e-6;
    double **matrix = (double **) malloc(size * sizeof(double));
    populateMatrix(matrix, size);
    relax(matrix, size, threads, precision);
    free(matrix);
    pthread_exit(NULL);
    return 0;
}

The code runs fine when size is set to 8 or below. However anything larger than this (it only supports even numbers for now) gives a segmentation fault on the second iteration of relax(). The debug messages therefore run as:

Debug1
Debug2
Debug1
Debug2
Entering RelaxThread...
Entering RelaxThread...
Iterating...
Debug1
Debug2
Debug1
Segmentation Fault

The fact that this runs where size is 8 or less but crashes above this number has completely confused me, and I have spent a long time trying to work out why this is happening. I completely acknowledge that my code is neither the neatest nor most efficient, but would greatly appreciate some advice as to why this is failing.

Edit 1: attaching GDB backlog as requested.

Debug1
[New Thread 0x40040940 (LWP 23270)]
Debug2
Debug1
Entering RelaxThread...
Entering RelaxThread 0
[New Thread 0x40081940 (LWP 23271)]
Debug2
Entering RelaxThread...
Entering RelaxThread 1
[Thread 0x40040940 (LWP 23270) exited]
Main: completed join with thread 0 having a status of 0
Main: completed join with thread 1 having a status of 0
Iterating...
Debug1
[Thread 0x40081940 (LWP 23271) exited]
[New Thread 0x40081940 (LWP 23272)]
Debug2
Debug1

Edit2: updated the backtrace log following changes.

Edit3: the below is the full RelaxThread

    void *RelaxThread(void *para) {
        struct Parameter *parameters;
        parameters = (struct Parameter *) para;
        double **matrix = parameters->matrix;
        int size = parameters->size;
        double precision = parameters->precision;
        int threads = parameters->threads;
        int threadId = parameters->threadId;
        int startRow = parameters->startRow;
        int allocation = parameters->allocation;
        int finishRow = (startRow + allocation);
        int i;
        int j;
        double oldValue;
        double newValue;
        double difference;
        for(i=startRow;i<finishRow;i++) {
            for (j=1;j<size-1;j++) {
                pthread_mutex_lock (&mutexmatrix); //Tested with and without the mutex, which is globally defined and initialised elsewhere
                oldValue = matrix[i][j];
                newValue = ((matrix[i+1][j] + matrix[i-1][j] + matrix[i][j+1] + matrix[i][j-1]) / 4);
                matrix[i][j] = newValue;
                pthread_mutex_unlock (&mutexmatrix);
                difference = oldValue - newValue;
                if(difference < 0) {
                    difference = -difference; 
                }
                //printf("Precision on thread%d: %f , aiming for %f\n",threadId, difference,precision);
                if (difference > precision) {
                    keepRunning = 1;
                }
            }
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:40:12+00:00Added an answer on May 27, 2026 at 12:40 am

    The proper initialization, assuming a squared matrix would be:

    double **matrix = malloc(size * sizeof(double *));
    for(int i=0; i<size; i++)
    {
        matrix[i] = malloc(size * sizeof(double));
        for(int j=0; j<size; j++)
        {
            matrix[i][j] = 0; // removing the junk contents
        }
    }
    

    EDIT:
    I also realized you never actually initialize void *status. Since you’re not using it, change it to rc = pthread_join(threadcount[t], NULL);, that’s surely another problem.

    EDIT3:
    Another problem I found is int startRow = 1;, I do not see how you’re accessing the matrix, but it’s index should start as 0.

    EDIT4:
    Also, when you free a two dimensional array you need to do it this way:

    for(int i=0; i<size; i++)
    {
        free(matrix[i]);    //Free each row pointer
    }
    free(matrix);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting this error: Program received signal SIGSEGV, Segmentation fault. 0x0000000000407265 in Quadtree::deeper (this=0x7fffffffe430,
I am getting a segmentation fault error when I run this code. I don't
I've a 'C' program which has encountered a strange problem.. I'm getting segmentation fault
I am getting the following error message: Program received signal SIGSEGV, Segmentation fault. 0x08048ff3
I am getting a segmentation fault while running this code. I can't work out
I am getting a segmentation fault in console while running the program i am
I am getting segmentation fault in getaddrinfo(). This is the stack trace. Program received
I am getting segmentation fault in this code but i cant figure out why.
Whats wrong with this?I am getting segmentation fault during runtime. int size; scanf(%d,&size); int
I have a C program below written on UNIX. I am getting segmentation fault.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.