在下面的代码我用过cout<<(char*)NULL;这一行后,我的程序没有打印到输出屏幕.这是否意味着我已经做了close(1)与cout这里?这里到底发生了什么?这是一个错误吗?请分享你的想法.
#include<iostream>
using namespace std;
void f(){
cout<<"\nfun\n";
}
main(){
cout<<(char*)NULL;
f(); //not getting printed !
cout<<"\nhello\n"; //not getting printed !
cout<<"hii how are you?"; //not getting printed, why??
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了gcc和DevCpp编译器,观察到相同的行为.
在这里,我a在main()不创建该类的对象的情况下调用类的构造函数,并且它看起来在调用之后立即调用析构函数.这里到底发生了什么?根据我的理解,它正在发生,因为我没有用一些内存创建一个对象?如何在这里调用dtor?它是如何实现的?请分享您对此的看法.
#include<iostream>
using namespace std;
class a{
public:
a(){
cout<<"\nctor";
}
~a(){
cout<<"\ndtor";
}
};
int main(){
a(); //why the dtor is getting called before the scope ends?
cout<<"\nctor_called\n";
}
Run Code Online (Sandbox Code Playgroud)
o/p该计划:
ctor
dtor
ctor_called
Run Code Online (Sandbox Code Playgroud) 这里 pthread 在 1013 个线程之后没有被创建。我知道每个进程的线程创建都有限制,但在这里我取消了线程,并且在线程中我还调用了pthread_testcancel()取消点。实际上这里发生了什么?有人可以帮我纠正线程创建失败吗?我是多线程的新手,如果您能为我提供详细的解释,那就太好了。谢谢。
#include<iostream>
#include<pthread.h>
void* t(void*){
while(1){
pthread_testcancel(); //cancellation point?
}
}
main(){
pthread_t id;
int i = 0;
while(1){
++i;
if(pthread_create(&id, 0, t, 0)){
std::cout<<"\n failed to create "<<i; //approx get hit at i=1013
break;
}
if(pthread_cancel(id))
std::cout<<"\n i = "<<i; //not at al executes, pthread_cancell is always successful?
}
}
Run Code Online (Sandbox Code Playgroud) 下面是显示相同操作的不同输出的代码段.为什么我在下面的couts中得到不同的值?请分享您对此的看法.
#include<iostream>
#include<stdio.h>
using namespace std;
int const& f(int i=9, int j=99, int=999) {
return i;
}
int main() {
const int &k = f();
//cout<<" \n hello \n"; //If I enable this cout thenn all the below will only print some junk values(even if I remove the const!!).
cout<<"\n"<<k<<" "<<k<<" "<<k; //prints "9 9 9"
cout<<"\n\n"<<k<<" "<<k<<" "<<k<<"\n"; //prints "134520896 134520896 134520896", some garbage value. why is it so?
cout<<"\n\n"<<k<<" "<<k<<" "<<k<<"\n\n"; //prints "134520896 134520896 134520896", some …Run Code Online (Sandbox Code Playgroud) 我观察到如果我没有从int返回类型的空函数返回任何值1.但在下面的例子中,它显示4 3 2为o/p(这是静态变量的值在si这里打印?如果我打印si我会得到o/p为2 3 4,与我现在得到的相反.是否存在在这种情况下,与函数的堆栈推送和弹出有关吗?).另外我观察到如果我使用float作为返回类型,那么它打印nan nan nan为o/p.这种行为是编译器依赖的(我已尝试使用gcc和devcpp,观察相同)?这里到底发生了什么?请分享您对此的看法.
#include<iostream>
using namespace std;
int f(int i){
static int si = i;
si = si + i;
///return si;
}
int main(){
cout<<f(1)<<" "<<f(1)<<" "<<f(1);
//cout<<" "<<f(1); //if I uncomment this line then the o/p is: 4 3 2 5, it looks like it's printing the value of si.
}
Run Code Online (Sandbox Code Playgroud)
它看起来像是cout导致反向打印静态变量si值的行为?