debug醒目:new delete实现出现溢出

DraculaW 2004-11-15 09:53:41
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

const int INITIAL_LIST_CAPACITY = 10;

void build_wordlist (string* word_list, int capacity, string filename)
{

ifstream words;
string word;
int size = 0;

// Allocate the space for the word list
word_list = new string[INITIAL_LIST_CAPACITY];
capacity = INITIAL_LIST_CAPACITY;

// Open the file
words.open (filename.c_str());

if (!words)
{
cerr << "Could not open " << filename << endl;
exit(EXIT_FAILURE);
}

// Loop to read in words
words >> word;
while (! words.eof())
{

// If there isn't enough space, grow the array and copy the contents
if (size > (capacity-1))
{

// Create another array that is twice as large as the word_list
string new_word_list = *(new string[2 * capacity]);

// Copy the words from the word_list into the new array
for (int index=0; (index < capacity); index++)
{
new_word_list = word_list[index];
}

delete word_list;
* word_list = new_word_list;

capacity *=2;
}

// Save the word
word_list[size] = word;
size++;

// Read in next word
words >> word;
}

words.close();
}

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

// An array of string objects to store our
// word list.
string *word_list=NULL;
int capacity = 0;

// Read the word list file
build_wordlist (word_list, capacity, "words.txt");

// Print out the words
for (int index=0; (index < capacity) && (word_list[index] != "");
index++)
{
cout << word_list[index] << endl;
}

return EXIT_SUCCESS;
}

在同一目录下有一个word文件里面包含有二十多个单词

运行后出现溢出错误
...全文
142 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
DraculaW 2004-11-15
  • 打赏
  • 举报
回复
不好意思哈


我刚才下课了
hongjun_han 2004-11-15
  • 打赏
  • 举报
回复
老大 你还不揭帖,难道还不满意吗???
你这种人我看没救了,以后还想让我给你回帖吗???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????///////////////////////////////////////////////?????????????????????????????????????????
hongjun_han 2004-11-15
  • 打赏
  • 举报
回复
这个你看 没问题了吧,没问题就揭帖吧
hongjun_han 2004-11-15
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdlib>


using namespace std;

const int INITIAL_LIST_CAPACITY = 10;
/*
string readString(const ifstream& words)
{
char ch=0x00;
string str = "";

while(ch!=0x0a)
{
words>>ch;
str += ch;
}
return str;
}

*/
void* build_wordlist (string* word_list, int& capacity, string filename)
{

ifstream words;
string word;
int size = 0;

// Allocate the space for the word list
word_list = new string[INITIAL_LIST_CAPACITY];
capacity = INITIAL_LIST_CAPACITY;

// Open the file
words.open (filename.c_str());

if (!words)
{
cerr << "Could not open " << filename << endl;
exit(EXIT_FAILURE);
}

// Loop to read in words
words >> word;
while (! words.eof())
{

// If there isn't enough space, grow the array and copy the contents
if (size > (capacity-1))
{

// Create another array that is twice as large as the word_list
//string new_word_list = *(new string[2 * capacity]);
string* new_word_list = new string[2 * capacity];
if (new_word_list == NULL)
{
cerr << " error " << endl;
system("pause");
}
// Copy the words from the word_list into the new array
for (int index=0; (index < capacity); index++)
{
new_word_list[index] = word_list[index];
}


delete[] word_list;
word_list = new_word_list;
new_word_list = NULL;

capacity *=2;
}

// Save the word
word_list[size] = word;
size++;

// Read in next word
words >> word;
capacity = size;
// word = readString(words);
}

words.close();

return word_list;
}

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

// An array of string objects to store our
// word list.
string *word_list= new string[10];
int capacity = 10;

// Read the word list file
word_list = (string*)build_wordlist (word_list, capacity, "words.txt");
cout << "cpacity is " << capacity << endl;

// Print out the words
for(int i=0;i<capacity;i++){

cout << word_list[i] << endl;
}

system("pause");

return EXIT_SUCCESS;
}
快结帖
xuzheng318 2004-11-15
  • 打赏
  • 举报
回复
用delete []word_list;
DraculaW 2004-11-15
  • 打赏
  • 举报
回复
但是words里面是:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
DraculaW 2004-11-15
  • 打赏
  • 举报
回复
谢谢六楼

不过输出好像不够哦

输出的是:
capacity is 40
/*然后是
23个*/
one
hongjun_han 2004-11-15
  • 打赏
  • 举报
回复
把这个
for (int index=0; (index < capacity) && (word_list[index] != "");
index++)
{
cout << *word_list << endl;
}
换成
cout << *word_list << endl;
hongjun_han 2004-11-15
  • 打赏
  • 举报
回复
试试这个

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdlib>


using namespace std;

const int INITIAL_LIST_CAPACITY = 10;

void* build_wordlist (string* word_list, int& capacity, string filename)
{

ifstream words;
string word;
int size = 0;

// Allocate the space for the word list
word_list = new string[INITIAL_LIST_CAPACITY];
capacity = INITIAL_LIST_CAPACITY;

// Open the file
words.open (filename.c_str());

if (!words)
{
cerr << "Could not open " << filename << endl;
exit(EXIT_FAILURE);
}

// Loop to read in words
words >> word;
while (! words.eof())
{

// If there isn't enough space, grow the array and copy the contents
if (size > (capacity-1))
{

// Create another array that is twice as large as the word_list
//string new_word_list = *(new string[2 * capacity]);
string* new_word_list = new string[2 * capacity];
if (new_word_list == NULL)
{
cerr << " error " << endl;
system("pause");
}
// Copy the words from the word_list into the new array
for (int index=0; (index < capacity); index++)
{
new_word_list[index] = word_list[index];
}

delete[] word_list;
word_list = new_word_list;
new_word_list = NULL;

capacity *=2;
}

// Save the word
word_list[size] = word;
size++;

// Read in next word
words >> word;
}

words.close();
return word_list;
}

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

// An array of string objects to store our
// word list.
string *word_list=NULL;
int capacity = 10;

// Read the word list file
word_list = (string*)build_wordlist (word_list, capacity, "words.txt");
cout << "cpacity is " << capacity << endl;
// Print out the words
for (int index=0; (index < capacity) && (word_list[index] != "");
index++)
{
cout << *word_list << endl;
}

system("pause");

return EXIT_SUCCESS;
}
DraculaW 2004-11-15
  • 打赏
  • 举报
回复
不是啊


这个是实验课作业

要求debug来的
goodluckyxl 2004-11-15
  • 打赏
  • 举报
回复
void build_wordlist (string*& word_list//引用, int& capacity,//引用 string filename)
//引用否则不能修改内容
hongjun_han 2004-11-15
  • 打赏
  • 举报
回复
build_wordlist (word_list, capacity, "words.txt"); 不能实现内存分配,
建议使用返回值
另外像 string new_word_list = *(new string[2 * capacity]); 这种语句也太搞笑了吧。

另外capacity的改变不会返回给 主程序,不知你是怎么运行的。
毛病太多了
建议用 Vector 重写一个。
yjh1982 2004-11-15
  • 打赏
  • 举报
回复
delete word_list;
改用delete []word_list;
oo 2004-11-15
  • 打赏
  • 举报
回复
发多个一样的帖子,小心被咔嚓掉

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧