c++ - 指针问题 char **words;

标签 c++ pointers

所以我要删除函数 resetLine() 中的指针,然后在函数 breakLine() 中分配它们。我得到一个核心转储,所以我想我要么删除了错误的指针,要么试图在程序中的某处使用空指针。虽然我自己看不出问题出在哪里。非常感谢您的帮助!

#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;
}

主要

#include"online.h"

int main()
{
    string sCheck = "";
  //2. create a OneLine object
  OneLine obj;

  //1. create an ifstream for the file test.txt (please use this file for testing)
  ifstream inData;

  inData.open("test.txt");

  if(!inData)
    {
      cout << "Problem opening test.txt" << endl;
      return 1;
    }

  while(!inData.eof())
  {

      //3.  while you can still read from the file: 
      //a. call readLine
      obj.readLine(inData);

      if(!inData)   //This line exits the loop when eof is reached. This needs to be here since you don't want to pass eof just to other functions
          break;    

      //b. call breakLine
      obj.breakLine();

      //c. call printReverse

      obj.printReverse();
  }
  //4. close the file
  inData.close();


  return 0;
}

头文件

#include <string>
#include<cstring>
#include<iostream>
#include<fstream>
using namespace std;

class OneLine
{
   public:
      OneLine();
      ~OneLine();
      void breakLine();
      void printReverse();
      istream &readLine(istream& is);
      string returnLine();
   private:
      string oneLine;
      char **words;
      int wordCount;
      void resetLine();
};

最佳答案

Words 是一个双指针。我看到你在使用

new char[]

初始化初始指针,但是接下来还需要new第二层。所以像这样。

char** words
words = new char*[10];
for(int i = 0; i < 10; ++i)
{
    words[i] = new char[10];
}

这将创建 10 个字符串,每个字符串包含 10 个字符。

然后在最后:

for(int i = 0; i < 10; ++i)
{
    delete words[i];
}
delete []words;

关于c++ - 指针问题 char **words;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21484102/

相关文章:

c++ - 保留然后分配给 std::string

c - 当用 %d 打印 int 指针时打印出什么?

android - Qt- 从 Android 资源添加自定义 XML

c - C中的指针N链表

c - getc 函数产生段错误

c - 为什么不能将 `char **` 传递给在 C 中采用 `const char **` 的函数?

c++ - C++-如果找不到帐号,则显示错误消息

c++ - operator>>(,) 重载以一种令人惊讶的方式表现

c++ - 为什么在 C++ 中使用 "*(*(array + i) + j)"而不是 "array[i][j]"?

c++ - 错误,符号 'vector' 无法解析