Jump to content
  • Advertisement
Sign in to follow this  
giugio

[c++]pointers of pointers

This topic is 3177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hy. Nowi post the code: #include "stdafx.h" #include "A.h" #include "B.h" #include <string> using namespace std; main
int _tmain(int argc, _TCHAR* argv[])
{
	CA *c =new CA();
	c->processA();
	CB* data = c->GetCB();
	CC** cx = data->m_mappa[0];
	
	CC* ce = * cx;
	wstring str = ce->GetName();
	
	ce = * cx;
	str = ce->GetName();
	
	
	return 0;
}
A.h
#pragma once
#include "B.h"
class CA
{
public:
	CA(void);
	~CA(void);
	void processA();
	CB* GetCB();
private:
	CB *m_b;
};
A.cpp
#include "StdAfx.h"
#include ".\a.h"

CA::CA(void)
{
}

CA::~CA(void)
{
}

void CA::processA()
{
	CC* c= new CC();
	CC* cx= new CC();
	c->SetName(L"giugio");
	cx->SetName(L"pippo");
	m_b = new CB();
	m_b->processaB(&c);
	m_b->processaB(&cx);
}

CB* CA::GetCB()
{
	return m_b;

}
B.h
#pragma once
#include "C.h"
#include <map>

using namespace std;

class CB
{
public:
	CB(void);
	~CB(void);
	void processaB(CC ** pPointer);
	map<int, CC**>m_mappa;
};
B.cpp
#include "StdAfx.h"
#include ".\b.h"

CB::CB(void)
{
}

CB::~CB(void)
{
}

void CB::processaB(CC** pC)
{
	m_mappa[m_mappa.size()] = pC;
	
}
C.h
#pragma once
#include <string>

using namespace std;
class CC
{
public:
	CC(void);
	~CC(void);
	void SetName(wstring str);
	wstring GetName();
private:
	wstring m_name;
};
C.cpp
#include "StdAfx.h"
#include ".\c.h"

CC::CC(void)
{
}

CC::~CC(void)
{
}

wstring CC::GetName()
{
	return m_name;
}

void CC::SetName(wstring str)
{
	m_name = str;
}
where are the errors?because in main the CC* ce = * cx; have a bad pointer?(0Xccccccc)?and not he "giugio" and "pippo" values? I'm not understand good pointers of pointers? (i must use it for other explanations). Grazie

Share this post


Link to post
Share on other sites
Advertisement
This looks very complicated and I have no clue what it's supposed to do, but I managed to spot one error:

In CA::processA you pass the addresses of c and cx to CB::processaB, which than puts them into a map (which looks like it should be a vector anyway, but that's unrelated). Since c and cx are on the stack, they disappear, once the CA::processA is over, hence their addresses, which have been stored in the map are invalid.

The key to understanding pointers to pointers is to forget that pointers are special data types, because they aren't. It's basically an int which happens to store an address instead of an arbitrary number.

So you have only have to make one thing:
When you store an address (no matter of what type, pointer or non-pointer) in a pointer, make sure it will still a valid address once it is used. A good way for beginners is to only store the addresses of heap allocated objects (things that were created with "new").

Lastly, pointers to pointers are very rarely useful in C++. It's preferable to use references to pointers instead.

Share this post


Link to post
Share on other sites
Since c and cx are on the stack, they disappear, once the CA::processA is over, hence their addresses, which have been stored in the map are invalid.

How i can don't disappear the two pointers?and why they disappear ?
I have created they in the heap with the keyword new!
sorry for my english.

Share this post


Link to post
Share on other sites
Quote:
Original post by giugio
How i can don't disappear the two pointers?and why they disappear ?
I have created they in the heap with the keyword new!


Here's a simplified example of what's going on:

{ // scope starts
int * p = NULL; //pointer pointing to NULL declared on stack
{ // another scope starts
int i; // variable declared on stack
p = &i;
} // scope ends, i is gone
*p = 4711; // invalid statement because i no longer exists (no compiler error tough)
}



Now, just replace the type int with int*:

{ // scope starts
int ** p = NULL; //pointer pointing to NULL declared on stack
{ // another scope starts
int* i; // variable declared on stack
i = new int; // we need something to point it to
p = &i;
} // scope ends, i is gone
**p = 4711; // invalid statement because i no longer exists (no compiler error tough)
delete *p; // heap allocated objects need to be cleaned up manually
}



Same thing!

The only way to have things not disappear when their scope ends is to not put them on the stack, but somewhere else. The usual way to put it is the heap, with new.

The simple example:

{ // scope starts
int * p = NULL; //pointer pointing to NULL declared on stack
{ // another scope starts
int * i = new int; // variable declared on stack
p = i;
} // scope ends, i (containing the address) is gone, but the memory allocated with new isn't
*p = 4711; // valid statement! Hooray!
delete p; // heap allocated objects need to be cleaned up manually
}



Aaand with int* instead of int:

{ // scope starts
int ** p = NULL; //pointer pointing to NULL declared on stack
{ // another scope starts
int ** i = new int*; // variable declared on stack
int * value = new int; // we need something to point it to
*i = value;
p = i;
} // scope ends, i (containing the address) is gone, but the memory allocated with new isn't
**p = 4711; // valid statement! Hooray!
delete *p; // heap allocated objects need to be cleaned up manually
delete p; // two news - two deletes
}



As I said before, pointers to pointers are rarely useful.

I hope I didn't make any mistakes with my examples, I haven't tested them...

Finally, to fix your problem you'd have to change processA to:

void CA::processA()
{
CC* c= new CC();
CC* cx= new CC();
c->SetName(L"giugio");
cx->SetName(L"pippo");
m_b = new CB();

CC* pc= new CC*;
pc = &c;
CC* pcx= new CC*;
pcx = &cx;

m_b->processaB(pc);
m_b->processaB(pcx);

}



Note: Cleanup code is missing, as it was in your original code.



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!