小编Pet*_*ter的帖子

Visual Studio中的long long值

我们知道-2*4 ^ 31 + 1 = -9.223.372.036.854.775.807,你可以在long long中存储的最低值,如下所述:整数类型在C++中存储的值的范围.所以我有这个操作:

#include <iostream>

unsigned long long pow(unsigned a, unsigned b) { 
    unsigned long long p = 1; 
    for (unsigned i = 0; i < b; i++) 
        p *= a; 
    return p; 
}

int main()
{
    long long nr =  -pow(4, 31) + 5 -pow(4,31);
    std::cout << nr << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

为什么它显示-9.223.372.036.854.775.808而不是-9.223.372.036.854.775.803?我正在使用Visual Studio 2015.

c++ long-long

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

意外的pipe()行为

我正在尝试通过管道进行2个进程通信.我等待孩子在管道中写一个字符,然后父母将从管道中读取字符并将其显示在屏幕上.问题是我成功地在管道中写入了字符(我做了一个测试,立即从中读取,我看到它在管道中),但是当父管从管道中读取时,它内部没有任何内容.我真的不明白为什么; 一切似乎都很好.

int PID, canal[2];
if(-1 == (PID = fork()))
{
    perror("Error");
    return 0;
}
if(-1 == pipe(canal))
{
    perror("Error");
    return 0;
}
if(PID == 0)
{
    close(canal[0]);
    printf("Child\n");
    char buffer = 'C';
    if( 0 == write(canal[1], &buffer, sizeof(char)))
        printf("Didn't write anything\n");
    close(canal[1]);
}
else
{
    char readBuffer;
    wait(NULL);
    printf("Parent\n");
    close(canal[1]);
    if(read(canal[0], &readBuffer, sizeof(char)))
    {
        printf("I read: ");
        printf("%c\n", readBuffer);
    }
    close(canal[0]);
}
Run Code Online (Sandbox Code Playgroud)

c linux posix fork pipe

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

标签 统计

c ×1

c++ ×1

fork ×1

linux ×1

long-long ×1

pipe ×1

posix ×1