C ++:string = NULL给出了SEGFAULT 11?

时间:2015-07-18 08:37:18

标签: c++

我今天修改了我的项目,允许它将文件保存在不同的文件夹中,我发现我的程序在启动时崩溃了:

Segmentation fault: 11

因为我在测试程序之前引入了很多更改,所以我开始注释掉我添加的所有功能,但没有帮助。我甚至把

cout << "hello world" << endl;
return 0;

作为int main()中的前两行,它仍然没有显示任何内容而崩溃。

最后,我花了一个小时来弄清楚错误。修改包括声明全局变量

string foldername = NULL;

上面的一行似乎是无辜的,它只是声明一个全局变量。

然后我尝试了一个简单的程序:

#include <string>

std::string a = NULL;

int main(){
    return 0;
}

它在启动时也崩溃了。

为什么将字符串全局变量声明为NULL会使程序在没有任何信息的情况下以静默方式崩溃?

2 个答案:

答案 0 :(得分:6)

std::string - 与从C char*字符串继承的相反 - 始终包含有效字符串。它可能是空的,但不能为NULL。如果你尝试用NULL初始化std::string,它会盲目地从NULL地址复制类似C的字符串,这是未定义的行为。

只需使用

std::string a;

然后a将初始化为空字符串。

答案 1 :(得分:1)

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import javax.swing.WindowConstants; public class FrameDemo extends JFrame { private static final long serialVersionUID = 1L; public FrameDemo() { super("Frame Demo"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); getContentPane().add(new CustomPanel(Color.RED), BorderLayout.NORTH); getContentPane().add(new CustomPanel(Color.GREEN), BorderLayout.SOUTH); getContentPane().add(new CustomPanel(Color.BLUE), BorderLayout.EAST); getContentPane().add(new CustomPanel(Color.YELLOW), BorderLayout.WEST); getContentPane().add(new JScrollPane(new CustomPanel(Color.BLACK)), BorderLayout.CENTER); pack(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new FrameDemo(); frame.setVisible(true); } }); } } class CustomPanel extends JPanel implements MouseMotionListener { private static final long serialVersionUID = 1L; private static final Dimension PANEL_SIZE = new Dimension(200, 100); private static final int HAND_CURSOR_INDEX = 1; private static final int DEFAULT_CURSOR_INDEX = 0; private static final Cursor[] _cursors = new Cursor[] { Cursor.getDefaultCursor(), Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) }; // I want to place a JPanel with 300,200 size at 100,50 location in a JFrame private static final Rectangle _rectangle = new Rectangle(50, 10, 40, 50); public CustomPanel(Color color) { setBackground(color); addMouseMotionListener(this); } public Dimension getPreferredSize() { return PANEL_SIZE; } public Dimension getMinimumSize() { return PANEL_SIZE; } public Dimension getMaximumSize() { return PANEL_SIZE; } protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.WHITE); g.fillRect( (int)_rectangle.getX(), (int)_rectangle.getY(), (int)_rectangle.getWidth(), (int)_rectangle.getHeight()); } @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseMoved(MouseEvent e) { int cursorIndex = _rectangle.contains(e.getPoint()) ? HAND_CURSOR_INDEX : DEFAULT_CURSOR_INDEX; setCursor(_cursors[cursorIndex]); } } 仅分配给指针类型,C也分配给C. C ++方式为NULLstd::nullptr_t。声明指向字符串的指针,如

nullptr

或将其留给字符串构造函数

std::string * a = nullptr;
相关问题