我有一个问题 - 使用以下代码我试图找出存储在某个地址的内容以及我的静态变量存储在这个特定位置的时间.(我读到静态变量无限存储并且非常惊讶 - 想测试这是否属实).代码定义了一个静态变量(它在我的系统上的地址是0x1000020c0 - 这可能是相当随机的但是一直是这种情况)
如果我现在想要找出该地址存储的整数值,我必须先用$ number打印出地址,然后给出0x1000020c0.重新/重新解释地址(0x1000020c0)仅提供100!如果地址是在之前打印的,或者我在重新解释/重铸中使用&编号.
有人可以解释为什么会这样吗?
int static number = 100;
// std::cout << number << std::endl; <- prints 100
// prints 100 and the address 0x1000020c0 in my case
// std::cout << number << " " << &number << std::endl;
// this does not work unless &number is printed previously
// std::cout << "Value is : " << *reinterpret_cast<int*>(0x1000020c0) << std::endl;
// this does work and show the correct value (100)
std::cout << "Value is …
Run Code Online (Sandbox Code Playgroud) 当我有一个循环并且在这个循环内创建一个新的堆栈变量(不将它分配在堆上并在循环体内声明它的变量),是否保证在下一次迭代开始之前调用这个对象的析构函数,或者可能编译器展开的循环会改变什么?
当给 ARM gcc 9.2.1 提供命令行选项-O3 -xc++ -mcpu=cortex-m0
[compile as C++] 和以下代码时:
unsigned short adjust(unsigned short *p)
{
unsigned short temp = *p;
temp -= temp>>15;
return temp;
}
Run Code Online (Sandbox Code Playgroud)
它产生合理的机器代码:
ldrh r0, [r0]
lsrs r3, r0, #15
subs r0, r0, r3
uxth r0, r0
bx lr
Run Code Online (Sandbox Code Playgroud)
这相当于:
unsigned short adjust(unsigned short *p)
{
unsigned r0,r3;
r0 = *p;
r3 = temp >> 15;
r0 -= r3;
r0 &= 0xFFFFu; // Returning an unsigned short requires...
return r0; // computing a …
Run Code Online (Sandbox Code Playgroud) 假设以下课程:
class Example
{
public:
...
Example& operator=(const Example& rhs);
...
private:
other_type *m_content;
size_t m_content_size;
}
Example& Example::operator=(const Example& rhs)
{
if (this != &rhs)
{
delete m_content;
m_content = nullptr;
m_content = getCopiedContent(rhs);
}
return *this;
}
Run Code Online (Sandbox Code Playgroud)
我知道这不是最好的实现方式,operator=
但这是有目的的,因为我的问题是关于这两行:
m_content = nullptr;
m_content = getCopiedContent(rhs);
Run Code Online (Sandbox Code Playgroud)
可以是编译器将优化,m_content = nullptr;
即使getCopiedContent
未定义为throw()
或noexcept
:
other_type* getCopiedContent(const Example& obj);
Run Code Online (Sandbox Code Playgroud)
一方面,编译器可以假设如果在m_content = nullptr;
我m_content
用返回值覆盖值之后getCopiedContent
,它可以优化整个m_content = nullptr;
表达式.另一方面,如果编译器将其优化并 …
看看这个小片段:
struct A {
virtual ~A() { }
};
struct B { };
bool fn() {
A *volatile a = new A;
return dynamic_cast<B *>(a);
}
Run Code Online (Sandbox Code Playgroud)
编译器是否允许dynamic_cast
完全删除,并转换dynamic_cast
为简单nullptr;
?
这个问题的原因是这个答案.
笔记:
假设volatile意味着编译器不能假设任何东西a
,因为它是volatile.这是一个问题的原因.
dynamic_cast
可能不被允许删除的事实是程序中某处可能存在某种类型,它来源于A
和B
.
我想证明密码很容易从程序中读取:
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv)
{
char password[] = "a big refreshing lemonade";
return strcmp(argv[1], password);
}
Run Code Online (Sandbox Code Playgroud)
但是它不能按预期工作:
$ gcc foo.c
$ hexdump -C a.out | grep -C2 'lem'
000006c0 00 00 00 48 89 45 f8 31 c0 48 b8 61 20 62 69 67 |...H.E.1.H.a big|
000006d0 20 72 65 48 ba 66 72 65 73 68 69 6e 67 48 89 45 | reH.freshingH.E|
000006e0 d0 48 89 55 d8 48 b8 …
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
int array[1000000];
array[1] = 1;
array[10] = 10;
Run Code Online (Sandbox Code Playgroud)
我们已经为该数组静态分配了大小为1000000的内存。但是是否全部使用了这些内存?
我的意思是如果我们改为这样做:
int *array = malloc(sizeof(int) * 1000000);
array[1] = 1;
array[10] = 10;
Run Code Online (Sandbox Code Playgroud)
然后看来,实际上我们实际上使用了所有这1000000个空间:由于我们已经分配了它们,因此它们无法用于内存中的其他任何空间。但是对于静态分配的数组,事物未初始化,因此事物仍可以存储在没有设置值的其余999998点中。
本质上,我要问的是:int array[num]
使用的内存将少于int array = malloc(sizeof(int) * num)
。
如果我宣布一个原子变量,这是只有在一个线程中使用,是编译器能够优化了这一点,并更换std::atomic<T>
有T
在某些情况下?
我已经阅读了一些关于激励的编译器优化的文章,但它们主要是关于锁和存储的重新排序和分组,而不是关于消除它们.
我们std:shared_pointer
举个例子.它有一个原子计数器,但如果只有一个线程可以访问它,它可以用一个简单的计数器代替,它仍然会表现得好像是一个原子.
我有以下示例
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
typedef struct test{
int a;
long b;
int c;
} test;
int main()
{
test *t = (test*) malloc(offsetof(test, c));
t -> b = 100;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我不确定.我想我在这里有UB.我们有一个指向结构类型对象的指针.但结构类型的对象并不真正有效.
我通过了标准,找不到这种行为的任何定义.我能找到的唯一一个接近这个的部分是6.5.3.2:
如果为指针分配了无效值,则unary*运算符的行为未定义
但这并不是真正相关的,因为返回的指针malloc
是完全有效的.
标准中是否有参考解释这种行为?我正在使用C11 N1570.
我知道该constexpr
关键字可用于在 C++ 中执行编译时计算。例如:
constexpr int factorial(int n)
{
return n <= 1 ? 1 : (n * factorial(n - 1));
}
Run Code Online (Sandbox Code Playgroud)
(取自https://en.cppreference.com/w/cpp/language/constexpr)
能否将编译时计算视为 C++ 与 C 的主要优势?
据我了解,在 C 中不可能进行编译时计算。constexpr
不可用,我认为必须在运行时评估代码。
这是 C++ 程序与等效 C 程序相比可以获得更好性能(例如速度)的一种方式吗?
c++ ×7
c ×5
atomic ×1
c++11 ×1
compile-time ×1
constexpr ×1
destructor ×1
dynamic-cast ×1
elf ×1
exception ×1
gcc ×1
optimization ×1
pointers ×1
string ×1
struct ×1
throw ×1
variables ×1
volatile ×1