C - 分配单个整数

发布于 2024-10-31 05:26:46 字数 398 浏览 0 评论 0原文

我正在尝试 pthread 库,并且得到了以下代码:

for (int j = 0; j < NUM_THREADS; j++)
{
     int *threadNum = malloc(sizeof(int));
     *threadNum = j;
     pthread_create(..., (void *)threadNum);
}

由于没有空闲,因此该代码存在内存泄漏。我应该在哪里放置 free 以避免内存泄漏?如果我只是写这样的内容:

int *threadNum = 0;
*threadNum = j;

这会导致段错误。我无法将 free 放在范围内,因为我在接下来的几行中使用 pthread_join 。

I'm experimenting with pthread library and I've got the following piece of code:

for (int j = 0; j < NUM_THREADS; j++)
{
     int *threadNum = malloc(sizeof(int));
     *threadNum = j;
     pthread_create(..., (void *)threadNum);
}

Since there is no free, this code has a memory leak. Where should I place free to avoid memory leaks? If I simply write something like:

int *threadNum = 0;
*threadNum = j;

This leads to a segfault. And I can't place free inside the scope, because I'm using pthread_join in the next few lines.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爺獨霸怡葒院 2024-11-07 05:26:46

使用它的线程(因为它作为参数传递)应该在使用它后释放它。

The thread which uses it (since it is passed as the parameter) should free it when it is done with it.

新雨望断虹 2024-11-07 05:26:46

永远不要这样做!每次循环时,您都会丢失threadNum的值,这是一个巨大内存错误!您应该在循环之前分配一个包含 NUM_THREADS 个元素的数组,并在不再需要时将其释放!

Never do this! You will lose threadNum's value every time you loop, that's a huge memory fault! You should allocate an array with NUM_THREADS elements before the loop and free when you don't need it anymore!

幸福%小乖 2024-11-07 05:26:46

当不再使用分配的内存时,应该调用 free。

据推测,这是在连接之后,但由于整数是在循环内分配的,因此您需要在循环外保留引用。

不要单独分配整数,而是考虑在循环之外立即分配所有整数:(

int *threadNums = malloc(sizeof(int) * NUM_THREADS)

for (int j = 0; j < NUM_THREADS; j++) {
  threadNum[j] = j;
  pthread_create(..., (void *)&threadNum[j]);
} 

//something to ensure the threads are done using the memory

free(threadNums);

未测试语法错误,但希望您明白)

特别注意注释。如果您的线程在调用 free 之前尚未完成内存的使用,则会出现竞争条件,从而导致未定义的行为。确保这一点的一种方法是连接所有线程。

或者,如果数据仅由线程使用,则线程可能负责释放内存 - 在这种情况下,当线程函数不再需要它时,您可以在指针上调用 free 。

You should call free when you're no longer using your allocated memory.

Presumably, that's after your join, but since your integers are allocated inside the loop, you would need to keep a reference outside the loop.

Rather than allocating your integers individually, consider allocating all of them at once, outside the loop:

int *threadNums = malloc(sizeof(int) * NUM_THREADS)

for (int j = 0; j < NUM_THREADS; j++) {
  threadNum[j] = j;
  pthread_create(..., (void *)&threadNum[j]);
} 

//something to ensure the threads are done using the memory

free(threadNums);

(Not tested for syntax errors, but hopefully you get the idea)

Pay special attention to the comment. If your threads are not done with the memory before you call free, you have a race condition which causes undefined behavior. One way of ensuring that is to join all of the threads.

Alternatively, if the data is only used be the thread, the thread could be responsible for freeing the memory - in that case, you call free on your pointer when the thread function no longer needs it.

并安 2024-11-07 05:26:46

怎么样:创建一个 NUM_THREADS 大小的数组,每次迭代时,您可以将 j 分配给数组中的第 j 个索引。然后,您可以将数组索引的地址传递给每个 pthread_create 调用,并且也不需要任何动态分配。

How about this: create an array of NUM_THREADS size, and with each iteration you can assign j to the jth index in the array. Then you can pass the address of the array index to each pthread_create call, and it would also not require any dynamic allocation.

街道布景 2024-11-07 05:26:46

您应该像这样简单地编写代码:

for (int j = 0; j < NUM_THREADS; j++)
{
     pthread_create(..., (void *)j);
}

然后就不需要涉及分配了。将线程启动函数的参数强制转换回 (int) 来使用它。由于您使用的是 pthread_create,因此您的目标是 POSIX 系统,这意味着您不必处理纯 C 允许的所有无意义的内容,而这种类型的转换可能无法正常工作。

You should simply write your code like this:

for (int j = 0; j < NUM_THREADS; j++)
{
     pthread_create(..., (void *)j);
}

Then there is no allocation involved. Cast the thread start function's argument back to (int) to use it. Since you're using pthread_create, you're targetting a POSIX system, which means you don't have to deal with all the nonsense plain C allows whereby this type of casting might not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文