bnbfreak
bnbfreak

Reputation: 353

Pass / copy pointer to another pointer

I have a pointer code as follows :

class NsObject : public TclObject, public Handler {
public:
  NsObject();
  virtual ~NsObject();
  virtual void recv(Packet*, Handler* callback = 0) = 0;
  virtual void recv(Packet* p, const char* s);
}

NsObject* uptargetTX;    
NsObject* uptarget_; 

void NsObject::recv(Packet *p, const char*)
{
   Packet::free(p);
}

if (NodeType_ == TX) {
    uptarget_->recv(ppp, (Handler*) 0);
    *uptargetTX = *uptarget_; //in this line error happens
}

I want to pass/copy the pointer uptarget_ to uptargetTX by using *uptargetTX = *uptarget_; but something goes wrong

segmentation fault (core dumped)

Then I change to uptargetTX = uptarget_; but same error occurs. How to remove this error ?

Upvotes: 1

Views: 80

Answers (2)

Marco A.
Marco A.

Reputation: 43662

I have no idea of what your code does but this

NsObject* uptarget_;
uptarget_->recv(ppp, (Handler*) 0); <- dereference the pointer

is wrong in the first place: you need to initialize that pointer to something valid before.

The rest is also wrong for the same reason.

Upvotes: 1

TNA
TNA

Reputation: 2745

uptargetTX and uptarget_ are unininitialized, so they point to random memory locations, so access causes a segmentation fault. Depending on what you are trying to do, you probably have to allocate memory first for example like this: NsObject* uptargetTX = new NsObject

Upvotes: 2

Related Questions