简体   繁体   English

尝试输出对象函数时出现C ++分段错误

[英]C++ segmentation fault when trying to output object functions

I am attempting to output functions common to a set of objects that share a base class and I am having some difficulty. 我试图输出一组共享基类的对象共有的函数,但遇到了一些困难。 When the objects are instantiated they are stored in an array and then I am attempting with the following code to execute functionality common to all the objects in this loop: 实例化对象时,将它们存储在数组中,然后尝试使用以下代码执行此循环中所有对象共有的功能:

  if ( truck <= v ) // all types of trucks

vptr is an array of objects and the functions in the loop are common to all the objects. vptr是一个对象数组,循环中的函数对所有对象都是公用的。 The code compiles fine but when I run it I get a segmentation fault when it enters this loop. 代码可以正常编译,但是当我运行它时,进入此循环会遇到分段错误。 I'm fairly confident that the call to the first function in this loop is what is causing the problem. 我完全有信心导致此循环的第一个函数被调用。

this is how I have instantiated the objects in a previous loop: 这就是我在上一个循环中实例化对象的方式:

        vptr[ i ] = new Vehicle( sn, pc );

I should also mention, I'm sorry I forgot to be clear from the beginning, that in this array each object is of a different class. 我还应该提到,很抱歉我从一开始就忘记了这一点,因为在这个数组中,每个对象都属于不同的类。 They all share a base class but they are derived objects of that class. 它们都共享一个基类,但是它们是该类的派生对象。 Sorry for forgetting that probably important piece of information. 很抱歉忘记了那条可能重要的信息。

thanks nmr 谢谢nmr

dynamic_cast to a pointer type returns a null pointer (aka 0 , NULL ) if the object isn't of the specified type. 如果对象不是指定的类型,则dynamic_cast到指针类型将返回空指针(即0NULL )。 You must check the pointer before using it, or use a reference type (which throws an exception on failure instead): 您必须在使用指针之前或使用引用类型( 在失败时引发异常 )检查指针:

if (Truck* p = dyanmic_cast<Truck*>(vptr[i])) {
  // use the pointer here
}
else {
  // vptr[i] doesn't point to a Truck
}

(Notice the nice effect of the correctly-typed pointer being scoped for you, take advantage of this when you can to improve readability.) (请注意,为您确定范围正确的指针的作用是很好的,请在可以提高可读性的情况下利用它。)

If you use a debugger, you can easily figure out which call is causing the segmentation fault and even examine memory to see if you have a null pointer, etc. 如果使用调试器,则可以轻松找出导致分段错误的调用,甚至可以检查内存以查看是否有空指针等。

GCC with GDB is a good example. GDB与GCC就是一个很好的例子。 Try this--first build your application with debugging info (to do this you add the -g switch to your compiler): 尝试执行此操作-首先使用调试信息构建应用程序(为此,请将-g开关添加到编译器中):

gcc -g test.c -o myProgramName

Now launch GDB: 现在启动GDB:

gdb myProgramName

At the first GDB prompt, enter the 'r' command to run the program: 在第一个GDB提示符下,输入“ r”命令以运行程序:

(gdb) r

Then, when you reach the segfault, use the 'bt' command to view the stack trace and find out where the program was when it had the bad memory access: 然后,当到达段错误时,使用“ bt”命令查看堆栈跟踪,并找出程序访问内存错误时所在的位置:

Program received signal SIGSEGV, Segmentation fault.
0x08048380 in testFunction2 () at test.c:17
17      *test = 1;
(gdb) bt
#0  0x08048380 in testFunction2 () at test.c:17
#1  0x0804836e in testFunction1 () at test.c:10
#2  0x0804835a in main () at test.c:5

When you compile with debugging enabled, it lets you see the source file, line number, and the actual code that caused the crash. 在启用调试的情况下进行编译时,它使您可以查看源文件,行号以及导致崩溃的实际代码。 Learning to use a debugger makes hunting down segmentation faults really easy. 学习使用调试器使查找分段错误非常容易。

Debuggers also let you examine memory--with the above example, I can check the value of the 'test' pointer: 调试器还允许您检查内存-通过以上示例,我可以检查'test'指针的值:

(gdb) print test
$1 = 0x0

Whoops, the pointer was NULL when I tried to change the memory it referenced, thus I was accessing memory my program wasn't allowed to touch, causing a segmentation fault. 糟糕,当我尝试更改其所引用的内存时,指针为NULL,因此我正在访问不允许触摸程序的内存,从而导致分段错误。

Try the 'print' command with objects as well--GDB is smart enough to actually break the object down and show you the value of each of the object's members. 还要对对象尝试“打印”命令-GDB足够聪明,可以实际分解对象并向您显示该对象的每个成员的值。 Additionally, you can use the 'printf' command to print strings, etc. while you're debugging your program. 另外,在调试程序时,可以使用'printf'命令打印字符串等。

Hope this is helpful, Maha 希望这会有所帮助,玛哈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM