这是在C中创建数组的有效方法吗?(Is this a valid method of creating an array in C?)
int threads = 5;
pthread_t * thread = malloc(sizeof(pthread_t)*threads);
for (i = 0; i < threads; i++){
int ret = pthread_create(&thread[i], NULL, &foobar_function, NULL);}
我现在无法运行代码。 但我看到这是一个在线示例的一部分,并且由于缺少方括号而有点困惑。 我对C不太好
这对于创建线程数组有用吗?
int threads = 5; pthread_t * thread = malloc(sizeof(pthread_t)*threads); for (i = 0; i < threads; i++){ int ret = pthread_create(&thread[i], NULL, &foobar_function, NULL);}I'm not in a position to run the code right now. But I saw this as part of an online example and was a little confused by the total lack of square brackets. I'm not great with C.
So does this work for creating an array of threads?
最满意答案
是。
thread指向malloc分配的malloc块,该内存块足够大以容纳threads pthread_t对象。
pthread_t对象的threads数组可以用这种方式表示。
Yes.
thread is pointing at a block of memory allocated by malloc that is large enough to hold threads pthread_t objects.
An array of threads pthread_t objects can be represented in exactly this way.
更多推荐
发布评论