C++ 指针字符**单词的问题;
因此,我在函数resetLine()中删除指针,然后在函数breakLine()中分配它们。我得到了一个内核转储,所以我想我要么删除了错误的指针,要么试图在程序中的某个地方使用空指针。虽然我自己看不出有什么问题。非常感谢您的帮助C++ 指针字符**单词的问题;,c++,pointers,C++,Pointers,因此,我在函数resetLine()中删除指针,然后在函数breakLine()中分配它们。我得到了一个内核转储,所以我想我要么删除了错误的指针,要么试图在程序中的某个地方使用空指针。虽然我自己看不出有什么问题。非常感谢您的帮助 #include"online.h" //Public Funcions----------------------------------------------------- //creator function OneLine::OneLine() { o
#include"online.h"
//Public Funcions-----------------------------------------------------
//creator function
OneLine::OneLine()
{
oneLine = "";
wordCount = 0;
}
//Destructor
OneLine::~OneLine()
{
//if allocation has occurred than free up the words
if(wordCount > 0)
{
delete [] words;
}
}
istream& OneLine::readLine (istream& is)
{
//call resetLine to free up memory and reset oneLine and wordCount to empty string and zero respectively
char test[12] = "HEYTHERE";
resetLine();
//read one line from is (in this case, a file stream) and store it in oneLine
do
{
if(!getline(is, oneLine)) // if eof is reached
return is;
}while(oneLine.empty()); //check for empty lines
//return is
return is;
}
void OneLine::breakLine()
{
//create a temporary C String version of oneLine
char cString[800]; //There's a huge string in the file
char *pToken;
char *pToken2;
char cTemp[50];
//store a count of the number of words in wordCount
int nCount = 0;
int i = 1; //words[0] will already be filled with a word
strcpy(cString, oneLine.c_str()); //make a cString copy of a C++ string
//use strtok to break the temporary line into words
//allocate enough space to hold all of the words and store them in words
pToken = strtok(cString, " ");
while((pToken=strtok(NULL, " "))!= NULL) //find how many words
{
nCount++;
}
strcpy(cString, oneLine.c_str()); //make a cString copy of a C++ string
pToken2 = strtok(cString, " ");
words = new char *[nCount]; //allocate enough space for words
strcpy(cTemp,pToken2);
words[0] = strdup(cTemp);
//free(words[0]);
while((pToken2=strtok(NULL, " "))!= NULL) //find how many words
{
strcpy(cTemp,pToken2);
words[i] = strdup(cTemp);
// free(words[i]);//This line was in the lab material but is causinerrors on my version of emacs
i++;
}
//update wordCount
wordCount = nCount;
}
void OneLine::printReverse()
{
for(int i=(wordCount); i>=0; i--)
{
cout << " " << words[i];
}
cout << endl;
}
string OneLine::returnLine()
{
return oneLine;
}
//Private Functions------------------------------------------------------
void OneLine::resetLine()
{
//set oneLine to be an empty string
oneLine = "";
//set wordCount to zero
//if allocation has occurred than free up the words
if(wordCount > 0)
{
delete[] words;
}
wordCount = 0;
}
#包括“online.h”
//公共职能-----------------------------------------------------
//创造者函数
OneLine::OneLine()
{
oneLine=“”;
字数=0;
}
//析构函数
OneLine::~OneLine()
{
//如果发生分配,则释放单词
如果(字数>0)
{
删除[]字;
}
}
istream&OneLine::readLine(istream&is)
{
//调用resetLine释放内存,并将oneLine和wordCount分别重置为空字符串和零
字符测试[12]=“HEYTHERE”;
resetLine();
//从is(在本例中为文件流)中读取一行,并将其存储在一行中
做
{
if(!getline(is,oneLine))//如果达到eof
回报是;
}while(oneLine.empty());//检查是否有空行
//回报是
回报是;
}
void OneLine::特征线()
{
//创建oneLine的临时C字符串版本
char cString[800];//文件中有一个巨大的字符串
查普顿;
char*pToken2;
char-cTemp[50];
//在wordCount中存储字数计数
int nCount=0;
int i=1;//单词[0]将已用单词填充
StrcPy(cString,OnLetry.CyString());/ /制作C++字符串的cstring拷贝
//使用strtok将临时行拆分为单词
//分配足够的空间容纳所有单词并将它们存储在单词中
pToken=strtok(cString,“”);
while((pToken=strtok(NULL,“”)!=NULL)//查找有多少个单词
{
nCount++;
}
StrcPy(cString,OnLetry.CyString());/ /制作C++字符串的cstring拷贝
pToken2=strtok(cString,“”);
words=新字符*[nCount];//为单词分配足够的空间
strcpy(cTemp,pToken2);
字[0]=strdup(cTemp);
//免费(字[0]);
while((pToken2=strtok(NULL,“”)!=NULL)//查找有多少个单词
{
strcpy(cTemp,pToken2);
字[i]=strdup(cTemp);
//free(words[i]);//这一行在实验室材料中,但在我的emacs版本中是因果关系
i++;
}
//更新字数
wordCount=n计数;
}
void OneLine::printReverse()
{
对于(inti=(字数);i>=0;i--)
{
cout单词是双指针。我看到你在使用
new char[]
初始化初始指针,但是你还需要更新第二层
char** words
words = new char*[10];
for(int i = 0; i < 10; ++i)
{
words[i] = new char[10];
}
char**words
单词=新字符*[10];
对于(int i=0;i<10;++i)
{
单词[i]=新字符[10];
}
这将创建10个字符串,每个字符串中包含10个字符
最后:
for(int i = 0; i < 10; ++i)
{
delete words[i];
}
delete []words;
for(int i=0;i<10;++i)
{
删除文字[i];
}
删除[]字;
单词是双指针。我看到你在使用
new char[]
初始化初始指针,但是你还需要更新第二层
char** words
words = new char*[10];
for(int i = 0; i < 10; ++i)
{
words[i] = new char[10];
}
char**words
单词=新字符*[10];
对于(int i=0;i<10;++i)
{
单词[i]=新字符[10];
}
这将创建10个字符串,每个字符串中包含10个字符
最后:
for(int i = 0; i < 10; ++i)
{
delete words[i];
}
delete []words;
for(int i=0;i<10;++i)
{
删除文字[i];
}
删除[]字;
单词是双指针。我看到你在使用
new char[]
初始化初始指针,但是你还需要更新第二层
char** words
words = new char*[10];
for(int i = 0; i < 10; ++i)
{
words[i] = new char[10];
}
char**words
单词=新字符*[10];
对于(int i=0;i<10;++i)
{
单词[i]=新字符[10];
}
这将创建10个字符串,每个字符串中包含10个字符
最后:
for(int i = 0; i < 10; ++i)
{
delete words[i];
}
delete []words;
for(int i=0;i<10;++i)
{
删除文字[i];
}
删除[]字;
单词是双指针。我看到你在使用
new char[]
初始化初始指针,但是你还需要更新第二层
char** words
words = new char*[10];
for(int i = 0; i < 10; ++i)
{
words[i] = new char[10];
}
char**words
单词=新字符*[10];
对于(int i=0;i<10;++i)
{
单词[i]=新字符[10];
}
这将创建10个字符串,每个字符串中包含10个字符
最后:
for(int i = 0; i < 10; ++i)
{
delete words[i];
}
delete []words;
for(int i=0;i<10;++i)
{
删除文字[i];
}
删除[]字;
使用诸如共享\u ptr之类的工具可能有助于解决您的内存管理问题(正确删除指针/释放内存)。这对您来说可能是一个更安全的选择,因为管理将是动态的。此处为您提供有关这些工具的简短文章:
使用诸如共享ptr之类的工具可能有助于解决内存管理问题(正确删除指针/释放内存)。这对您来说可能是一个更安全的选择,因为管理将是动态的。此处为您提供有关这些工具的简短文章:
使用诸如共享ptr之类的工具可能有助于解决内存管理问题(正确删除指针/释放内存)。这对您来说可能是一个更安全的选择,因为管理将是动态的。此处为您提供有关这些工具的简短文章:
使用诸如共享ptr之类的工具可能有助于解决内存管理问题(正确删除指针/释放内存)。这对您来说可能是一个更安全的选择,因为管理将是动态的。此处为您提供有关这些工具的简短文章:
<> P> < /> P>你在调试器中已经通过了代码吗?为什么你在C++程序中使用C字符串和C库函数?一般使用<代码> STD::String 和C++习惯用法,你会省下很多的悲伤。我是一个视频游戏程序员,C++中的C字符串一直都用C。尝试尽可能地小而快地保存代码,这是业界的普遍做法。不过,C字符串和IoFipe的组合是不寻常的。您是否已经在调试器中通过了代码?为什么在C++程序中使用C++字符串和C库函数?你会为自己节省很多的悲伤。我是一个电子游戏程序员,我一直使用C++中的C字符串。