简体   繁体   English

C ++数组也会泄漏内存吗?

[英]Can c++ array also leak memory?

I am new to c++, I was fearlessly experimenting until I saw the lecture on cs50 that memory leaks and this other stuff can crash my computer, I have programmed in PHP and javascript, where such things don't exist I guess, here is the program I wrote: 我是c ++的新手,我无所畏惧地进行了实验,直到我看到了关于cs50的讲座,即内存泄漏以及其他东西可能会使我的计算机崩溃,我已经用PHP和javascript编程,我猜这些东西不存在,这是我写的程序:

int main(int argc, char** argv) {

    ifstream inFile;

ofstream outFile;

int size;

inFile.open("text.txt"); //Step 2

outFile.open("formated.out"); //Step 3

// here I am sending the file through PHP where the first line of the file is the number of lines in the file //这里是通​​过PHP发送文件,其中文件的第一行是文件中的行数

inFile >> size;

size += 1;

string strArr[size];

for(int i = 0;i < size;i++){
    getline(inFile, strArr[i]);
}

string crntStr;
int sl;
string newStr;
for(int i = 1;i < size;i++){

    newStr = "";

    string crntStr = strArr[i];

    sl = crntStr.length();

    for(int j = 0;j < sl;j++){

        if(crntStr[j] == '<')
        newStr += "&lt;";
        else if(crntStr[j] == '>')
        newStr += "&gt;";
        else
        newStr += crntStr[j];

    }

    cout << newStr  << endl; 

    if(i != (size - 1))
    cout <<  "<br>";

}

    return 0;
}

My question is when I write a program of this sort should I be afraid of memory leaks, I compiled this program in devc++ and it was working fine but when I went to visual studio I got the following error: c:\\users\\hamza\\source\\repos\\hypertextformatting\\hypertextformatting\\hypertextformatting.cpp(32): error C2131: expression did not evaluate to a constant c:\\users\\hamza\\source\\repos\\hypertextformatting\\hypertextformatting\\hypertextformatting.cpp(32): note: failure was caused by a read of a variable outside its lifetime c:\\users\\hamza\\source\\repos\\hypertextformatting\\hypertextformatting\\hypertextformatting.cpp(32): note: see usage of 'size' 我的问题是,当我编写此类程序时,我应该担心内存泄漏,我在devc ++中编译了该程序,并且运行良好,但是当我进入Visual Studio时,出现以下错误:c:\\ users \\ hamza \\ source \\ repos \\ hypertextformatting \\ hypertextformatting \\ hypertextformatting.cpp(32):错误C2131:表达式未求值为常数c:\\ users \\ hamza \\ source \\ repos \\ hypertextformatting \\ hypertextformatting \\ hypertextformatting.cpp(32):注意:失败是由于在其生存期之外读取变量而引起的c:\\ users \\ hamza \\ source \\ repos \\ hypertextformatting \\ hypertextformatting \\ hypertextformatting.cpp(32):注意:请参见'size'的用法

Only memory allocation on the heap (or dynamic allocation) can lead to memory leaks. 只有堆上的内存分配(或动态分配)才能导致内存泄漏。 When you declare an array string strArr[size]; 声明数组字符串时,strArr [size]; it will be placed on the stack and will be automatically "released" when program leaves current scope (stack pointer will decrease by strArr size) and desctructor will be called. 它将被放置在堆栈上,并在程序离开当前作用域时自动“释放”(堆栈指针将减小strArr大小)并调用desctructor。 Although the "string" objects data placed in dynamically alocated memory, it will be released by destructors. 尽管“字符串”对象数据放置在动态分配的内存中,但析构函数将释放它。 Memory leak is impossible here. 内存泄漏在这里是不可能的。 You can create a memory leak if you allocate memory in a heap by new, new[], malloc etc. call and forget to release the data after they are no longer needed. 如果通过new,new [],malloc等调用在堆中分配内存,则会造成内存泄漏,而在不再需要它们之后忘记释放数据。

So 所以

string strArr[size]; // no leaks
string* strArr = new string[size]; //may be leak if you forget call delete[] strArr

Besides, variable length arrays is non-standard in C++ avoid using it! 此外,可变长度数组在C ++中是非标准的,请避免使用它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM