指针问题

时间:2009-04-19 05:29:27

标签: c++ pointers

任何人都知道如何在多维数组中存储指针?我认为这可能是我主要遇到的问题:

// main.cpp
#ifdef  _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <fstream>
#include <string>
#endif
#include "Word.h"
using namespace std;

const int WORD_SZ = 100;
Word** g_wordArray;
int g_arrSz;

static char filePath[ FILE_PATH_SZ ] = {};
void FreeWordArray();

int main( const int argc, const char **argv )
{
    int 
        wrdCount = 0;
    char 
        usrMenuOption     = 0,
        getFirstLetter    = 0,
        tmpArray[WORD_SZ] = {},
        *getWord = new char;
    string 
        str, 
        str2;
    ifstream 
        inFile, 
        inFile2;
    do 
    {
        cout << "Please make a selection: \n\
a) Read a text file\n\
b) Remove words starting with letter\n\
c) Print words to console\n\
d) Quit\n";
        cin  >> usrMenuOption;
        switch( usrMenuOption )
        {
        case'A':
        case'a':
            cout << "Enter a file name: ";
            cin.sync();
            cin  >> filePath;
            inFile.open( filePath );
            if ( !inFile ) return -1;
            inFile >> tmpArray; // prime the eof flag.
            while ( !inFile.eof() )
            {   
                inFile >> tmpArray;
                wrdCount++;
                g_wordArray = new Word *[wrdCount];

            }
        inFile.close();
        inFile2.open( filePath );
        while( !inFile2.eof()  )
        {   
            inFile2 >> tmpArray;
            // supplies the member functions with information from the file
            g_wordArray[wrdCount] = new Word( tmpArray );
            g_wordArray[wrdCount]->GetFirstLetterLower();
            g_wordArray[wrdCount]->GetWord();
        }
        cout << wrdCount << " Words read from the file " << endl;
        inFile2.close();
        break;
        case'B':
        case'b':
        // information not found returning null
                g_wordArray[wrdCount]->GetFirstLetterLower();
        break;
        case'C':
        case'c':
                g_wordArray[wrdCount]->GetWord();
        break;
        case'D':
        case'd':
        cout << "Quit Requested. " << endl;
        break;
        default:
        cout << '"' << usrMenuOption << '"' << " Not Defined! " << endl;
        }

    } while ( usrMenuOption != 'D' && usrMenuOption != 'd' );


#ifdef _DEBUG
    _CrtDumpMemoryLeaks();
#endif
    cin.ignore();
    return 0;
}

void FreeWordArray()
{
    delete[ ] g_wordArray;
    return;
}


// Word.cpp
#define _CRT_SECURE_NO_WARNINGS // disable warnings for strcpy
#define ARRY_SZ 100
#include <iostream>
#include <fstream>
#include "Word.h"
#include <vector>


using namespace std;

// No paramaters.
// Is this what im missing?
// I just threw it in because of an error.
Word::Word() 
{
}

Word::Word( const char* word )
{
    ptr_ = new char[ strlen( word ) + 1 ];
    strcpy( ptr_, word  ); 
    len_ = strlen( ptr_ );
}

Word::~Word()
{
    delete[ ] ptr_;
    ptr_ = NULL;
    len_ = NULL;
}

char Word::GetFirstLetterLower()
{
    char myChar = tolower( ptr_[0] );
    return myChar;

}

char* Word::GetWord()
{
    Word *objectOne = new Word;
    objectOne->ptr_ = ptr_;
    strcpy( objectOne->ptr_, ptr_ );
    return objectOne->ptr_;
}

我的目标是从g_wordArray[wrdCount]->SomeFunction()中的文件中读取所有单词而不依赖于文件读取循环。

我一直想做的事情:

  • 在实施文件中,getFirstLetterLower下:每次都将char *_ptr私有成员添加到新变量中。例如someCharVar[0] = firstWordsomeCharVar[1] = secondWord ...

  • 将文件内容读入单个变量。在我需要的每种情况下循环使用。

我喜欢这个主意,但还没弄明白怎么做。有什么建议吗?

6 个答案:

答案 0 :(得分:1)

请发布可以重现您问题的最少代码。现在看起来很朦胧。我想这个代码有很多问题

        inFile >> tmpArray;     // prime the eof flag.
        while ( !inFile.eof() )
        {       
                inFile >> tmpArray;
                wrdCount++;
                g_wordArray = new Word *[wrdCount];

        }

你在这里严重泄漏记忆。先前分配的'g_wordArray'会发生什么?

此外,当您分配'n'个元素时,您无法访问'nth'索引。内存占用范围为0 - (n-1)。

g_wordArray[wrdCount]

请重新检查代码,尝试调试然后发布最少的代码。

答案 1 :(得分:1)

IIUC,您正在尝试读取文件,并可选择删除一些以特定字母开头的单词。这是一个简短的例子, ,甚至没有基本的错误处理 ,关于如何使用STL和一些模板实现相同的目的:

#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <iostream>

struct starts_with {
    char mC;
    starts_with(char c) : mC(c) {}
    bool operator()(std::string const& s) { return s[ 0 ] == mC; }
};

// uses commandline parameters for input arguments
// usage:
//    ./test.exe "path/to/file" letter
// 
// (assumes the file to be read is the first such parameter
// the character to be checked for is the second parameter)
int main(int argc, char *argv[ ]) {
    using namespace std;
    vector<string> v;
    ifstream fStm(argv[ 1 ]);
    istream_iterator<string> b(fStm), e;

    remove_copy_if(b, e, back_inserter(v), starts_with(argv[ 2 ][ 0 ]));

    copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));

    return 0;
}

答案 2 :(得分:1)

这是用于读取文件的常见反模式 请注意,在将单词读入tmpArray时,在循环内部检测文件结束(EOF)时,wrdCount始终为一次。

inFile >> tmpArray;     // prime the eof flag.
while ( !inFile.eof() )
{       
    inFile >> tmpArray;
    wrdCount++;
    g_wordArray = new Word *[wrdCount];
}

请改用:

/*
 * The result of the >> operator is a reference to a stream.
 *
 * When a stream is used in a boolean context (like a while loop)
 * it has a cast method that automatically returns an object
 * of a type that can be used as a boolean.
 *
 * The value of the boolean will be true if the file is still valid
 * and false if something went wrong (like reading past the end of file).
 *
 * So the loop will NOT be entered when you read past the EOF but will
 * be entered for every valid word that is read from the file.
 */
while ( inFile >> tmpArray )
{       
    wrdCount++;
    g_wordArray = new Word *[wrdCount];
}

答案 3 :(得分:1)

您对数组的索引总是错误的:

g_wordArray[wrdCount]->GetFirstLetterLower();

这里'wrdCount'是'g_wordArray'中元素的数量,因此生成的'g_wordArray [wrdCount]'正在访问超出数组末尾的元素。

请记住,C / C ++数组从0开始索引,因此具有有效元素0 - &gt; (wrdCount -1)

答案 4 :(得分:0)

你在整个地方都在泄漏记忆 您应该尝试使用STL标准容器来处理这类事情。

Word** g_wordArray;

这可能更好地代表:

std::vector<Word>  g_wordArray;

然后不是像这样为数组分配空间:

g_wordArray = new Word *[wrdCount];
-----
g_wordArray.reserve(wrdCount);

并且恰如其分地添加了新的元素变化:

g_wordArray[wrdCount] = new Word( tmpArray );
g_wordArray[wrdCount]->GetFirstLetterLower();
g_wordArray[wrdCount]->GetWord();

------

g_wordArray.push_back(Word( tmpArray ));
g_wordArray[wrdCount].GetFirstLetterLower();    // Note we use . not ->
g_wordArray[wrdCount].GetWord();

现在你的代码不再使用任何讨厌的新操作符,因此你不会泄漏任何内存。

答案 5 :(得分:-2)

你的问题究竟是什么?你在哪里得到错误?

您是否考虑过缩短main()并将您的工作放在其他功能上?这段代码很难阅读。 另外,do ... while循环确实很难使用,请考虑切换到while循环。你需要某种布尔值,但为了便于阅读它是值得的。