我尝试避免使用向量并将其替换为智能指针作为练习。目标是从智能指针中受益,从而避免内存泄漏而不依赖向量,因为这就是我现在要尝试的方法。
以下代码只是一种易于理解的方式。
我希望我的C ++代码能够正常工作。
更新:我想尽可能地使用原始c'ish风格的IO:一般来说,请不要使用std :: string或c ++流。
C版本(工作):
typedef struct{
char ** my_arr;
} MyInputs;
...
MyInputs *Doc = malloc(sizeof *Doc);
*Doc->my_arr = malloc(sizeof (*Doc->my_arr) * 2);
Doc->my_arr[0] = malloc(10);
Doc->my_arr[1] = malloc(10);
// Yes, that is stupid to alloc 10 bytes and use only 6 or 5. That is for the example.
memcpy(Doc->my_arr[0],(char*)"Hello\0",6);
memcpy(Doc->my_arr[1],(char*)"Cool\0",5);
printf("%s %s \n",Doc->my_arr[0],Doc->my_arr[1] );
...
Run Code Online (Sandbox Code Playgroud)
C ++版本(尝试):
typedef struct {
std::shared_ptr<char*>my_arr;
}MyInputs;
...
std::shared_ptr<MyInputs> MainDoc (static_cast<MyInputs*>(malloc(sizeof (*MainDoc))),free);
std::shared_ptr<char*> Z (static_cast<char**>(malloc(sizeof …Run Code Online (Sandbox Code Playgroud) 所以我有这个代码:
#include <iostream>
#include <list>
#include <string>
#include <memory>
using namespace std;
int main() {
{
shared_ptr<string> str = new string("Marius");
cout << str + " MMG";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通过编译它:
clang++ -Wall -g -std=c++14 test.c++ -o test
Run Code Online (Sandbox Code Playgroud)
我明白了:
test.c++:11:22: error: no viable conversion from 'string *' (aka 'basic_string<char> *') to 'shared_ptr<string>'
shared_ptr<string> str = new string("Marius");
Run Code Online (Sandbox Code Playgroud)
哪里出错了?使用GCC我得到了同样的错误.
我有一个执行类的下面程序,该程序填充下面所示的映射
map<string,map<string,vector<StructAbsTypeObject>>>
Run Code Online (Sandbox Code Playgroud)
在这里,我正在创建共享对象并分配它们,它们在第一次检查时有效,但是在第二次检查时,shared_ptr返回null。我需要知道原因。该代码看起来不错,但不知道哪里出了问题。
//Code begins
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <memory>
using namespace std;
class Test {
public:
Test(int i):t(i) {
}
private:
int t;
};
class ConcTypeObject {
public:
ConcTypeObject() {
}
ConcTypeObject(const ConcTypeObject& other) {
m_ptr_Test = other.m_ptr_Test;
}
ConcTypeObject& operator=(const ConcTypeObject& other) {
m_ptr_Test = other.m_ptr_Test;
}
void setTest(shared_ptr<Test> ptr) {
cout << "setTest" << endl;
m_ptr_Test = ptr;
}
shared_ptr<Test> getTest() {
return m_ptr_Test;
}
bool isValid() {
if(m_ptr_Test) {
return true; …Run Code Online (Sandbox Code Playgroud) 为什么以下代码抛出
Exception thrown at 0x53A5C6DC (nvoglv32.dll) in RenderEngine.exe: 0xC0000005: Access violation reading location 0x0002B174.
Run Code Online (Sandbox Code Playgroud)
在运行时,什么是一个很好的解决方案?
std::vector<std::shared_ptr<Static>> statics;
void drawStatics() {
for (std::shared_ptr<Static> stat: statics) {
Static *statptr = stat.get();
statptr->Draw(); //This is what triggers the runtime exception.
}
}
void addStatic(Mesh &mesh, Texture &texture, Transform transform) {
statics.push_back(
std::make_shared<Static>(
mesh,
texture,
transform,
shader,
camera
));
}
int main() {
addStatic(playerMesh, playerTexture, platformTransform);
drawStatics();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Static头文件如下:
#pragma once
#include "mesh.h"
#include "texture.h"
#include "transform.h"
#include "camera.h"
#include "shader.h"
class …Run Code Online (Sandbox Code Playgroud) 我有一个类Foo,其中包含一个shared_ptrclass Foo2作为成员变量。我这样宣告以上观点:
class Foo{
public:
Foo();
private:
shared_ptr<Foo2> f2;
};
Run Code Online (Sandbox Code Playgroud)
首先,我可以做以上吗?即不初始化shared_ptr?
在Foo我的默认构造函数中,如下所示初始化了shared_ptr变量:
Foo::Foo(){
//create and calculate parameters a, b and c
f2(new Foo2(a, b, c));
}
Run Code Online (Sandbox Code Playgroud)
由于Foo2的唯一构造函数具有3个参数。但是,这显示了一个错误:
Type 'shared_ptr<Foo2>' does not provide a call operator
这不是创建类的共享指针实例的方法吗?
#include <memory>
#include <iostream>
struct Foo {
Foo() { std::cout << "Constructor ...\n"; }
void doSth() {std::cout << "hi" << std::endl;}
~Foo() { std::cout << "Destructor ...\n"; }
};
int main() {
{std::weak_ptr<Foo> jack = (*(new std::shared_ptr<Foo>(new Foo)));
std::cout << (jack).use_count() << std::endl;
// std::shared_ptr<Foo> ptr = jack.lock();
// std::cout << ptr.use_count() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
use_count()返回值为1,因此我认为拥有该对象的最后剩余的shared_ptr将被销毁,因此将调用析构函数.但事实并非如此.任何人都可以解释原因吗?如果我想维护这样的结构:new std :: shared_ptr(new Foo)并且还调用了析构函数,我该怎么办?代码只是为了好玩而编写,没有任何应用程序背景.
首先,一个典型的实施是std::weak_ptr什么?特别是std::weak_ptr只是一个控制块的指针std::shared_ptr?
如果所有std::shared_ptr引用都消失了,内部控制块是否被删除?如果是这样,std::weak_ptr::expired()如果重新使用该内存,如何正常运行?
我有一个包含a的对象,std::weak_ptr我想memcpy将对象转换为稍后要处理的缓冲区.这样做会以某种方式打破智能指针的内部工作吗?
我有这个代码,我试图在两个不同的类中使用相同的模板.当我编译时,我得到错误:
#include <iostream>
#include <memory>
template <class T>
class Node
{
public:
int value;
std::shared_ptr<Node> leftPtr;
std::shared_ptr<Node> rightPtr;
Node(int val) : value(val) {
std::cout<<"Contructor"<<std::endl;
}
~Node() {
std::cout<<"Destructor"<<std::endl;
}
};
template <class T>
class BST {
private:
std::shared_ptr<Node<T>> root;
public:
void set_value(T val){
root->value = val;
}
void print_value(){
std::cout << "Value: " << root.value << "\n";
}
};
int main(){
class BST t;
t.set_value(10);
t.print_value();
return 1;
}
Run Code Online (Sandbox Code Playgroud)
错误:
g++ -o p binary_tree_shared_pointers.cpp --std=c++14
binary_tree_shared_pointers.cpp:39:8: error: elaborated type refers …Run Code Online (Sandbox Code Playgroud)