小编k3o*_*3oy的帖子

使用realloc是否安全?

前段时间我的一位朋友告诉我不要使用realloc,因为它不安全,但他不能告诉我为什么,所以我对这个问题进行了一些研究,最接近我怀疑的是:
https://buildsecurityin.us -cert.gov/bsi/articles/knowledge/coding/809-BSI.html
http://www.iso-9899.info/wiki/Why_not_realloc
我想知道我是否可以继续在我的代码中使用realloc或者如果它是不安全有没有其他方法来重新分配内存?感谢您的关注.

c memory-leaks memory-management realloc

14
推荐指数
3
解决办法
8483
查看次数

同步STD cout输出多线程

我一直在使用多线程编码,经过一段时间的编写,我意识到如果我在不同的boost :: threads中使用std :: cout,输出将没有逻辑顺序,我正在测试的程序是就像是:

#include <boost/thread/thread.hpp>
#include <iostream>

int my01( void )
{
    std::cout << "my01" << std::endl;
    return 0;
}
/* my02, my03 and my04 are the same with different outputs*/
[...]
int main( void )
{
    boost::thread t1(&my01);
    boost::thread t2(&my02);
    boost::thread t3(&my03);
    boost::thread t4(&my04);

    while(!t1.joinable() || !t2.joinable() || !t3.joinable() || !t4.joinable());

    t1.join();
    t2.join();
    t3.join();
    t4.join();

    std::cout << "The end!" << std::endl;
    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


输出通常就像(它改变):

my02my01
my04
my03
BLANK LINE
结束!

考虑到这个问题,我想创建一个单独的线程来管理所有输出,所以它们将按顺序排列:

MY01
MY02
my03
my04 …

c++ multithreading synchronization cout

6
推荐指数
2
解决办法
9648
查看次数