我理解在C++ 中使用new反对的好处malloc.但是,对于特定的情况下,如原始数据类型(非阵列) - int,float等等,是它更快地使用malloc比new?
虽然,new如果我们分配一个数组以便我们可以使用,它总是建议甚至用于原语delete[].
但对于非数组分配,我认为不会有任何构造函数调用int?因为,new运算符分配内存,检查它是否已分配,然后调用构造函数.但只为元非阵列堆分配,是它更好地使用malloc比new?
请指教.
请就此提出建议。我正在尝试学习并看看无效的期货是否可以代替以下场景的条件变量。
在 Scott Meyers《Effective Modern C++》一书中,他建议使用 void Promise 和 void futures 来进行线程之间的一次性通信,等待线程可以有一个 std::future< void > 并在其上调用 wait() ,并且调用线程可以调用 set_value () 对应的 std::promise <void>。(对于多个线程,每个线程都可以有一个 void 共享 future 的副本)但他提到这只能使用一次。这意味着什么?
考虑一个应用程序,其中主线程将工作放置在具有 3 个槽的向量的每个槽上。每个槽位有 2 个工人,并且只有一个人可以从槽位中挑选工作。我希望 master 每次用新工作填充所有 3 个槽时通知所有工作人员,并睡眠 n 个单位时间以允许工作人员完成工作。我需要重复一遍,那么我可以给每个工人一份主人承诺的共同未来吗?根据 Scott 的说法,promise/future 只能使用一次,这是否意味着一旦设置了promise 的值,就不能再次使用?对于这种情况,条件变量、互斥体是更好的方法吗?
请建议何时使用 void Promise、Future 与条件变量、互斥体?
在我的项目中,我需要每隔n秒轮询一些设备并睡眠并永远继续.我创建了一个async任务,启动为async而不是std::thread.但是如果我std::this_thread::sleep_for()在异步任务中使用启动async,看起来它实际上阻止了我的主线程?以下程序永远输出"Inside Async ..",它永远不会打印"Main function".
如果我使用a std::thread(),而不是异步,它将工作正常.但是我想使用异步任务,因为我不必加入它并管理它的生命周期,而不像线程.
如何让异步任务睡眠?
#include <iostream>
#include <future>
#include <thread>
int main()
{
std::async(std::launch::async,
[]()
{
while(true)
{
std::cout <<"Inside async.."<< std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
}
});
std::cout <<"Main function"<< std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 考虑以下代码,
#include "iostream"
#include "conio.h"
using namespace std;
class sample
{
private:
int i;
public:
sample(int ii=0) : i(ii){
cout<<"Constructing Object"<<endl;
}
~sample() { cout<<"Destructing Object"<<endl; }
void* operator new(size_t nSize, void* loc){
cout <<"Inside new"<<endl;
cout <<loc<<endl;
return loc;
}
void operator delete(void* ptr){
cout <<"Inside delete"<<endl;
free(ptr);
}
};
int main()
{
int intArr[2];
sample* samplePtr = new(intArr) sample(5);
cout <<samplePtr<<endl;
delete samplePtr;
// samplePtr->sample::~sample();
getch();
}
Run Code Online (Sandbox Code Playgroud)
输出:
Inside New
0x22ff38
Constructing Object
0x22ff38
Destructing Object
Inside Delete …Run Code Online (Sandbox Code Playgroud) 我在Bruce Eckel的Thinking in C++中读到,当C++编译器遇到一个函数时,它使用它的名称和参数来装饰它的名字.对于函数,int func(char ch); 它将它的名称装饰为_func_char它不使用返回类型.
在这种情况下,在下面的程序中,如果在函数名称修饰期间没有存储返回类型,编译器如何抱怨"从int到const char*的无效转换"?有人可以澄清一下吗?
#include <iostream>
using namespace std;
int func()
{
int i = 5;
return i;
}
int main()
{
string str = func();
}
Run Code Online (Sandbox Code Playgroud) 这是关于删除堆上分配的对象的指针.程序如下,
class Sample{
private:
int m_value1;
int m_value2;
public:
Sample();
void setValues(int m_value1, int m_value2);
void display();
};
Sample::Sample(){
this->m_value1 = 0;
this->m_value2 = 0;
}
void Sample::setValues(int m_value1, int m_value2){
this->m_value1= m_value1;
this->m_value2 = m_value2;
display();
if(this != NULL)
{
cout <<"Deleting this pointer!"<<endl;
delete this;
cout <<"this->m_value1 is "<<this->m_value1<<endl;
cout <<"this->m_value2 is "<<this->m_value2<<endl;
}
}
void Sample::display(){
cout <<"Values are "<<this->m_value1<<" and "<<this->m_value2<<endl;
}
int main(){
Sample *obj1 = new Sample();;
obj1->setValues(4,6);
obj1->display();
getch();
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
Values are …Run Code Online (Sandbox Code Playgroud)