我们刚刚收到 C++ 期末考试的结果。问题之一是编写 的二维矩阵的简单表示double。我通过阅读笔记在析构函数实现中推断出一些要点invalid implementation,但没有进一步的信息。你能告诉我哪里出错了吗(如果我确实出错了)。
这是我写的代码:
~Matrix() {
for (int i=0; i<_rows; i++) {
for (int j=0; j<_cols; j++) {
delete _arr[i][j];
}
delete _arr[i];
}
delete [] _arr;
}
Run Code Online (Sandbox Code Playgroud)
分配内存的代码非常简单,我将粘贴它以使事情更清楚,但有问题的部分在上面的代码中。
Matrix(int rows, int cols) :
_arr(new double*[rows]), _rows(rows), _cols(cols)
{
for (int i=0; i<_rows; i++) {
_arr[i] = new double[_cols];
}
}
Run Code Online (Sandbox Code Playgroud)
他们正确地剥夺了积分吗?我的dtor实施确实无效吗?
所以我想在CUDA中分配2D数组并在CPU和GPU之间复制它们,但我是一个完全的初学者,其他在线材料对我来说很难理解或不完整。重要的是我能够在内核代码中将它们作为二维数组进行访问,如下所示。
请注意,数组的高度!=宽度,如果可能的话,这会让我更加困惑,因为我总是在选择网格大小方面遇到困难。
我考虑过将它们压平,但我真的想让它以这种方式工作。
这就是我自己的研究所取得的进展。
__global__ void myKernel(int *firstArray, int *secondArray, int rows, int columns) {
int row = blockIdx.x * blockDim.x + threadIdx.x;
int column = blockIdx.y * blockDim.y + threadIdx.y;
if (row >= rows || column >= columns)
return;
// Do something with the arrays like you would on a CPU, like:
firstArray[row][column] = row * 2;
secondArray[row[column] = row * 3;
}
int main() {
int rows = 300, columns = 200;
int h_firstArray[rows][columns], h_secondArray[rows][columns];
int *d_firstArray[rows][columns], *d_secondArray[rows][columns]; …Run Code Online (Sandbox Code Playgroud) 我在通话时面临一个奇怪的问题malloc。我正在开发一个使用巨大数组(大小以 GB 为单位)的程序,并在尝试使用malloc该数组为数组分配内存时发现,malloc即使我分配的大小大于我的 RAM(64GB),它也是成功的。
参见下面的代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define Sixteen_G 16000000000
int main() {
int *c = (int *)malloc(sizeof(int)*Sixteen_G);
int *d = (int *)malloc(sizeof(int)*Sixteen_G);
int *e = (int *)malloc(sizeof(int)*Sixteen_G);
int *f = (int *)malloc(sizeof(int)*Sixteen_G);
int *g = (int *)malloc(sizeof(int)*Sixteen_G);
if(c == NULL)
printf("c Allocation failed\n");
if(d == NULL)
printf("d Allocation failed\n");
if(e == NULL)
printf("e Allocation failed\n");
if(f == NULL)
printf("e Allocation failed\n");
if(g == NULL)
printf("e Allocation failed\n");
else
printf("All arrays allocated\n");
return …Run Code Online (Sandbox Code Playgroud) 如果我有一个包含数百万个项目的大型列表,我想迭代它们中的每一个。一旦我使用了该项目,它将永远不会再次使用,那么如何从使用过的列表中删除该项目呢?最好的方法是什么?我知道 numpy 快速且高效,但想知道如何使用普通列表来完成它。
mylst = [item1, item2,............millions of items]
for each_item in mylist:
#use the item
#delete the item to free that memory
Run Code Online (Sandbox Code Playgroud) 最初的问题是docker的java应用程序由于内存使用而被OOM杀死。所以我开始使用 NMT 来理解为什么消耗比预期多。JVM 版本为 1.8.0_212,支持容器。docker 与下一个 java 选项一起启动
JAVA_OPTS='-XX:+AlwaysPreTouch -Xmx128m -Xms128m -XX:MaxMetaspaceSize=150m -XX:ReservedCodeCacheSize=100m -XX:+UseStringDeduplication -XX:+PrintFlagsFinal -XshowSettings:vm -XX:NativeMemoryTracking=detail' ... -m="450m" --cpu-shares="256" docker-image
Run Code Online (Sandbox Code Playgroud)
本机内存跟踪:
Total: reserved=1464054KB +843KB, committed=344578KB +1359KB
...
- Class (reserved=1124594KB +19KB, committed=85066KB +275KB)
(classes #15631)
(malloc=2290KB +19KB #20081 +27)
(mmap: reserved=1122304KB, committed=82776KB +256KB)
...
Run Code Online (Sandbox Code Playgroud)
所有数字均符合预期。唯一的问题是 Class 字段,因为它显示保留值约为 1GB,是否可以以某种方式减少?
以前我见过很多C++函数的汇编。在 gcc 中,所有指令都以以下指令开头:
push rbp
mov rbp, rsp
sub rsp, <X> ; <X> is size of frame
Run Code Online (Sandbox Code Playgroud)
我知道这些指令存储前一个函数的帧指针,然后为当前函数设置一个帧。但在这里,程序集既不要求映射内存(如 malloc),也不检查所指向的内存是否rbp分配给进程。
因此它假设启动代码已经为调用堆栈的整个深度映射了足够的内存。那么到底为调用堆栈分配了多少内存呢?启动代码如何知道调用栈的最大深度?
这也意味着,我可以远距离访问越界数组,因为虽然它不在当前帧中,但它映射到进程。所以我写了这段代码:
int main() {
int arr[3] = {};
printf("%d", arr[900]);
}
Run Code Online (Sandbox Code Playgroud)
当索引为 900 时,它会退出。SIGSEGV但令人惊讶的是,当索引为 901 时,它不会退出。同样,SIGSEGV对于某些随机索引,它会退出,而对于某些索引则不会。在编译器资源管理器中使用 gcc-x86-64-11.2 进行编译时观察到此行为。
有没有办法在 Rust 中将 HashMap 转换为 Vector,无需复制元素,即将数据从哈希映射移动到向量?通过使用 iter+map+collect 需要复制哈希映射的所有元素,这不是最佳的,因为哈希映射不被假定进一步使用。在 Option 的情况下,它工作得很好,因为数据会自动移动,我们可以使用 as_ref 来阻止它。
我必须复制所有元素的代码示例
use std::collections::HashMap;
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
struct base1 {
x: i32,
}
#[derive(Clone, Debug)]
struct base2 {
x: i32,
}
#[derive(Debug)]
struct ss {
x: base1,
y: base2,
}
fn main() {
let mut map = HashMap::new();
map.insert(base1 { x: 13 }, base2 { x: 13 });
let vec: Vec<ss> = map
.iter()
.map(|x| ss {
x: x.0.clone(),
y: x.1.clone(),
})
.collect();
println!("Ma13: {:?}", vec);
}
Run Code Online (Sandbox Code Playgroud) 如何处理这样的场景:我有一个带有自定义删除器的唯一指针,但第 3 方库的函数需要一个原始指针,并且我使用 .get() 在 unique_ptr 中传递存储的指针,但该函数获取原始指针的所有权。
例子:
使用自定义删除器初始化唯一指针:
std::unique_ptr<BIGNUM, decltype(&BN_free)> r(BN_bin2bn(signature.data(), bnLen, nullptr), &BN_free);
std::unique_ptr<BIGNUM, decltype(&BN_free)> s(BN_bin2bn(signature.data() + bnLen, bnLen, nullptr), &BN_free);
Run Code Online (Sandbox Code Playgroud)
将它们传递给 ECDSA_SIG 函数,该函数获取 r 和 s 唯一指针的所有权:
ECDSA_SIG_set0(ecDsaSignature.get(), r.get(), s.get()))
Run Code Online (Sandbox Code Playgroud)
在作用域末尾,调用自定义删除器,但由于 r 和 s 被移动,它崩溃了。我正在考虑使用原始指针,但是如果 ECDSA_SIG_set0 函数调用不成功会发生什么,我是否必须手动释放 r 和 s 变量?这是正确的方法,还是有更优雅的方法来处理这种情况?
先感谢您。
示例1:
char* var1 = "Random string";
Run Code Online (Sandbox Code Playgroud)
示例2:
char* firstline;
fscanf(input, "%s", firstline);
Run Code Online (Sandbox Code Playgroud)
示例3:
char* names[] = {"Karla", "Rob", "Tom"};
Run Code Online (Sandbox Code Playgroud) c++ ×3
arrays ×2
c ×2
docker ×2
memory ×2
callstack ×1
cuda ×1
destructor ×1
dockerfile ×1
java ×1
jvm ×1
loops ×1
memory-leaks ×1
optimization ×1
pointers ×1
python ×1
python-3.x ×1
rust ×1