【发布时间】:2014-05-21 03:20:53
【问题描述】:
正如标题所示,在函数中声明非指针变量会导致内存泄漏吗?我在 Internet 上到处寻找,确实找到了一个答案,但它是针对 C 的,我不确定相同的规则是否适用于 C++。我目前正在改进我的一个较旧的项目,并且我正在努力提高内存效率。例如,我有一个加载函数,它在启动期间至少会被调用 10-20 次,我想知道声明非指针变量会对内存产生什么影响。
void ObjectLoaderManager::loadObject(char FileName[20])
{
char file[100] = "Resources\Models\"; // 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
【问题讨论】:
-
您可能希望正确转义该目标文件路径。
-
哦,我现在做的事情写得不一样。我只是以它为例。
标签: c++ pointers memory-leaks