2015-08-14 73 views
2

我正在使用基本類並嘗試訪問成員函數中的成員字符串數組。我很困惑,因爲字符串數組初始化並沒有任何內存錯誤(博士存儲器)被釋放的時候,如果我只能調用set_wordsprint_words,並del_words功能:從類成員函數中訪問字符串數組

class XWORD { 
    public: 
    int vdir; 
    int len; 
    int wid; 
    int nWords; 
    char ** arr; 
    char ** words;  
    ... 
}; 

void XWORD::set_words(int istart, int inWords, char* iwords[]) { 
    int w = istart; 
    int k = 0; 
    this->nWords = inWords; 

    this->words = new char* [this->nWords]; 
    for (w=istart; w<(istart+this->nWords); w++) { 
     this->words[k] = new char [1+strlen(iwords[w])]; 
     this->words[k] = strcpy(this->words[k], iwords[w]); 
     k++; 
    } 
} 

void XWORD::del_words() { 
    int w = 0; 
    for (w=0; w<(this->nWords); w++) { 
     delete[] this->words[w]; 
    } 
    delete[] this->words; 
} 

void XWORD::print_words() { 
    int w = 0; 
    for (w=0; w<(this->nWords); w++) { 
     printf("\n%s",this->words[w]); 
    } 
    printf("\n"); 
} 

然而,當我調用下面的函數,我得到如下所示的博士內存錯誤(init_arrdel_arr單獨不給錯誤其一):

void XWORD::add_word_to_arr(int iw, int iy, int ix) { 
    int k = 0; 
    int y = iy; 
    int x = ix; 

    for (k=0; k<(int)strlen(this->words[iw]); k++) { 
     this->arr[y][x] = this->words[iw][k]; 
     if (this->vdir) { 
      y++; 
     } else { 
      x++; 
     } 
    } 
} 

int main(int argc, char * argv[]) { 
    XWORD x; 
    x.set_words(1, argc-1, argv); 
    x.init_arr(10,10); 

    x.add_word_to_arr(0, x.len/2, x.wid/2); // WITHOUT THIS, NO ERRORS 
    x.print_words(); 

    x.del_words(); 
    x.del_arr(); 
} 

內存博士錯誤:

c:\MinGW\WORKSPACE\cpp\xword>make runmem 
drmemory -brief -batch bin/test.exe 
~~Dr.M~~ Dr. Memory version 1.8.0 
~~Dr.M~~ Running "bin/test.exe" 
~~Dr.M~~ 
~~Dr.M~~ Error #1: UNADDRESSABLE ACCESS beyond heap bounds: reading 4 byte(s) 
~~Dr.M~~ # 0 XWORD::add_word_to_arr    [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:301] 
~~Dr.M~~ # 1 main         [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:302] 
~~Dr.M~~ Note: refers to 0 byte(s) beyond last valid byte in prior malloc 
~~Dr.M~~ 
~~Dr.M~~ Error #2: UNADDRESSABLE ACCESS: reading 1 byte(s) 
~~Dr.M~~ # 0 replace_strlen      [d:\drmemory_package\drmemory\replace.c:375] 
~~Dr.M~~ # 1 XWORD::add_word_to_arr    [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:301] 
~~Dr.M~~ # 2 main         [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:302] 
~~Dr.M~~ 
~~Dr.M~~ Error #3: LEAK 0 bytes 
~~Dr.M~~ # 0 replace_operator_new_array    [d:\drmemory_package\common\alloc_replace.c:2638] 
~~Dr.M~~ # 1 XWORD::set_words       [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:301] 
~~Dr.M~~ # 2 main          [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:302] 
~~Dr.M~~ 
~~Dr.M~~ Error #4: LEAK 40 direct bytes + 110 indirect bytes 
~~Dr.M~~ # 0 replace_operator_new_array    [d:\drmemory_package\common\alloc_replace.c:2638] 
~~Dr.M~~ # 1 XWORD::init_arr       [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:301] 
~~Dr.M~~ # 2 main          [../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/crt1.c:302] 
~~Dr.M~~ 
~~Dr.M~~ ERRORS FOUND: 
~~Dr.M~~  2 unique,  2 total unaddressable access(es) 
~~Dr.M~~  0 unique,  0 total uninitialized access(es) 
~~Dr.M~~  0 unique,  0 total invalid heap argument(s) 
~~Dr.M~~  0 unique,  0 total GDI usage error(s) 
~~Dr.M~~  0 unique,  0 total handle leak(s) 
~~Dr.M~~  0 unique,  0 total warning(s) 
~~Dr.M~~  2 unique,  2 total, 150 byte(s) of leak(s) 
~~Dr.M~~  0 unique,  0 total,  0 byte(s) of possible leak(s) 
~~Dr.M~~ Details: C:\Users\jesse\AppData\Roaming\Dr. Memory\DrMemory-test.exe.4512.000\results.txt 
~~Dr.M~~ WARNING: application exited with abnormal code 0xc0000005 
make: *** [runmem] Error 5 

另外,當我在add_word_to_arr函數內打印this->words[0]的「%s」時,它是null。但它可以從print_words();打印。

的exe呼叫是:

bin\test.exe a123 b123 

init_arr()是:

void XWORD::init_arr(int ilen, int iwid) { 
    int y = 0; 
    int x = 0; 
    this->len = ilen; 
    this->wid = iwid; 

    this->arr = new char* [this->len]; 
    for (y=0; y<(this->len); y++) { 
     this->arr[y] = new char [1+this->wid]; 
     for (x=0; x<(this->wid); x++) { 
      this->arr[y][x] = BLANK; 
     } 
     this->arr[y][x] = (char) NULL; 
    } 
} 
+2

我建議你使用'std :: vector ',並且只留下手動內存管理。 – NathanOliver

+0

謝謝,我最初會這麼做,但我對2d向量表示法感到困惑 –

+0

您不需要2d向量。 'std :: string'將代替'char *',並將它包裝在'std :: vector'中去掉一個'char *'數組。 – NathanOliver

回答

0

確定init_arr(10,10)初始化尺寸11的10個字符數組的數組(10個字符+ 1個終止空)。

然後,您調用add_word_to_arr(0, 5, 5),以便將第一個單詞(命令行的第一個參數)複製到從位置5,5開始的數組中。所以如果這個詞至少有7個字符與vdir == 0或6與vdir!= 0,你寫過保留數組。我可以用params「foo」和「bar」成功運行,但不能與「feefoobar」運行。

您應該計算最長的單詞的長度,並用它在調用int_arr

不管怎麼說,它看起來您測試vdir值,而不會有任何初始化。這會導致不確定的行爲,因爲你不知道哪個分支(水平或垂直)將緊隨其後,你真的應該重寫add_word_to_arr這樣:

void XWORD::add_word_to_arr(int iw, int iy, int ix) { 
    int k = 0; 
    int y = iy; 
    int x = ix; 

    for (k=0; k<(int)strlen(this->words[iw]); k++) { 
     this->arr[y][x] = this->words[iw][k]; 
     if (this->vdir) { 
      if (++y >= this.len) return; // refuse to write past allocated mem 
     } else { 
      if (++x > this.wid) return; // refuse to write past allocated mem 
     } 
    } 
} 

我強迫VDIR值一次爲0後執行它在調試器下和一次爲1,並且從未訪問過去分配的內存。但我不得不想象一些代碼,因爲你沒有給一個Minimal Compilable Verifiable example

+0

我希望它是那麼簡單...!上面我顯示了makefile中的exe:'bin \ test.exe a123 b123' - 所以最長的單詞只有'4 + null = 5'。事實上,當不使用Dr. Memory時程序運行時沒有錯誤。但是,當我在Dr.M下運行時,會出現錯誤。我懷疑這與公私夥伴有關,甚至可能是Dr.M的錯誤......? –

+0

@JesseKnight在消除剩餘的未定義行爲原因後,我無法複製。看我的編輯 –

0

原來我是從我makefile運行我的記憶博士工具,忘了包括$(ARGS)在我make runmem目標:

FLAGS = -Wall -std=c++11 -pedantic 
LIBS = -Iinclude 
SRC = src/xword.cpp 
TGT = bin/test.exe 
ARGS = human prothean vorcha krogan asari salarian turian batarian quarian 

all: 
    g++ $(SRC) -o $(TGT) $(FLAGS) $(LIBS) 

run: 
    $(TGT) $(ARGS) 

runmem: 
    drmemory -batch $(TGT) **$(ARGS)** # was missing 

這是爲什麼argv在Dr.Memory封裝的執行過程中爲空,以及爲什麼代碼只會在這些過程中崩潰。

在此處將$(ARGS)添加到makefile之後,沒有發生任何碰撞,也沒有報告錯誤/泄漏。

抱歉鵝追逐,並感謝您的幫助 - 請讓我知道如果這是多餘的職位應該被刪除。