考虑下面这个简单的代码: class X {
int i_;
public:
X();
};
void f() {
X x;
} f的栈帧是32字节长的,与GCC,这是不必要的长。返回地址和x只需要12个字节,根据Linux/x86_64ABI应该需要16字节对齐。使用Clang时,仅分配了16个字节。为什么GCC需要这么多堆栈空间? GCC组件: f():
sub rsp, 24
lea rdi, [rsp+12]
call X::X()
add rsp, 24
ret Clang组件: f():
push rax
m
在clang中有每个结构的内存开销吗?通常,结构的大小只是其所有成员的总大小。但在clang中,情况似乎并非如此:
#include <iostream>
using namespace std;
struct Obj {
int x;
int y;
};
int main() {
int a = 42;
int b = 43;
Obj obj = {100, 101};
int c = 44;
cout << *(&a - 0) << endl; // 42
cout <
我注意到有时编译器会将垃圾数据保存在调用堆栈中。调用堆栈由函数堆栈框架组成,它是函数调用的激活记录。理想情况下,函数的堆栈框架应该只包含必要的数据,包括溢出的被叫保存寄存器、必须在嵌套函数调用中保留的局部变量、返回地址等。
考虑函数foo()调用多个其他函数的情况。在这些嵌套函数调用中,foo()的激活记录大小可能会发生变化。以下是一个例子:
extern long f(long x);
extern void bar(long x);
extern void tail(void);
void foo(long x) {
long fx = f(x); // x must be pr
在OS X Snow Leopard (10.6)中,我在~/.bashrc中使用了以下几行代码来强制执行clang编译,而不是标准的gcc编译:
# Set Clang as the default compiler for the system
export CC=clang
export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments
当clang无法编译某些东西时,我也(偶尔)不得不使用以下几行代码来使用llvm-gcc (PostgreSQL在很长一段时间内都对此感到内疚):
# Set LLVM GCC as
我最近偶然发现了隐式SSE/AVX加载/存储。我原以为这些是对GCC的一些特殊扩展,但后来意识到它们也可以在MSVC上工作。
__m128 a = *(__m128*)data // same as __m128 a = _mm_load_ps(data)?
__m128 *b = (__m128*)result; // same as _mm_store_ps(result, a)?
这些隐式加载/存储的正确语法是什么?
根据我所读到的(),隐式加载/存储使用对齐的加载/存储,因此内存必须正确对齐。假设它们对大多数编译器(GCC/ICC/MSVC/Clang/MinGW,...)都是一
我一直在寻找内存中的局部变量。令我惊讶的是,我不明白为什么地址是它们的样子。
下面是示例程序
int main() {
int my_array[30];
int a = 10;
int b = 11;
char h = 'A';
char temp_array[10];
int c = 12;
}
我希望堆栈帧是所有这些变量的顺序。在GDB中,打印出内存地址显示情况并非如此!
(gdb) print &my_array
$4 = (int (*)[30]) 0x7fffffffde10
(gdb) print &
此代码段():
#include <iostream>
#include <sstream>
using namespace std;
int main() {
auto s = (ostringstream{} << "string").str();
cout << s;
return 0;
}
用msvc编译和运行,但是无法用clang9.0.0和gcc 9.2编译,这两个错误消息是:no member named 'str' in 'std::basic_ostream&
当用gcc 6.3编译时,这个示例程序
#include <array>
class alignas(4096) A {
std::array<int, 256> array;
};
int main()
{
A a;
}
发出以下警告:
3 : <source>:3:21: warning: requested alignment 4096 is larger than 128 [-Wattributes]
class alignas(4096) A {
^
不过,gcc 7.2没有犯过这样的
我试图使用以下代码在macOS High上打印32位macOS程序集中的整数:
.cstring
STR_D:
.asciz "%d"
.globl _main
_main:
movl $53110, %edi #moves constant int into register
sub $8, %esp #make space on stack
movl %edi, 4(%esp) #move int to stack
movl $STR_D, 0(%esp) #move "%d" to stack