c++ - Problems with pointers char **words; -


so i'm deleting pointers in functionr resetline(), , assigning them in function breakline(). i'm getting core dump, think i'm either deleting pointers wrong, or trying use null pointer somewhere in program. although can't see problem myself. appreciated!

#include"online.h"  //public funcions-----------------------------------------------------  //creator function oneline::oneline() {   oneline = "";   wordcount = 0; }  //destructor oneline::~oneline() {   //if allocation has occurred free words   if(wordcount > 0)     {       delete [] words;     } }  istream& oneline::readline (istream& is) {   //call resetline free memory , reset oneline , wordcount empty string , 0 respectively    char test[12] = "heythere";   resetline();   //read 1 line (in case, file stream) , store in oneline     {      if(!getline(is, oneline)) // if eof reached         return is;   }while(oneline.empty()); //check empty lines   //return   return is; }  void oneline::breakline() {   //create temporary c string version of oneline   char cstring[800]; //there's huge string in file   char *ptoken;   char *ptoken2;   char ctemp[50];   //store count of number of words in wordcount   int ncount = 0;   int = 1; //words[0] filled word    strcpy(cstring, oneline.c_str()); //make cstring copy of c++ string    //use strtok break temporary line words   //allocate enough space hold of words , store them in words   ptoken = strtok(cstring, " ");   while((ptoken=strtok(null, " "))!= null) //find how many words  {         ncount++;  }     strcpy(cstring, oneline.c_str()); //make cstring copy of c++ string     ptoken2 = strtok(cstring, " ");      words = new char *[ncount]; //allocate enough space 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 in lab material causinerrors on 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 empty string   oneline = "";   //set wordcount 0      //if allocation has occurred free words  if(wordcount > 0)    {          delete[] words;    }    wordcount = 0; } 

main

#include"online.h"  int main() {     string scheck = "";   //2. create oneline object   oneline obj;    //1. create ifstream file test.txt (please use file testing)   ifstream indata;    indata.open("test.txt");    if(!indata)     {       cout << "problem opening test.txt" << endl;       return 1;     }    while(!indata.eof())   {        //3.  while can still read file:        //a. call readline       obj.readline(indata);        if(!indata)   //this line exits loop when eof reached. needs here since don't want pass eof other functions           break;            //b. call breakline       obj.breakline();        //c. call printreverse        obj.printreverse();   }   //4. close file   indata.close();     return 0; } 

header file

#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 double pointer. see using

new char[] 

to initialize initial pointer, need new second layer. like.

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

this create 10 strings, each 10 characters in it.

then @ end:

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

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -