小编Gal*_*axy的帖子

为什么将变量移位超过其位宽度会归零?

这个问题的灵感来自 StackOverflow 的其他问题。今天,在浏览 StackOverflow 时,我遇到了一个问题,即将变量按值 k 进行位移,该值 >= 该变量的位宽。这意味着将 32 位 int 移位 32 位或更多位。

将整数左移 32 位

意外的 C/C++ 按位移位运算符结果

从这些问题中可以明显看出,如果我们尝试将数字移位 >= 变量位宽的 k 位,则仅采用最低有效 log2k 位。对于 32 位 int,最低有效 5 位被屏蔽并作为移位量。

因此,一般来说,如果 w = 变量的位宽, x >> k则变为x >> (k % w) 对于 an int,则为x >> (k % 32)

计数被屏蔽为 5 位,这将计数范围限制为 0 到 31。

所以我编写了一个小程序来观察理论上应该产生的行为。我在评论中写下了最终的偏移量 % 32。

#include <stdio.h>
#include <stdlib.h>

#define PRINT_INT_HEX(x) printf("%s\t%#.8x\n", #x, x);

int main(void)
{
    printf("==============================\n");
    printf("Testing x …
Run Code Online (Sandbox Code Playgroud)

c bit-shift undefined-behavior

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

C++ sleep()函数的工作方式与预期不符

同事!我试着sleep()在我的代码中使用函数和system()函数.代码应该显示第一条消息,然后倒计时,每次打印剩余的秒数并等待1秒.然后应该清除屏幕.我想制作一个简单的文本动画.但该程序没有按预期工作!我检查了我的代码,看起来我没有语法或逻辑错误.当我编译并运行此代码时,首先它会等待五秒钟,然后在没有等待的情况下将消息打印到stdout,并立即清除屏幕.我认为这是一个运行时错误,虽然它不应该在逻辑上发生.如果以下信息相关,我从Linux终端编译并运行我的C++程序.

#include <iostream>
#include <cstdlib>   // includes system();
#include <unistd.h>  // includes sleep();
using std::cout;
using std::cin;
using std::endl;

int main() {
  cout << "Clearing the screen in: ";
  for (int i = 5; i > 0; i--) {
    cout << i << " ";
    sleep(1);  // takes seconds
  }
  system("clear");

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ sleep runtime-error

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

为什么C允许我调用未声明的函数?

我有两个文件:test1.ctest2.c,其中包含main()函数。

test1.c:

#include <stdio.h>  // printf() function declaration/prototype

// function definition
void say_hello() {
  printf("\tHello, world!\n");
}
Run Code Online (Sandbox Code Playgroud)

test2.c:

#include <stdio.h>  // printf() function declaration/prototype
int main() {
  printf("Inside main()\n");
  say_hello();

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我的makefile:

a.out: test1.o test2.o
    $(CXX) -o a.out test1.o test2.o

test1.o: test1.c
    $(CXX) -c test1.c

test2.o: test2.c
    $(CXX) -c test2.c
Run Code Online (Sandbox Code Playgroud)

现在应该清楚问题出在哪里:test2.c中的main()函数无需声明就可以调用say_hello()!我运行以下命令以使用gcc编译器: make CXX=gcc 屏幕上显示以下警告:

gcc -c test1.c
gcc -c test2.c
test2.c: In function ‘main’:
test2.c:16:3: warning: implicit declaration of function ‘say_hello’ [-Wimplicit-function-declaration] …
Run Code Online (Sandbox Code Playgroud)

c c++ scope function-declaration

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

为什么我不能在C中的函数外部为全局变量赋值?

假设我有一个全局变量,并且想为其分配另一个变量。我发现您可以将另一个值分配给函数内的全局变量:

int i = 8;

int main(void)
{
  i = 9;     /* Modifies i */
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,在函数外部分配全局变量不起作用!

int i = 8;

i = 9;  /* Compiler error */

int main(void)
{
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

warning: data definition has no type or storage class
warning: type defaults to 'int' in declaration of 'i'
  error: redefinition of 'i'
note: previous definition of 'i' was here
int i = 8;
    ^
Run Code Online (Sandbox Code Playgroud)

为什么会这样呢?

c scope global-variables

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

赋值与初始化:在C中的另一个结构内初始化结构

我有一个结构有两个字段结构本身.我想使用初始化列表来分配内部结构的字段,而不必手动分配每个单个字段.

struct point
{
    int x;
    int y;
};

struct rectangle
{
    struct point p1;
    struct point p2;
};

struct rectangle r2;
r2.p1 = {5, 6};
r2.p2 = {7, 20};
Run Code Online (Sandbox Code Playgroud)

但是这段代码不会编译:

structs3.c:105:11: error: expected expression before ‘{’ token
   r2.p1 = {5, 6};
           ^
structs3.c:106:11: error: expected expression before ‘{’ token
   r2.p2 = {7, 20};
           ^
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用?是什么原因?

c struct

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

在C++中更改out-of-bounds指针的取消引用是否安全?

假设我有一个错误的指针,我会增加以查看下一个内存位置内的内容.我的代码工作正常,没有任何错误或问题.我只是打印出这些内存位置的内容.我在屏幕上看到输出,其中一些内存位置的值为0,其他的包含一些大的负数或正数,似乎按照模式排列.但是如果我尝试更改或覆盖这些内存位置的内容呢?它们代表什么?什么样的数据可以存储在这些存储器位置中,如果这些存储器位置有足够的变化,是否有可能破坏操作系统?

  #include <iostream>
  using std::cout;
  using std::endl;
  int main() {
    int num1 = 5;
    int* bad_ptr = &num1;
    cout << "Address of num1: " << &num1 << endl;
    cout << "Dereference bad pointer: " << *bad_ptr << endl;

    // The bad pointer acesses 500 memory addresses
    for (int i = 0; i < 500; i++) {
      bad_ptr++;
      cout << "Dereference bad pointer: " << *bad_ptr << endl;
      // What if I try to change it?
      // *bad_ptr = 1;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ memory pointers shared-memory

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

C++是否重制了循环中定义的变量?

当在C++中的循环内声明变量时,C++是否在循环的每次迭代中重制变量?我的意思是,它是否为另一个num变量重新分配内存?因此,如果循环迭代5次,您是否获得5个num具有自己唯一值的独立变量?在循环开始之前声明变量是一种更好的做法,即使该变量仅在循环内使用?如果我想将变量用作计数器或占位符怎么办?

// is this better code?
// int num;
for (int i = 0; i < 5; i++) {
  int num;
  // do stuff with num
}
Run Code Online (Sandbox Code Playgroud)

c++ memory loops

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

字符串文字可以与 char* 连接吗?

我知道在 C 中相邻的字符串文字是连接在一起的。我想知道,相邻的字符串文字是否与 char*s 连接?

我问这个问题的原因是因为我想将两个字符串的串联传递给perror(),并且其中一个字符串事先未知,所以我必须使用char*.

perror("error opening " "file.txt");  // string literals are concatenated

char* filename = "file.txt";  // or some other file
perror("error opening " filename);    // will this work?
Run Code Online (Sandbox Code Playgroud)

c string-literals char-pointer

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

C++ 11 int8_t 有问题的输入/输出

这是我的程序中的一段代码。我正在使用int8_t数据类型,并且在输入/输出方面遇到一些问题。显然,int8_t在程序中使用需要-std=c++11为编译器设置标志g++。我这样做了,但出现运行时错误。这是我的代码:

#include <iostream>
#include <cstdint>
using std::cout;
using std::cin;
using std::endl;
int main() {
  int8_t number;

  cout << "Enter a number [1-100]: ";
  cin >> number;
  cout << "You entered: " << number << endl;
  cout << number << " / 10 = " << (number / 10) << endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是程序的输出:

 $ ./a.out
Enter a number [1-100]: 95
You entered: 9
9 / 10 = 5
Run Code Online (Sandbox Code Playgroud)

$是 …

printf iostream input cstdint c++11

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

传递NULL作为参数时,函数重载如何工作?

我知道NULL是#defined为0.它似乎是一个int转换为指针类型的常量.那么当有两个重载函数时会发生什么:一个采用指针类型,另一个采用int类型.这是如何工作的nullptr

#include <iostream>
using namespace std;

void callMe(int* p)
{
    cout << "Function 1 called" << endl;
}

void callMe(int i)
{
    cout << "Function 2 called" << endl;
}

int main()
{
    callMe(nullptr);
    callMe(NULL);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道callMe(nullptr)肯定会调用第一个函数.但是哪个函数会被调用callMe(NULL)?我在我的电脑上编译了这段代码.只有一个警告.

g++ -std=c++11 nullptr_type.cpp
nullptr_type.cpp: In function 'int main()':
nullptr_type.cpp:44:14: warning: passing NULL to non-pointer argument 1 of 'void callMe(int)' [-Wconversion-null]
   callMe(NULL);
          ^
Run Code Online (Sandbox Code Playgroud)

我的代码编译没有任何问题,当我运行它时,我看到callMe(NULL)称为函数2. Visual Studio也编译代码,它调用函数2.

但是,当我尝试在在线GeeksForGeeks IDE上编译我的代码时,我遇到了一些编译器错误.

https://ide.geeksforgeeks.org/UsLgXRgpAe

我想知道为什么会出现这些错误. …

c++ overloading overload-resolution c++11

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

Rust 借用了可能未初始化的变量 - 作为程序员,这对我来说是显而易见的,它总是会被初始化

我在这里遇到 Rust 编译器问题。这段代码最初是用 C 编写的,我的工作是将其移植到 Rust 代码中。我不会在这里修改算法或任何内容,但 Rust 编译器比 C 编译器更严格,并且它标记了完全有效的代码。

error[E0381]: borrow of possibly-uninitialized variable: `output_file_handler`
   --> src/main.rs:318:9
    |
318 |         output_file_handler.write(b"Why won't it work?");
    |         ^^^^^^^^^^^^^^^^^^^ use of possibly-uninitialized `output_file_handler`

error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)

我正在编写的这个程序使用 Rust 的 MPI 库,但现在让我们忽略它,因为它不是我的问题的一部分。

问题是,我File在函数顶部声明了一个可变对象main(),但在开始时未初始化。现在因为我使用的是 MPI,所以这是一个多进程程序。我正在对此进行一些简化,并删除对我的问题来说不必要的代码。

my_rank变量基本上保存当前正在执行的进程的“进程ID”,因为相同的代码可以由多个进程运行。进程my_rank == 0只负责将输出打印到文件,其他进程都不执行此操作。在 的进程中my_rank == 0output_file_handler是“活动的”,而在其他进程中,它保持未初始化状态,只是从未使用过。

除了声明之外,所有对 的访问都output_file_handler在进程中my_rank == 0,因此它在使用时总是被初始化。然而,编译器太愚蠢而无法意识到这一点,而且太严格,因此它会惩罚完美的代码

首先我有 的声明output_file_handler。然后我有一些代码启动 MPI 子系统并“分叉”一堆进程,并为每个进程分配其各自的等级。然后在之后 …

compilation rust

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

为什么64位系统上的int_fast16_t为64位?

我查看了<stdint.h>我的实现中的头文件.我看到以下内容:

typedef long int int_fast16_t;
typedef long int int_fast32_t;
typedef long int int_fast64_t;
Run Code Online (Sandbox Code Playgroud)

我有一个64位系统,所以long int占用64位.为什么所有三种数据类型都被定义为长整数?我理解int_fast64_t的情况,它是64位.但为什么16位和32位数据类型有64位?这是某种错误吗?我创建了一个小程序来检查是否是这种情况:

sizeof(int_fast8_t) : 1
sizeof(int_fast16_t) : 8
sizeof(int_fast32_t) : 8
sizeof(int_fast64_t) : 8
Run Code Online (Sandbox Code Playgroud)

是否定义了这些数据类型的大小?哪些特征或特征将数据类型定义为"快速"?是数据块从RAM加载到CPU的速度吗?如果int_fast16_tint_fast32_t是8字节宽,性能有哪些好处?在64位系统上访问64位数据类型真的更快吗?

c cstdint stdint

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