小编Ren*_*uka的帖子

"cout <<(char*)NULL"在这里做"关闭(1)"吗?

在下面的代码我用过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编译器,观察到相同的行为.

c++ unix

5
推荐指数
2
解决办法
389
查看次数

为什么当我从主要电话中调用ctor时,在范围结束之前调用dtor?(实验)

在这里,我amain()不创建该类的对象的情况下调用类的构造函数,并且它看起来在调用之后立即调用析构函数.这里到底发生了什么?根据我的理解,它正在发生,因为我没有用一些内存创建一个对象?如何在这里调用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)

c++ stl

4
推荐指数
1
解决办法
81
查看次数

pthread 取消成功,但在几百个线程后无法创建线程

这里 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)

c++ linux pthreads

3
推荐指数
1
解决办法
838
查看次数

引用变量显示下一行程序执行后的未查找行为

下面是显示相同操作的不同输出的代码段.为什么我在下面的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)

c++

0
推荐指数
1
解决办法
60
查看次数

如果我们不从c ++中的非void返回类型函数返回任何内容,那么返回值是什么?[实验]

我观察到如果我没有从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值的行为?

c++

0
推荐指数
1
解决办法
164
查看次数

标签 统计

c++ ×5

linux ×1

pthreads ×1

stl ×1

unix ×1