vas*_*sin 1 c memory malloc memory-management
我正在尝试使用pthread库,我有以下代码:
for (int j = 0; j < NUM_THREADS; j++)
{
int *threadNum = malloc(sizeof(int));
*threadNum = j;
pthread_create(..., (void *)threadNum);
}
Run Code Online (Sandbox Code Playgroud)
由于没有空闲,此代码存在内存泄漏.我应该放在哪里以避免内存泄漏?如果我只是写一些像:
int *threadNum = 0;
*threadNum = j;
Run Code Online (Sandbox Code Playgroud)
这导致了段错误.而且我无法在范围内自由放置,因为我在接下来的几行中使用了pthread_join.