我有一个关于 Fortran-OpenMP 和可分配数组的问题。很简单:空间将分配到哪里?如果我有类似的东西
!$omp parallel default(shared) private(arr)
!$omp critical
allocate( arr(BIGNUMBER) )
!$omp end critical
!do calculations with many arr accesses
!$omp critical
deallocate( arr )
!$omp end critical
!$omp end parallel
Run Code Online (Sandbox Code Playgroud)
空间是分配在栈上还是堆上?如果它在堆上,上面的代码和类似的代码之间有区别吗
allocate( arr(BIGNUMBER, nThread) )
!$omp parallel default(shared) private(localArr)
iThread = omp_get_thread_num()
localArr => arr(:, iThread)
!do calculations with many localArr accesses
!$omp end parallel
deallocate( arr )
Run Code Online (Sandbox Code Playgroud)
简短的问题基本上是:哪一个似乎是问题的最佳解决方案?
假设我想在不使用 POP 的情况下查看堆栈中顶部的两个元素。
我怎样才能访问它 - 我正在尝试:
mov ebp, esp
mov eax, [ebp]
mov ebx, [ebp-4]
Run Code Online (Sandbox Code Playgroud) 我开始学习动态内存分配的主题。
我有以下代码:
#include <iostream>
#include "A.h"
#include "B.h"
using namespace std;
int main() {
/* Both objects on Stack */
A classAStack;
B classBStack;
/* Both objects on Heap*/
// A *classAHeap = new A();
// B *classBHeap = new B();
/* A objects on Heap B ???*/
A *classAHeap = new A();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#ifndef A_H_
#define A_H_
#include <iostream>
#include "B.h"
class A {
public:
A();
virtual ~A();
public:
B b;
};
#endif /* A_H_ */
Run Code Online (Sandbox Code Playgroud)
#include …Run Code Online (Sandbox Code Playgroud) 对于程序来说,局部变量是在堆栈中定义和分配的,但是,我只是想知道定义局部变量的顺序与使用它的顺序不同。例如,在main函数中定义了int abc,如上所述,abc是在堆栈中分配的,这意味着如果变量a位于堆栈底部,但是当变量第一次被使用时,如何将a从堆栈中弹出?或者ebp指向所有变量已存储的位置?
这是一个简单的函数
#include <stdio.h>
int foo() {
int a = 3;
int b = 4;
int c = 5;
return a * b * c;
}
int main() {
int a = foo();
}
Run Code Online (Sandbox Code Playgroud)
foo() 的程序集看起来像
foo:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], 3
mov DWORD PTR [rbp-8], 4
mov DWORD PTR [rbp-12], 5
mov eax, DWORD PTR [rbp-4]
imul eax, DWORD PTR [rbp-8]
imul eax, DWORD PTR [rbp-12]
pop rbp
ret
Run Code Online (Sandbox Code Playgroud)
从 中可以看出rbp - N,内部堆栈框架正在被修改。那么,为什么没有 …
当我使用 分配堆上的内存时malloc(),该内存中可能存在垃圾,除非我使用calloc()或memset()将其清零。对于堆栈来说,情况是否相同,或者我可以安全地假设我在堆栈上分配的任何内容都不会包含垃圾?另外,这在不同的系统或操作系统上是否有所不同?
我正在学习x86汇编,使用下面的代码进行测试,我在gdb控制台中看到指向堆栈顶部的rsp寄存器从0x7FFFFFFFDFD0开始,如果我理解正确的话,在代码中我没有使用push或pop修改 rsp,因此 0x7FFFFFFFDFD0 它是默认值,这意味着我们在堆栈中具有相同的字节数,但我使用堆栈大小为 8mb 的 Linux
section .text
global _start
_start:
mov rcx, 2
add rcx, 8
mov rax, 0x1
mov rbx, 0xff
int 0x80
Run Code Online (Sandbox Code Playgroud) 我的系统:在 x86_64 上运行的 Ubuntu 22.04.3。海湾合作委员会版本 11.4.0
我读到 System V ABI 强制要求使用红色区域。来自海湾合作委员会手册:
红色区域是 x86-64 ABI 强制规定的,它是超出堆栈指针位置的 128 字节区域,不会被信号或中断处理程序修改,因此可用于临时数据而无需调整堆栈指针。该标志
-mno-red-zone禁用该红色区域。
我的问题:
如果我在 gcc 中使用该标志,红色区域是否仍然存在-mno-red-zone?
如果红色区域被“禁用”,这是否意味着我不再遵守系统 V ABI?
这会产生什么后果(不符合 System V ABI)?
我正在使用函数指针数组,并且我想直接将其与其关联的事件 ID 一起使用。问题是,事件 ID 从 0x10 开始到 0x1C,从 0x90 到 0xA5。
我不想在开始时写十个 NULL 元素,是否可以以某种方式声明如下:
int (*tab[256])(uint8_t *data, int *datalen) = {
NULL[10],
fun1,
[...],
funx,
NULL[116],
...
};
Run Code Online (Sandbox Code Playgroud)
目前,我没有看到令人满意的解决方案,这就是我问的原因
我有以下C++代码:
struct B {
int c;
int d;
}
struct A {
int a;
B x;
}
int main() {
A * ptr = new A;
ptr->a = 1;
ptr->x.c = 2;
ptr->x.d = 23;
// a lot of lines of codes later ...
// Will the following values be guranteed?
cout << ptr->x.c << endl;
cout << ptr->x.d << endl;
}
Run Code Online (Sandbox Code Playgroud)
在堆上声明一个新的"struct A"后,ptr-> x会在堆栈还是堆上声明?如果我希望x在堆上,我必须将属性x声明为指针(因此,用"new"初始化它)?