小编Emp*_*ata的帖子

当子线程崩溃和主要等待加入时会发生什么?

在下面的程序中,我创建了一个pthread_t在函数中崩溃的thread1 func().我对pthread_join命令的确切情况感兴趣main().

我在程序下面运行并正常通过打印"完成"退出.我不知道为什么?

#include <iostream> 
#include <string> 
#include <vector> 
#include <map> 
#include <cstring> 
#include <climits> 
#include <cstdio> 
#include<pthread.h>
#include <stdlib.h>
using namespace std; 

void* func(void *data)
{
    cout<<"Calling func"<<(long)(data)<<endl;
    int *a;
    cout<<a[2]<<endl;
    pthread_exit(0);
}

int main( )
{ 
    pthread_t thread1;
    pthread_create(&thread1, 0 , &func, (void*)2);
    pthread_join(thread1, NULL);
    cout<<"complete"<<endl;

}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading pthreads

9
推荐指数
2
解决办法
2152
查看次数

在代码中使用-DBL_MAX是否安全?

我想要极端的价值.

#include <iostream> 
using namespace std; 
#include <math.h>
#include <limits.h>
#include <values.h>

#define THRESHOLD 2*DBL_MIN

#define FEQ(x, y) (fabs((x) - (y)) < THRESHOLD)

int main( )
{ 
double  a = -DBL_MAX; // I want here minimum value of double
if(FEQ(a,-DBL_MAX))
cout<<"Equal " <<endl;
else
cout<<"NOt equal"<<endl;
return 0;



}
Run Code Online (Sandbox Code Playgroud)

那么,-DBL_MAX在代码中使用是否安全?如果有人知道更好的方法,请在这里分享.

c++

8
推荐指数
1
解决办法
9242
查看次数

当派生类的析构函数是非虚拟的时,为什么基类析构函数在派生对象上调用?

为什么所有析构函数,~D(),~C(),~B(),~A()被称为在下面的例子吗?

只有一个虚拟析构函数:A.

这是代码:

#include<iostream>
using namespace std;

class A
{
public:
  virtual ~A()
  {
    cout<<"destruct A\n";
  }

};
class B:public A
{
public:
  ~B()
  {
  cout<<"destruct B\n"; 
  }
};
class C:public B
{
public:
  ~C()
  {
    cout<<"destruct C\n";
  }
};
class D:public C
{
public:
   ~D()
   {
     cout<<"destruct D\n"; 
   }
};

int main()
{
    A* ptr = new D();
    delete ptr;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism virtual-destructor

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

缺少必需的客户端配置选项:区域

我正在尝试使用以下代码检查 Amazon S3 上的存储桶是否存在:

$credentials = new Aws\Common\Credentials\Credentials($creds['access_key_id'], $creds['secret_access_key']);
$client = Aws\S3\S3Client::factory(array( 'credentials' => $credentials ) );
if( ! $client->doesBucketExist($creds['bucket']) ) {
    throw new Exception("Bucket (" . $creds['bucket'] . ") does not exist.");
}
Run Code Online (Sandbox Code Playgroud)

它在 localhost (wamp) 上工作,但是当我在服务器上尝试它时它不起作用。我收到以下错误:

缺少必需的客户端配置选项:区域:(字符串)“s3”服务需要“区域”配置值(例如,“us-west-2”)。可以在http://docs.aws.amazon.com/general/latest/gr/rande.html上找到可用公共区域和端点的列表. 版本:(字符串)需要“版本”配置值。指定版本约束可确保您的代码不会受到对服务所做的重大更改的影响。例如,在使用 Amazon S3 时,您可以将 API 版本锁定为“2006-03-01”。您的 SDK 版本具有以下“s3”版本: *“2006-03-01” 您可以为“版本”配置值提供“最新”以利用您客户端 API 的最新可用 API 版本供应商可以找到。注意:不建议在生产应用程序中使用“最新”。可以在每个客户端的 API 文档页面上找到可用 API 版本的列表:http : //docs.aws.amazon.com/aws-sdk-php/v3/api/index.html

我不知道为什么它不能在服务器上运行,但相同的代码在 localhost 上运行。

php amazon-s3 amazon-web-services

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

如何在c ++中使用system()命令获取进程的pid

当我们使用system()命令时,程序等到它完成但我正在执行process使用system()和使用负载平衡服务器,因为在执行系统命令之后哪个程序进入下一行.请注意,这process可能不完整.

system("./my_script");

// after this I want to see whether it is complete or not using its pid.
// But how do i Know PID?
IsScriptExecutionComplete();
Run Code Online (Sandbox Code Playgroud)

c++ pid process

5
推荐指数
1
解决办法
1万
查看次数

复制和交换技术在赋值运算符函数中使用复制构造函数

我正在阅读"Scott Meyers的有效C++",其中第11项建议在我的赋值运算符中使用"复制和交换"技术:

Widget& Widget::operator=(const Widget &rhs)
{
    Widget temp(rhs); // Copy constructor
    swap(temp); //Swap with *this
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

但在第12项中写道:

让复制赋值运算符调用复制构造函数是没有意义的.

我认为第11项和第12项是矛盾的.我不正确地理解它吗?

c++

5
推荐指数
1
解决办法
525
查看次数

如何在编译时初始化vptr?

以上是面试问题.

我理解,作为虚拟调度机制的一部分,编译器vTable为每个类创建一个并vptr在编译期间插入一个额外的指针().但究竟是什么时候将类的虚拟表分配给它vptr

如何vptr在编译时初始化?

无论我通过互联网阅读什么,都说编译器vptr在编译时初始化,但初始化是一种运行时机制.我错了吗?

我不明白编译器如何初始化它.

c++

2
推荐指数
1
解决办法
202
查看次数

如何使用fcntl.h在附加模式下写入文件

以下是我的代码:

#include <iostream> 
#include <fcntl.h>
#include <unistd.h>
using namespace std; 

int main()
{

int filedesc = open("testfile.txt", O_RDWR | (O_APPEND |O_CREAT) ,S_IRWXO);

    if (filedesc < 0) {
    cout<<"unable to open file";
        return -1;
    }

    if (write(filedesc, "This will be output to testfile.txt", 36) != -1) {
        cout<<"writing";
        close( filedesc );
    }

    return 0;
return 0;

}
Run Code Online (Sandbox Code Playgroud)

如果我在第二次上面运行相同,则o/p是"无法打开文件".难道我做错了什么?

c++ linux

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

字符数组中的null是否会阻止内存被删除?

有内存泄漏吗?

    const char * pname = "NAME";
    char * temp = new char[strlen(Name) + 64];
    sprintf(temp,"%s", pname);

    delete [] temp; // is there any memory leaks because now length of temp is 4.
Run Code Online (Sandbox Code Playgroud)

c++

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

为priority_queue定义自己的比较器

我正在尝试制作我自己的比较器最小优先级队列:

#include<iostream>
#include <queue>
#include <vector>
using namespace std;

struct ele{
    int data;
    int i,j;
};

struct mycomp
{
    bool operator () (const ele& lhs, const ele& rhs) const
    {
        return lhs.data > rhs.data;
    }
}
int main()
{

    int arr[] = { 1,3, 4, 5};
    int size = *(&arr + 1) - arr;
    cout<<size<<endl;
    priority_queue<ele, std::vector<ele> , mycomp> pq(arr, arr+size);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但它没有编译.你能帮忙吗?我也尝试过operator <,struct ele但也没有编译.

错误信息 :

prog.cpp:17:5: error: expected ';' after struct definition …
Run Code Online (Sandbox Code Playgroud)

c++

-2
推荐指数
1
解决办法
66
查看次数