删除数组指针时,我的程序没有 return 0
My programs doesn't return 0 when deleting array pointer
当我尝试删除声明为
的数组指针时
short *width = NULL;
width = new short[lenght];
delete[] width;
程序停止在删除部分和returns一个随机数。
这是我的全部程序
int main(){
short **junction = NULL, *width = NULL, length = 0;
ifstream input;
input.open("sample_input.txt");
if(!input)
cout << "No such a file named \"sample_input.txt\"" << endl;
else if(input >> length){
junction = new short*[length];
width = new short[length];
string str;
for(int i = 0; i - 1 < length && getline(input, str); i++){
istringstream ss(str);
width[i - 1] = (str.size() + 1) / 2; //numbers can be two char, so in this case, here we allocate more than necessary memory
junction[i - 1] = new short[width[i - 1]];
for(int j = 0; j < width[i - 1]; j++){
if(ss.eof()){
junction[i - 1][j] = -1; // -1 is the key value of emtpy spaces
continue;
}
ss >> junction[i - 1][j];
}
}
}
input.close();
/*
for(int i = 0; i < length; i++)
delete[] junction[i];
delete[] junction;
delete[] width;
*/
return 0;
}
评论部分有问题,我分别尝试了那些删除语句
只是部分正在运行但不像我预期的那样
这是我的示例输入:
你的代码实际上应该没问题。请向我们展示整个程序。
florian@florian-desktop:~$ cat -n test2.cpp
1 #include <iostream>
2 int main()
3 {
4 const int length = 4;
5 short *width = NULL;
6 width = new short[length];
7 delete[] width;
8 }
florian@florian-desktop:~$ valgrind ./a.out
==22630== Memcheck, a memory error detector
==22630== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22630== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==22630== Command: ./a.out
==22630==
==22630==
==22630== HEAP SUMMARY:
==22630== in use at exit: 0 bytes in 0 blocks
==22630== total heap usage: 2 allocs, 2 frees, 72,712 bytes allocated
==22630==
==22630== All heap blocks were freed -- no leaks are possible
==22630==
==22630== For lists of detected and suppressed errors, rerun with: -s
==22630== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Valgrind 显示所有保留的内存都已正确释放。
--
更新:
我更正了您的完整代码:
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main() {
short **junction = NULL, *width = NULL, length = 12;
ifstream input;
input.open("sample_input.txt");
if(!input)
cout << "No such a file named \"sample_input.txt\"" << endl;
else if(input >> length){
junction = new short*[length];
width = new short[length];
string str;
for(int i = 0; i < length && getline(input, str); i++){
istringstream ss(str);
width[i] = (str.size() + 1) / 2; //numbers can be two char, so in this case, here we allocate more than necessary memory
junction[i] = new short[width[i]];
for(int j = 0; j < width[i]; j++){
if(ss.eof()){
junction[i][j] = -1; // -1 is the key value of emtpy spaces
continue;
}
ss >> junction[i][j];
}
}
}
input.close();
for(int i = 0; i < length; i++)
delete[] junction[i];
delete[] junction;
delete[] width;
return 0;
}
当我尝试删除声明为
的数组指针时short *width = NULL;
width = new short[lenght];
delete[] width;
程序停止在删除部分和returns一个随机数。
这是我的全部程序
int main(){
short **junction = NULL, *width = NULL, length = 0;
ifstream input;
input.open("sample_input.txt");
if(!input)
cout << "No such a file named \"sample_input.txt\"" << endl;
else if(input >> length){
junction = new short*[length];
width = new short[length];
string str;
for(int i = 0; i - 1 < length && getline(input, str); i++){
istringstream ss(str);
width[i - 1] = (str.size() + 1) / 2; //numbers can be two char, so in this case, here we allocate more than necessary memory
junction[i - 1] = new short[width[i - 1]];
for(int j = 0; j < width[i - 1]; j++){
if(ss.eof()){
junction[i - 1][j] = -1; // -1 is the key value of emtpy spaces
continue;
}
ss >> junction[i - 1][j];
}
}
}
input.close();
/*
for(int i = 0; i < length; i++)
delete[] junction[i];
delete[] junction;
delete[] width;
*/
return 0;
}
评论部分有问题,我分别尝试了那些删除语句
只是部分正在运行但不像我预期的那样
这是我的示例输入:
你的代码实际上应该没问题。请向我们展示整个程序。
florian@florian-desktop:~$ cat -n test2.cpp
1 #include <iostream>
2 int main()
3 {
4 const int length = 4;
5 short *width = NULL;
6 width = new short[length];
7 delete[] width;
8 }
florian@florian-desktop:~$ valgrind ./a.out
==22630== Memcheck, a memory error detector
==22630== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22630== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==22630== Command: ./a.out
==22630==
==22630==
==22630== HEAP SUMMARY:
==22630== in use at exit: 0 bytes in 0 blocks
==22630== total heap usage: 2 allocs, 2 frees, 72,712 bytes allocated
==22630==
==22630== All heap blocks were freed -- no leaks are possible
==22630==
==22630== For lists of detected and suppressed errors, rerun with: -s
==22630== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Valgrind 显示所有保留的内存都已正确释放。
-- 更新:
我更正了您的完整代码:
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main() {
short **junction = NULL, *width = NULL, length = 12;
ifstream input;
input.open("sample_input.txt");
if(!input)
cout << "No such a file named \"sample_input.txt\"" << endl;
else if(input >> length){
junction = new short*[length];
width = new short[length];
string str;
for(int i = 0; i < length && getline(input, str); i++){
istringstream ss(str);
width[i] = (str.size() + 1) / 2; //numbers can be two char, so in this case, here we allocate more than necessary memory
junction[i] = new short[width[i]];
for(int j = 0; j < width[i]; j++){
if(ss.eof()){
junction[i][j] = -1; // -1 is the key value of emtpy spaces
continue;
}
ss >> junction[i][j];
}
}
}
input.close();
for(int i = 0; i < length; i++)
delete[] junction[i];
delete[] junction;
delete[] width;
return 0;
}