gpt4 book ai didi

c++ - 从类成员函数访问字符串数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:49:20 25 4
gpt4 key购买 nike

我正在使用一个基本类并尝试在成员函数中访问成员字符串数组。我很困惑,因为如果我只调用 set_wordsprint_wordsdel_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");
}

但是,当我调用以下函数时,我得到如下所示的 Dr. 内存错误(单独的 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;
}
}

最佳答案

确定 init_arr(10,10) 初始化一个大小为 11 的 10 个 char 数组(10 个字符 + 1 个以 null 结尾)。

然后,调用 add_word_to_arr(0, 5, 5),将第一个单词(命令行的第一个参数)复制到从位置 5,5 开始的数组中。因此,如果该单词至少有 7 个字符且 vdir == 0 或 6 个字符且 vdir != 0 则您写过保留数组。我可以使用参数“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

关于c++ - 从类成员函数访问字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32016124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com