1

I need to pass a structure containing parameters to some threads. One of the parameters is a very large array. I am trying to do what I have done previously where I create the array on the heap using malloc but I can't seem to figure out how to do it with a struct. Then what I'll do is memcpy a different array into the struct array.

#define LIMIT 100000           //Size of seive
#define THNUMS 3               //Number of threads

struct ThreadParams
{ 
    int id;                       // id
    int low;                      // start
    int high;                     // end
    int *structprimes;            // each thread's seive
    structprimes = malloc(sizeof(int)*LIMIT);
 };

Then I create a sieve and then need to copy this sieve to the structs array. I was able to do this with a smaller arrays on the stack with something like this (not complete):

struct ThreadParams ptharg[THNUMS];

memcpy(ptharg[i].structprimes, primes, sizeof(int)*LIMIT);

pthread_create(&tid, NULL, Work, (void*)&ptharg[i])

Hopefully this makes sense? What I'm trying to do is create an array inside a struct using malloc if that's at all possible?

EDIT AND SOLUTION: What I ended up doing was making the struct like this:

struct ThreadParams
{ 
    int id;                       // id
    int low;                      // start
    int high;                     // end
    int *structprimes;     // each thread's seive
};

and then in main() assigning the memory to the pointer with malloc:

for (a = 0; a < THNUMS; a++) 
{
    ptharg[a].structprimes = malloc(sizeof(int)*LIMIT);
}
6
  • I think you just want a static array, not dynamic memory allocation. Commented Dec 7, 2015 at 4:35
  • @JonathonReinhart What do you mean? Like don't use malloc just create the array normally like int structprimes[LIMIT];? Commented Dec 7, 2015 at 4:39
  • int * structprimes = new int[LIMIT]; should help you. I doubt by the Q if you need to use that malloc Commented Dec 7, 2015 at 4:45
  • @nullpointer This is a C question not C++. Commented Dec 7, 2015 at 4:45
  • ok. noted. malloc reqd Commented Dec 7, 2015 at 4:47

1 Answer 1

3

In C it is not possible to have statements inside struct definitions. Instead you need to declare the variable then initialise the variable, including any dynamic memory. For example:

struct ThreadParams ptharg[THNUMS];
int ix;

for (ix = 0; ix < THNUMS; ix++) {
    ptharg[ix].structprimes = malloc(sizeof(int)*LIMIT);
    if (!ptharg[ix].structprimes) {
        /* error handling goes here */
    }
}

Alternatively, the array can be declared statically in the structure. For example:

struct ThreadParams
{ 
    int id;                       // id
    int low;                      // start
    int high;                     // end
    int structprimes[LIMIT];            // each thread's seive
 };

However there is a second possible problem in your approach. You have not shown where struct ThreadParams ptharg[THNUMS]; is located. But if it is inside any function apart from main then it cannot be passed as a data argument to child threads because it would be an automatic variable that goes out of scope when that function exits.

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

3 Comments

This helped me fix my problem! But I was wondering if you could help me understand it better. When you say its not possible to have statements inside declarations what do you mean exactly? Also the way I did it was change my struct to have int *structprimes; Then I used the for loop you showed to allocate the memory and assign it to the struct pointer? Am I understanding this correctly?
In your second example with int structprimes[LIMIT];, this is what I originally used however it was causing segmentation faults because LIMIT needs to be set to values of 1,000,000+.
I've modified the answer to say "struct definitions" instead of just "declarations". What it means is that you can only have the field types and field names within the struct definition. You cannot have any other types of statements including function calls. Yes, the changes you have described sound right. And finally, if you are declaring ptharg on the stack and it is very large then yes you do need to use dynamic memory. But note carefully the last paragraph in the answer. You may need to dynamically allocate the whole ptharg array.

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.