采取以下琐碎的计划:
#include <iostream>
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用valgrind运行它,我被告知有72,704 bytes in 1 blocks
那些still reachable
.关于是否担心仍然可以达到警告的SO已经进行了广泛的讨论 - 我并不关心这一点.我只想了解当程序本身没有分配该库中的任何对象时,如何简单地包含标准库头可能会导致仍然可以访问的警告.
这是完整的valgrind
输出:
$ valgrind --leak-check=full --track-origins=yes --show-reachable=yes ./ValgrindTest
==27671== Memcheck, a memory error detector
==27671== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==27671== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==27671== Command: ./ValgrindTest
==27671==
==27671==
==27671== HEAP SUMMARY:
==27671== in use at exit: 72,704 bytes in 1 blocks
==27671== …
Run Code Online (Sandbox Code Playgroud) 编辑:对不起,我的问题不明确,为什么书籍/文章更喜欢实施#1而不是实施#2?
使用指针实现Singleton类与使用静态对象的实际优势是什么?为什么大多数书都喜欢这个
class Singleton
{
private:
static Singleton *p_inst;
Singleton();
public:
static Singleton * instance()
{
if (!p_inst)
{
p_inst = new Singleton();
}
return p_inst;
}
};
Run Code Online (Sandbox Code Playgroud)
在此
class Singleton
{
public:
static Singleton& Instance()
{
static Singleton inst;
return inst;
}
protected:
Singleton(); // Prevent construction
Singleton(const Singleton&); // Prevent construction by copying
Singleton& operator=(const Singleton&); // Prevent assignment
~Singleton(); // Prevent unwanted destruction
};
Run Code Online (Sandbox Code Playgroud) 我有一个类在其构造函数中调用内核,如下所示:
"ScalarField.h"
#include <iostream>
void ERROR_CHECK(cudaError_t err,const char * msg) {
if(err!=cudaSuccess) {
std::cout << msg << " : " << cudaGetErrorString(err) << std::endl;
std::exit(-1);
}
}
class ScalarField {
public:
float* array;
int dimension;
ScalarField(int dim): dimension(dim) {
std::cout << "Scalar Field" << std::endl;
ERROR_CHECK(cudaMalloc(&array, dim*sizeof(float)),"cudaMalloc");
}
};
Run Code Online (Sandbox Code Playgroud)
"classA.h"
#include "ScalarField.h"
static __global__ void KernelSetScalarField(ScalarField v) {
int index = threadIdx.x + blockIdx.x * blockDim.x;
if (index < v.dimension) v.array[index] = 0.0f;
}
class A {
public:
ScalarField v; …
Run Code Online (Sandbox Code Playgroud)