小编dth*_*guy的帖子

缓冲区溢出?

我试图将char *的向量转换为char指针的数组,但遇到此烦人的错误,无法弄清楚自己在做什么错。

char** Parse::toCommand(std::vector<char*> command) {
    char** array = new char* [command.size()];
    for (int i = 0; i < command.size(); i++) {
        array[i] = command[i];
    }

    return array;
}
Run Code Online (Sandbox Code Playgroud)

我收到此警告,导致我的程序无法运行。

 Buffer overrun while writing to 'array':  the writable size is 'command.public: unsigned int __thiscall std::vector<char *,class std::allocator<char *> >::size(void)const ()*4' bytes, but '8' bytes might be written.
Run Code Online (Sandbox Code Playgroud)

char *当然是一个交流字符串。

向量中的字符串是使用strtok_s分割的一部分字符串。我通过使用string :: copy()将每个转换为ac str以获得非常量c字符串,并使用std :: string的构造函数获取常规字符串来摆脱每个字符串末尾的Null。然后,我弹出后背以摆脱零位。

我的最终目标是我想要一个c字符串数组,以便可以将其传递给execvp()

for (int i = 0; i < exes.size(); i++) {  //Separate each executable and …
Run Code Online (Sandbox Code Playgroud)

c++ memory

7
推荐指数
1
解决办法
110
查看次数

不了解此返回类型?

我正在阅读老师给我的一些代码,但我不太了解其中一行特定的代码。该函数返回一个int&。

return (*(Vector *)this)[i];
Run Code Online (Sandbox Code Playgroud)

此return语句的运算符重载为“ []”。[]的另一个操作符重载在“ this”的基类中定义。基类是已定义的类“ Vector”。我不明白这行代码。

c++ inheritance class

0
推荐指数
1
解决办法
101
查看次数

标签 统计

c++ ×2

class ×1

inheritance ×1

memory ×1