构造函数中的分段错误

Lar*_*art 2 c++ header-files memory-segmentation

我尝试在C++中创建的每个类都会发生这种情况.从java迁移,我发现问题主要在于创建类.我运行valgrind并且它在构造函数中,它似乎是.

==30214== Memcheck, a memory error detector
==30214== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==30214== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==30214== Command: ./CoC
==30214== 
==30214== 
==30214== Process terminating with default action of signal 11 (SIGSEGV)
==30214==  Bad permissions for mapped region at address 0x404B4F
==30214==    at 0x4C2B9EC: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30214==    by 0x404220: Model::Model(std::string) (in /home/kronus/Apollo/CoC)
==30214==    by 0x402617: main (in /home/kronus/Apollo/CoC)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在尝试将此模型类的构造函数调用到main方法中.这是构造函数的代码

Model::Model(std::string filename)
{
m_TotalFaces = 0;
m_model = lib3ds_file_load(filename.c_str());
    // If loading the model failed, we throw an exception
    if(!m_model)
    {
           throw strcat("Unable to load ", filename.c_str());
    }
}
Run Code Online (Sandbox Code Playgroud)

当它被调用时,它会因分段错误而关闭.重要提示:我已在头文件中声明了该类.这是我收到错误的时候.我把类放在源文件中,运行正常.我究竟做错了什么?

aut*_*tic 9

strcat尝试在第一个参数指向的字符串末尾写入第二个参数指向的字符串.由于第一个参数是字符串文字(应该被认为是只读),因此会出现令人讨厌的段错误.

我建议学习C++,好像它是一种与Java 完全不同的语言,因为否则你可能会认为类似的功能是相同的.这很危险.猴子可以通过在键盘上捣碎它来学习Java.C++具有未定义的行为,可能看起来在您的机器上正常运行,但在另一个上发射核导弹.