我正在使用基本类并尝试访问成员函数中的成员字符串数组。我很困惑,因为字符串数组初始化并没有任何内存错误(博士存储器)被释放的时候,如果我只能调用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