在函数中声明非指针变量是否存在内存泄漏
Is declaring non pointer variables in a function a memory leak?
如标题所示,在函数中声明非指针变量是否存在内存泄漏?我在网上找遍了所有的答案,我确实找到了一个答案,但它是针对C的,我不确定同样的规则是否适用于c++。我目前正在改进我的一个老项目,我正在努力提高内存效率。作为一个例子,我有一个加载函数,将被调用至少10-20次,只是在启动期间,我想知道什么影响将声明非指针变量对内存。
void ObjectLoaderManager::loadObject(char FileName[20])
{
char file[100] = "ResourcesModels"; // Declare object file path
strncat_s (File, FileName, 20); // Combine the file path with the input name
std::string newName; // Declare a new scring
newFile = File; // Assign File to the string newFile
int health = 10;
// Assign newFile as the name of the newly created object and assign health variable
// in later parts of the function
}
虽然我确实理解这个函数的部分是明显的坏,很多这些都不是我不会做的做法,但是,我很想知道一遍又一遍地声明非指针变量会在本地化函数中做什么。谢谢你!这是我在文章开头提到的文章的链接http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html
不,在函数中声明的任何局部变量都是在堆栈上分配的,所以除非函数被递归地调用多次,否则这是没有问题的。
相关文章:
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 如何检查QList中是否存在值
- 根据某个函数是否存在启用模板
- 地图计数确实很重要,或者只是检查是否存在
- C++中是否存在 std::conditional 的懒惰等价物?
- 无论如何,我可以确定构造函数是否存在吗?
- COM :是否可以查看是否存在对我的某个 COM 对象的进程外引用?我可以释放它吗?
- 堆分配的对象是否存在永不为空的唯一所有者?
- 扩展类中的可选 vir 函数,测试它在运行时是否存在
- 模板化检查是否存在带有参数列表的类成员函数?
- 是否存在包含负号的isdigit函数(过载)
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 检查编译时是否存在静态函数
- 向量是否存在行主要形式?
- 检查 TinyXML 中的元素是否存在
- 检测是否存在具有 C++17 的类成员
- 虚拟继承中是否存在多重继承?
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- std::weak_ptr 和相应的 std::shared_ptr 之间是否存在数据竞争?
- 是否存在用于 C 或 C++ 中常见数学运算(例如最小值、最大值和平均值)的可导入库?