安全 return 和从函数动态分配内存的处理,C++ 11
Safe return and handling of dynamically allocated memory from a function, C++ 11
我是 C++ 的新手,因此对智能指针的概念和用法也是新手。我想为函数中的结构动态分配内存,然后在接收器完成后使用该内存。我希望唯一(非共享)接收器安全地释放内存。类似于以下内容:
typedef struct {
int x;
int y;
} myStruct;
myStruct* initMem(void)
{
myStruct* result = new myStruct();
result->x = 12;
result->y = 14;
return result;
}
int main()
{
cout << ">>>>> Main | STARTED <<<<<" << endl;
myStruct* w = initMem();
cout << w->x << endl;
cout << w->y << endl;
delete w;
return 1;
}
注意:以上只是我想要实现的示例示例。结构比那复杂得多,我只能使用动态内存分配。
我读到在 C++ 中使用原始指针进行动态内存管理并不好,因为 C++ 有专门用于此的智能指针的概念。你能帮我把上面的逻辑转换成使用智能指针吗?
提前致谢。
没有理由使用指针和动态分配的内存。使用自动存储时长:
myStruct initMem()
{
myStruct result{};
result.x = 12;
result.y = 14;
return result;
}
int main()
{
cout << ">>>>> Main | STARTED <<<<<" << endl;
myStruct w = initMem();
cout << w.x << endl;
cout << w.y << endl;
}
如果您有充分的理由使用动态分配的内存,那么您必须遵守 RAII 原则。标准库中的智能指针就是这样做的:
std::unique_ptr<myStruct> initMem(void)
{
auto result = std::make_unique<myStruct>();
result->x = 12;
result->y = 14;
return result;
}
int main()
{
std::cout << ">>>>> Main | STARTED <<<<<" << std::endl;
std::unique_ptr<myStruct> w = initMem();
std::cout << w->x << std::endl;
std::cout << w->y << std::endl;
}
同样在 C++ 中,您不需要 typedef。实际上不使用它是惯用的:
struct myStruct {
int x;
int y;
};
使用唯一指针 std::unique_ptr
. If coding with c++14 and later, then you can benefit from the std::make_unique
创建一个 myStruct
对象并将其环绕在唯一指针周围。
但即使您不使用 c++14 或更高版本,您也可以自己简单地创建 make_unique
函数并相应地使用它。
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
所以下面的 c++11 示例将使用 make_unique
而不是 std::make_unique
。
#include <iostream>
#include <memory>
struct myStruct
{
int x;
int y;
myStruct(int x_, int y_) : x(x_), y(y_)
{
std::cout<< "Calling user-def constructor..." <<std::endl;
}
~myStruct()
{
std::cout<< "Calling default destructor..." <<std::endl;
}
};
int main()
{
std::cout << ">>>>> Main | STARTED <<<<<" << std::endl;
std::unique_ptr<myStruct> ptr = std::make_unique<myStruct>(2,3);
std::cout<< ptr->x << "," << ptr->y <<std::endl;
}
我是 C++ 的新手,因此对智能指针的概念和用法也是新手。我想为函数中的结构动态分配内存,然后在接收器完成后使用该内存。我希望唯一(非共享)接收器安全地释放内存。类似于以下内容:
typedef struct {
int x;
int y;
} myStruct;
myStruct* initMem(void)
{
myStruct* result = new myStruct();
result->x = 12;
result->y = 14;
return result;
}
int main()
{
cout << ">>>>> Main | STARTED <<<<<" << endl;
myStruct* w = initMem();
cout << w->x << endl;
cout << w->y << endl;
delete w;
return 1;
}
注意:以上只是我想要实现的示例示例。结构比那复杂得多,我只能使用动态内存分配。
我读到在 C++ 中使用原始指针进行动态内存管理并不好,因为 C++ 有专门用于此的智能指针的概念。你能帮我把上面的逻辑转换成使用智能指针吗?
提前致谢。
没有理由使用指针和动态分配的内存。使用自动存储时长:
myStruct initMem()
{
myStruct result{};
result.x = 12;
result.y = 14;
return result;
}
int main()
{
cout << ">>>>> Main | STARTED <<<<<" << endl;
myStruct w = initMem();
cout << w.x << endl;
cout << w.y << endl;
}
如果您有充分的理由使用动态分配的内存,那么您必须遵守 RAII 原则。标准库中的智能指针就是这样做的:
std::unique_ptr<myStruct> initMem(void)
{
auto result = std::make_unique<myStruct>();
result->x = 12;
result->y = 14;
return result;
}
int main()
{
std::cout << ">>>>> Main | STARTED <<<<<" << std::endl;
std::unique_ptr<myStruct> w = initMem();
std::cout << w->x << std::endl;
std::cout << w->y << std::endl;
}
同样在 C++ 中,您不需要 typedef。实际上不使用它是惯用的:
struct myStruct {
int x;
int y;
};
使用唯一指针 std::unique_ptr
. If coding with c++14 and later, then you can benefit from the std::make_unique
创建一个 myStruct
对象并将其环绕在唯一指针周围。
但即使您不使用 c++14 或更高版本,您也可以自己简单地创建 make_unique
函数并相应地使用它。
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
所以下面的 c++11 示例将使用 make_unique
而不是 std::make_unique
。
#include <iostream>
#include <memory>
struct myStruct
{
int x;
int y;
myStruct(int x_, int y_) : x(x_), y(y_)
{
std::cout<< "Calling user-def constructor..." <<std::endl;
}
~myStruct()
{
std::cout<< "Calling default destructor..." <<std::endl;
}
};
int main()
{
std::cout << ">>>>> Main | STARTED <<<<<" << std::endl;
std::unique_ptr<myStruct> ptr = std::make_unique<myStruct>(2,3);
std::cout<< ptr->x << "," << ptr->y <<std::endl;
}