简体   繁体   English

如何在结构中malloc一个数组

[英]How to malloc an array inside a struct

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. 我正在尝试做我之前所做的事情,我使用malloc在堆上创建数组,但我似乎无法弄清楚如何使用结构。 Then what I'll do is memcpy a different array into the struct array. 那么我要做的是将一个不同的数组memcpy到struct数组中。

#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? 我想要做的是使用malloc在struct中创建一个数组,如果可能的话?

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: 然后在main()中使用malloc将内存分配给指针:

for (a = 0; a < THNUMS; a++) 
{
    ptharg[a].structprimes = malloc(sizeof(int)*LIMIT);
}

In C it is not possible to have statements inside struct definitions. 在C中,不可能在结构定义中包含语句。 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]; 你没有显示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. 但是如果它在除main之外的任何函数内部,则它不能作为数据参数传递给子线程,因为它将是一个自动变量,当该函数退出时超出范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM