如何清除由于指针错误而导致的崩溃? [英] how to remove crash due to bad pointer?

查看:100
本文介绍了如何清除由于指针错误而导致的崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,总是有一些变量会因崩溃而变坏并变成错误的指针.
我该如何解决呢?

Hi, in my code there are variables which always lead to crash because they get corrupted and become bad pointers.
How to i resolve this ?

推荐答案

解决这一问题的一些方法是:

-不要写进程不归您所有的内存
-确保不要超出缓冲区末尾
-初始化所有内容
-使用标准算法访问集合

-不要手动管理内存,这是近来的杯子游戏
-使用std :: vector,std :: shared_ptr和std :: unique_ptr代替原始指针

基本上不要违反语言的规则或习惯用法,就可以了.
Some ways of resolving this one are:

- don''t to write to memory that your process doesn''t own by
- making sure you don''t write beyond the end of buffers
- initialising everything
- use standard algorithms to access collections

- don''t manually manage memory, it''s a mugs game these days
- use std::vector, std::shared_ptr and std::unique_ptr instead of raw pointers

Basically don''t break the rules or idioms of the language and you''ll be okay.


在使用前必须对拥有的指针进行初始化(1)和验证(2) (3):)
An owned pointer has to be initialized (1) and validated (2) bofore its usage (3) :)
class B
{
public:
  void f();
};

class A
{
  B* m_pcB;

public:
  A() : m_pcB(NULL /*1*/) {}
  //..

  void UseB() { if (m_pcB /*2*/) m_pcB->f(/*3*/); }
  void ResetB() { if (m_pcB /*2*/) { delete m_pcB /*3*/; m_pcB = NULL /*1*/} }
  //..
}


所有(拥有和未拥有的)指针都应在使用前进行验证.


All (owned and not owned) pointers should be validated before their usage.


这篇关于如何清除由于指针错误而导致的崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆