相关疑难解决方法(0)

C++中指针变量和引用变量之间有什么区别?

我知道引用是语法糖,因此代码更容易读写.

但有什么区别?


以下答案和链接摘要:

  1. 指针可以重新分配任意次数,而在绑定后无法重新分配引用.
  2. 指针可以指向nowhere(NULL),而引用总是指对象.
  3. 您不能使用指针来获取引用的地址.
  4. 没有"参考算术"(但您可以获取引用所指向的对象的地址,并对其进行指针运算&obj + 5).

澄清一个误解:

C++标准非常谨慎,以避免规定编译器如何实现引用,但每个C++编译器都将引用实现为指针.也就是说,声明如下:

int &ri = i;
Run Code Online (Sandbox Code Playgroud)

如果它没有完全优化,则分配与指针相同的存储量,并将地址i放入该存储中.

因此,指针和引用都使用相同数量的内存.

作为基本规则,

  • 使用函数参数和返回类型中的引用来提供有用的自我文档化接口.
  • 使用指针实现算法和数据结构.

有趣的读物:

c++ pointers reference c++-faq

3115
推荐指数
34
解决办法
94万
查看次数

C中的"注册"关键字?

什么是register关键字在C语言吗?我已经读过它用于优化,但在任何标准中都没有明确定义.它是否仍然相关,如果是,你什么时候使用它?

c memory keyword

255
推荐指数
10
解决办法
11万
查看次数

为什么在调用printf之前%eax归零?

我想拿起一点x86.我正在使用gcc -S -O0在64位mac上进行编译.

C中的代码:

printf("%d", 1);
Run Code Online (Sandbox Code Playgroud)

输出:

movl    $1, %esi
leaq    LC0(%rip), %rdi
movl    $0, %eax        ; WHY?
call    _printf
Run Code Online (Sandbox Code Playgroud)

我不明白为什么在调用'printf'之前%eax被清除为0.由于printf返回打印的字符数量为%eax我最好的猜测,因此将其归零以准备它,printf但我认为printf必须负责准备它.而且,相反,如果我调用自己的功能int testproc(int p1),则gcc认为没有必要准备%eax.所以我想知道为什么gcc对待printftestproc不同.

macos assembly gcc x86-64 cpu-registers

32
推荐指数
3
解决办法
7918
查看次数

如何在C中最好地使用const关键字?

我试图了解我应该如何const在C代码中使用.首先,我并没有真正使用它,但后来我看到了很多const在整个过程中使用的例子.我应该努力并回去并虔诚地制作合适的变量const吗?或者我只是在等我的时间?

我想它可以更容易地读取预期会发生变化的变量,特别是在函数调用中,对于人类和编译器.我错过了其他重要的观点吗?

c const

8
推荐指数
2
解决办法
8552
查看次数

C++标准歧义

就我在标准中看到的而言,以下代码是有效的.它在MSVC1025中编译.

const struct omg;
struct omg volatile;

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

预选赛constvolatile似乎在这些声明漫无目的.他们既不帮助也不伤害编译器和程序员.

该标准似乎并未倾向于消除这些"空洞的模糊".在空声明的情况下,;明确允许.

在预处理之后,还有其他令牌的情况与表达的含义无关吗?

c++ language-lawyer

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

更快地计算(近似)方差

我可以通过CPU分析器看到,这compute_variances()是我项目的瓶颈.

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 75.63      5.43     5.43       40   135.75   135.75  compute_variances(unsigned int, std::vector<Point, std::allocator<Point> > const&, float*, float*, unsigned int*)
 19.08      6.80     1.37                             readDivisionSpace(Division_Euclidean_space&, char*)
 ...
Run Code Online (Sandbox Code Playgroud)

这是函数的主体:

void compute_variances(size_t t, const std::vector<Point>& points, float* avg,
                       float* var, size_t* split_dims) {
  for (size_t d = 0; d < points[0].dim(); d++) {
    avg[d] = 0.0;
    var[d] = 0.0;
  }
  float delta, n;
  for (size_t i = 0; i < points.size(); ++i) …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm optimization variance

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

与 SSE 比较 16 字节字符串

我有 16 字节的“字符串”(它们可能更短,但您可能会假设它们在末尾用零填充),但您可能不会假设它们是 16 字节对齐的(至少不总是)。

如何编写一个例程将它们与 SSE 内在函数进行比较(是否相等)?我发现这个代码片段可能会有帮助,但我不确定它是否合适?

register __m128i xmm0, xmm1; 
register unsigned int eax; 

xmm0 = _mm_load_epi128((__m128i*)(a)); 
xmm1 = _mm_load_epi128((__m128i*)(b)); 

xmm0 = _mm_cmpeq_epi8(xmm0, xmm1); 

eax = _mm_movemask_epi8(xmm0); 

if(eax==0xffff) //equal 
else   //not equal 
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下或者写一个函数体吗?

它需要在 GCC/mingw 中工作(在 32 位 Windows 上)。

c x86 gcc sse simd

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

为什么我可以获取寄存器变量的地址?

引自"Thinking in c ++"一书关于寄存器变量的部分:"寄存器变量的使用存在限制.你不能获取或计算寄存器变量的地址.一个寄存器变量只能在一个块中声明(你不能有全局或静态寄存器变量.)"

所以我写了这段程序来测试:

int global = 2;
// error
// register int global2 = 3;

int main() {
    register int local2 = 2;
    cout << local2 << " " << &local2 << endl;
}
Run Code Online (Sandbox Code Playgroud)

但是g ++没有生成错误,打印出local2的地址.那么为什么我可以在没有错误的情况下获取地址?

c++

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

为什么我必须玩“rsp”来调用 C++ 函数?

我刚刚开始我的组装之旅,很明显我是一个新手,我一直在编写相当简单和基本的程序,我只是注意到一些奇怪的东西(对我来说)。

一个程序给出以二进制 111 结尾的表格中的数字计数

入口点:

#include <iostream>
#include <cstdlib>

extern "C" auto _start(void *, void *)->void;

auto print_msg(char *msg) {
    std::cout << msg;
}

auto print_int(uint64_t val) {
    std::cout << val;
}

auto main()->int {
    _start(print_int, print_msg);
    std::cout << std::endl;
    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

集会:

.const
_tab    dw 65535, 61951, 61949, 61925, 61927, 61734, 61735, 61728
_LENGTH = ($ - _tab) / 2
_msg_1  db 'There are ', 0
_msg_2  db ' numbers ending with 111 in binary!', 0

.code
_start proc …
Run Code Online (Sandbox Code Playgroud)

windows assembly x86-64 masm calling-convention

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

将值作为参数传递时出现“类型名称不允许指定存储类”错误

我一直在为 6502 编写仿真器的基础。我有一个函数,我想用它来设置给定寄存器的零标志和负标志。该函数包含在一个名为 的结构体中CPU

我的代码目前看起来像这样:

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

typedef uint8_t BYTE;
typedef uint16_t WORD;
typedef uint32_t DWORD;

struct CPU {
   BYTE SP; // stack pointer
   WORD PC; // program counter
  
   BYTE A, X, Y; // registers

   // some more registers, flags, etc.
   
   void SetStatusFlags(BYTE register) {
      Z = (register == 0);
      N = (register & 0b10000000) > 0;
   }

   // some other functions that call this one...
};
Run Code Online (Sandbox Code Playgroud)

这会导致错误:

error: type name does not allow …
Run Code Online (Sandbox Code Playgroud)

c++

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

寄存器和易失性有什么区别?什么时候用用哪一种?易失性寄存器变量是什么意思?

寄存器和易失性有什么区别?什么时候使用哪一个?易失性寄存器变量的含义是什么?

register int a; volatile int a;

c volatile storage-class-specifier

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

奇怪的 C++ 错误,不合格的 ID,重新声明

我想创建一个项目,我有3个文件,一个test.cppsomething.h和一个something.cpp。他们来了:

测试.cpp:

#include <bits/stdc++.h>
#define f cin
#define g cout
#include "something.h"
using namespace std;

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

东西.h:

#ifndef __SOMETHING__H_
#define __SOMETHING__H_
#include <bits/stdc++.h>

void register(int x);
#endif
Run Code Online (Sandbox Code Playgroud)

东西.cpp:

#include "something.h"

void register(int x)
{
    std::cout << x << '\n';
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

In file included from test.cpp:4:0:
something.h:5:15: error: expected unqualified-id before ‘int’
 void register(int x);
               ^~~
something.h:5:15: error: expected ‘)’ before ‘int’
test.cpp: In function ‘int …
Run Code Online (Sandbox Code Playgroud)

c++ file

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