简体   繁体   English

分配类成员变量时出现C ++分段错误

[英]C++ Segmentation Fault when Assigning Class Member Variable

I have a function "testfunc()" within a class "Test" under a namespace "TEST_NS". 我在名称空间“ TEST_NS”下的“测试”类中有一个函数“ testfunc()”。

In the main program, I've created a pointer to "Test" and would like to point to the "testfunc()" function. 在主程序中,我创建了一个指向“ Test”的指针,并希望指向“ testfunc()”函数。 No problem... 没问题...

I declare a private variable "foo" as a member of "Test", and the "testfunc()" function attempts to change the variable. 我将私有变量“ foo”声明为“ Test”的成员,并且“ testfunc()”函数尝试更改该变量。 This results in a segmentation fault. 这导致分段错误。

Why does this happen? 为什么会这样?


I've created a simple example to show my issue. 我创建了一个简单的示例来展示我的问题。 The segfault happens when I set foo = 1; 当我设置foo = 1;时发生段错误foo = 1; in test.cpp 在test.cpp中

Here are the files: 这些是文件:

main.cpp: main.cpp:

#include <iostream>
#include "test.h"

using namespace std;

int main () {
  TEST_NS::Test * test;
  test->testfunc();
  return 0;
}

test.h: test.h:

#include <iostream>

namespace TEST_NS
{
  class Test
  {

  int foo;

  public:
    Test();
    ~Test();

    void testfunc();
  };
}

test.cpp: test.cpp:

#include <iostream>
#include "test.h"

using namespace TEST_NS;

Test::Test() { }

Test::~Test() { }

void Test::testfunc()
{
  std::cout << "This is testfunc()!" << std::endl;

  foo = 1;

}

The code was compiled with 该代码使用

g++ main.cpp test.cpp -o test 

And run with 并与

./test

You need to instantiate an object of your class; 您需要实例化您的类的对象。 try 尝试

TEST_NS::Test * test = new TEST_NS::Test();
test->testfunc();

Your main() function needs to change; 您的main()函数需要更改; the preferred way to write it is 编写它的首选方式是

int main () {
  TEST_NS::Test test;
  test.testfunc();
  return 0;
}

If you really want to use a pointer (why?), then you should write: 如果您确实要使用指针(为什么?),则应编写:

int main () {
  auto test = new TEST_NS::Test();
  test->testfunc();
  delete test;
  return 0;
}

notice the addition of delete test; 注意添加delete test; . But this is bad practice as it creates the potential for memory leaks, especially as more code is added (or if testfunc() throws an exception). 但这是一种不好的做法,因为它可能导致内存泄漏,尤其是在添加更多代码时(或者如果testfunc()引发异常)。 If you really need to use pointers, it's much better to use std::unique_ptr<> . 如果你真的需要使用指针,这更好使用std::unique_ptr<>

int main () {
  auto test = std::make_unique<TEST_NS::Test>();
  test->testfunc();
  return 0;
}

notice that this also gets rid of the explicit new which is generally a good idea, as well as the delete that had to be added; 请注意,这也摆脱了通常是一个好主意的显式new以及必须添加的delete plus, it's exception-safe. 另外,它是异常安全的。

There is very little reason for "naked" new or delete in modern C++. 在现代C ++中,几乎没有理由“裸体” newdelete Note that you can still get a "raw" pointer by using get() , although dereferencing would be preferred 请注意,您仍然可以使用get()获得“原始”指针,尽管首选取消引用

 void testfunc(TEST_NS::Test& test)
 {
    test.testfunc();
 }
 // ...
 testfunc(*test);

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

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