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 657767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:51:37+00:00 2026-05-13T22:51:37+00:00

I’m working in C with openMP using gcc on a linux machine. In an

  • 0

I’m working in C with openMP using gcc on a linux machine. In an openmp parallel for loop, I can declare a statically allocated array as private. Consider the code fragment:

int a[10];
#pragma omp parallel for shared(none) firstprivate(a)
for(i=0;i<4;i++){

And everything works as expected. But if instead I allocate a dynamically,

int * a = (int *) malloc(10*sizeof(int));
#pragma omp parallel for shared(none) firstprivate(a)

the values of a (at least a[1…9]) are not protected but act as if they are shared. This is understandable as nothing in the pragma command seems to tell omp how big the array a is that needs to be private. How can I pass this information to openmp? How do I declare the entire the dynamically allocated array as private?

  • 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-13T22:51:37+00:00Added an answer on May 13, 2026 at 10:51 pm

    I don’t think you do – what I did to solve this problem was used a parallel region #pragma omp parallel shared(...) private(...) and allocated the array dynamically inside the parallel region. Try this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    /* compile with gcc -o test2 -fopenmp test2.c */
    
    int main(int argc, char** argv)
    {
        int i = 0;
        int size = 20;
        int* a = (int*) calloc(size, sizeof(int));
        int* b = (int*) calloc(size, sizeof(int));
        int* c;
    
        for ( i = 0; i < size; i++ )
        {
            a[i] = i;
            b[i] = size-i;
            printf("[BEFORE] At %d: a=%d, b=%d\n", i, a[i], b[i]);
        }
    
        #pragma omp parallel shared(a,b) private(c,i)
        {
            c = (int*) calloc(3, sizeof(int));
    
            #pragma omp for
            for ( i = 0; i < size; i++ )
            {
                c[0] = 5*a[i];
                c[1] = 2*b[i];
                c[2] = -2*i;
                a[i] = c[0]+c[1]+c[2];
    
                c[0] = 4*a[i];
                c[1] = -1*b[i];
                c[2] = i;
                b[i] = c[0]+c[1]+c[2];
            }
    
            free(c);
        }
    
        for ( i = 0; i < size; i++ )
        {
            printf("[AFTER] At %d: a=%d, b=%d\n", i, a[i], b[i]);
        }
    }
    

    That to me produced the same results as my earlier experiment program:

    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    /* compile with gcc -o test1 -fopenmp test1.c */
    
    int main(int argc, char** argv)
    {
        int i = 0;
        int size = 20;
        int* a = (int*) calloc(size, sizeof(int));
        int* b = (int*) calloc(size, sizeof(int));
    
        for ( i = 0; i < size; i++ )
        {
            a[i] = i;
            b[i] = size-i;
            printf("[BEFORE] At %d: a=%d, b=%d\n", i, a[i], b[i]);
        }
    
        #pragma omp parallel for shared(a,b) private(i)
        for ( i = 0; i < size; i++ )
        {
            a[i] = 5*a[i]+2*b[i]-2*i;
            b[i] = 4*a[i]-b[i]+i;
        }
    
        for ( i = 0; i < size; i++ )
        {
            printf("[AFTER] At %d: a=%d, b=%d\n", i, a[i], b[i]);
        }
    }
    

    At a guess I’d say because OpenMP can’t deduce the size of the array it can’t be private – only compile-time arrays can be done this way. I get segfaults when I try to private a dynamically allocated array, presumably because of access violations. Allocating the array on each thread as if you’d written this using pthreads makes sense and solves the issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Something like this should do it: $("img.thumb").after('<img class="spinner" scr="spinner.gif"/>'); $("img.thumb").load(function()… May 14, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer In generaly, there is no reason to store the dialogs… May 14, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer The method in your DAL needs to return something like… May 14, 2026 at 8:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.