指针问题的问题;

时间:2014-01-31 15:46:13

标签: 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();
};

2 个答案:

答案 0 :(得分:2)

单词是双指针。我看到你使用

new char[]

初始化初始指针,但是你还需要新建第二层。就像这样。

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;

答案 1 :(得分:1)

使用类似shared_ptr的东西可能有助于解决内存管理问题(正确删除指针/释放内存)。这可能是一个更安全的选择,因为管理将是动态的。这里有关于他们的简短文章:

http://en.cppreference.com/w/cpp/memory/shared_ptr