Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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 ?
Posted
Comments
[no name] 28-Jun-12 0:17am    
This question is too general. It requires more information. Perhaps you could post a code sample to improve the question.
Sergey Alexandrovich Kryukov 28-Jun-12 1:25am    
Remove? Crash? Ever head or entropy? :-)
--SA
Sergey Alexandrovich Kryukov 28-Jun-12 1:27am    
OK, this is a totally pointless question. Try to provide proper description of the problem, use "Improve question" above.
--SA
Mohibur Rashid 28-Jun-12 1:41am    
Those rule I putted was not rule. Its some very basic(not advance) to avoid simple mistakes
Sergey Alexandrovich Kryukov 29-Jun-12 18:33pm    
Cannot understand what are you talking about, sorry. You really need to provide essential detail, create and post a code sample, something informative.
--SA

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.
 
Share this answer
 
An owned pointer has to be initialized (1) and validated (2) bofore its usage (3) :)
C++
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900