我正在使用基本類並嘗試訪問成員函數中的成員字符串數組。我很困惑,因爲字符串數組初始化並沒有任何內存錯誤(博士存儲器)被釋放的時候,如果我只能調用set_words
,print_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_arr
和del_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;
}
}
我建議你使用'std :: vector',並且只留下手動內存管理。 –
NathanOliver
謝謝,我最初會這麼做,但我對2d向量表示法感到困惑 –
您不需要2d向量。 'std :: string'將代替'char *',並將它包裝在'std :: vector'中去掉一個'char *'數組。 – NathanOliver