newbie on pointers

T

twomers

A few things:

Your variable a is not a pointer so you can't use new with it.

However, your variable is a global so it is never out of scope and
should always be valid.

If you have a few classes, functions etc using it it is possible that
one might inadvertently delete[] x's contents and you could run into
problems where your classes are trying to access information that is
no longer accessible. Does your class A have a destructor that does
anything to x? Or B for that matter.
 
T

twomers

On the point of a not being a pointer, you could force scope around it
by doing this (instead of deleting)

int main( void )
{
// ...

{ // Start of scope. a exists
A a;
// ...
} // a doesn't exist after this point

// ...

return 0;
}
 
A

Alf P. Steinbach

* Henrietta Denoue:
Hi,

What happens to a global pointer that is to be used in various
other objects, is instantiated in one object, then this second
object is deleted ? Is the pointer still valid ? like pseudo-code
---------------------------
// my global pointer, not a member of Classes A or B
X *x;

class A{
....
x = new X();

... do something with x

}


Class B {
...
do something with x;

}


int main()
{
A a;
B b;

a = new A();
....
delete a;

Technically yes.

But don't do that. It's like consolidating the firm's bank accounts
into one big account completely accessible to every employee, with no
tracing of who has done what. Imagine how long that firm would last.

If instances of A and B need to communicate or share things then first
consider function arguments. Then there is a host of different
techniques that can be used for the cases where function arguments don't
cut it. If or when you find that a global (single, common, shared,
completely open bank account) is the only possible solution to the
requirements, then it's time to start questioning the requirements.
 
D

Daniel T.

Henrietta Denoue said:
What happens to a global pointer that is to be used in various
other objects, is instantiated in one object, then this second
object is deleted ? Is the pointer still valid ? like pseudo-code
---------------------------
// my global pointer, not a member of Classes A or B
X *x;

class A{
....
x = new X();

... do something with x

}


Class B {
...
do something with x;

}


int main()
{
A a;
B b;

a = new A();
....
delete a;

If the thing that 'x' points to is not explicitly deleted, then it is
still valid even if the thing that created it has been destroyed. The
exact answer to your question cannot be determined by the pseudo-code
posted, too many errors.
 
H

Henrietta Denoue

Hi,

What happens to a global pointer that is to be used in various
other objects, is instantiated in one object, then this second
object is deleted ? Is the pointer still valid ? like pseudo-code
---------------------------
// my global pointer, not a member of Classes A or B
X *x;

class A{
.....
x = new X();

... do something with x

}


Class B {
....
do something with x;

}


int main()
{
A a;
B b;

a = new A();
.....
delete a;

---------------

My question is : Is x still valid in B evenif A is deleted ?

Thanks,
H.
 
T

twomers

ITYM " ... should always be /visible/". As you reason yourself below,
it is not always "valid".

Actually I didn't. I was typing as I was thinking, and the thought of
delete[]-ing the pointer didn't occur to me until the reasoning I came
up with afterwards, and I didn't proof read it either.

You are, of course, right. That's what I _should_ have said.
 
A

Anand Hariharan

twomers said:
A few things:

It took a lot of persuasion for Google to quote the post that one
replies to. People persuaded Google for a reason -- it provides people
who see your post, of some context. Please do not remove that context.

Your variable a is not a pointer so you can't use new with it.

However, your variable is a global so it is never out of scope and
should always be valid.

ITYM " ... should always be /visible/". As you reason yourself below,
it is not always "valid".
If you have a few classes, functions etc using it it is possible that
one might inadvertently delete[] x's contents and you could run into
problems where your classes are trying to access information that is
no longer accessible. Does your class A have a destructor that does
anything to x? Or B for that matter.
 
J

Jim Langston

Henrietta Denoue said:
Hi,

What happens to a global pointer that is to be used in various
other objects, is instantiated in one object, then this second
object is deleted ? Is the pointer still valid ? like pseudo-code
---------------------------
// my global pointer, not a member of Classes A or B
X *x;

class A{
....
x = new X();

... do something with x

As long as that something doesn't include delete x
}


Class B {
...
do something with x;

}


int main()
{
A a;
B b;

a = new A();
....
delete a;

When you call new you are returned a memory address where that instance
resides. Having that pointer and what type it is is all you need to get
access to the object, until it is deleted. It doesn't matter where new is
called, as long as you have the address.

So the answer to your question is: yes, b (not B) is still valid after a
(not A) is deleted, as long as a never deleted the pointer itself. As long
as a didn't delete the pointer in it's destructor or other methods.

Now, the use of globals is a bad thing, because it causes confusion as to
when something is created/deleted without looking at the classes themselves.
If you need a class to create a new object (such as a factory class) then
the method that creates the object should return the pointer, which you
would store. Modifying your code a little bit (may contain errors, not
tested):

class A
{
public:
X* Factory() const { return new X(); }
};

class B
{
// Class B needs to get the pointer. A few ways to get it.
// If the created object is going to live for the lifetime of
// the program, it could be passed in the constructor and
// copied. If it's just going to be used by a method, then
// it should be passed as a parm. I'll attempt to show
// both ways.
public:
B( X* x ): Xp( x ) {}
void Foo() { // We can use Xp here which was copied in the construtor }
void Bar( X* x ) { // We can use x here, which was passed as a parm }
private:
X* Xp;
};

int main()
{
A a;
X* MyX = a.Factory();

B b( MyX );
b.Foo(); // Uses pointer passed in constructor
b.Bar( MyX ); // Uses pointer we just passed

delete MyX; // Important to only do this once
};

As you can see, all we need is to get a copy of the ponter and use it
somewhere. It remains valid until delete is called, even if whatever
created it goes out of scope or is deleted itself, as long as delete is not
called on that specific pointer somewhere.
 
J

Jim Langston

Henrietta Denoue said:
Thanks guys for the answer,
and sorry for the typo, I mean 'a' and 'b' as pointers, just
forgot the '*'.
For some reason function argument does not work. 'A' and 'B' have
of course destructors but do not touch 'x' in their destructors.
What 'A' does with x (x is an object with its own pointers
to other objects and arrays) is to process some data and fill in some
arrays inside 'x'. 'x' is actually a pointer to a an object of a class
that reads a file of certain format. The filename is transferred to
it through a gui. If the user change the file, of course 'x' is deleted
inside 'A' and a new 'x' is instantiated. But when I am finished with
'a' (deleted), the contents of 'x' is not what is
supposed to be. Therefore I gave up the idea of
using function arguments (I sent the address of
'x' to 'a' as an argument to a fuction).
So I switched to a shared /global variable. It seems to work but I
am not comfortable wit the idea. I would have preferred to do it
as I did first but it just didn't work.

If it did not work passing it as a parameter, then something is wrong.
Using it as a global could just be hiding (even temporarily) the problem.
It sounds to me like class A may be newing something things, storing them to
class X, then when class A is destroyed it deletes what it created, making
X's pointers invalid. Or, somewhere you are overflowing memory.

Either way, it has to be fixed. Take a good look at both A and B and what
they are doing to pointers, make sure that anything newed in A or B and
assigned to X isn't being deleted accidently or something. Without seeing
code it's hard to say.

If you can't find the error, try posting the code here, if small, or to some
pasting bin and giving a link and maybe we can figure it out. It should be
fixed, however. Rearranging things in memory (such as chaning from a parm
to a global) making them work almost always means you have memory problems
somewhere and actually didn't fix the problem, just hid it.
 
H

Henrietta Denoue

Thanks guys for the answer,
and sorry for the typo, I mean 'a' and 'b' as pointers, just
forgot the '*'.
For some reason function argument does not work. 'A' and 'B' have
of course destructors but do not touch 'x' in their destructors.
What 'A' does with x (x is an object with its own pointers
to other objects and arrays) is to process some data and fill in some
arrays inside 'x'. 'x' is actually a pointer to a an object of a class
that reads a file of certain format. The filename is transferred to
it through a gui. If the user change the file, of course 'x' is deleted
inside 'A' and a new 'x' is instantiated. But when I am finished with
'a' (deleted), the contents of 'x' is not what is
supposed to be. Therefore I gave up the idea of
using function arguments (I sent the address of
'x' to 'a' as an argument to a fuction).
So I switched to a shared /global variable. It seems to work but I
am not comfortable wit the idea. I would have preferred to do it
as I did first but it just didn't work.
H.
 
H

Henrietta Denoue

Many thanks for the answer Jim,

I will try to implement your suggestion and hopefully if I don't
make some hundred other errors in the process thing will work :)

Thanks again

H.
 
T

twomers

Actually, if you want to simulate the whole global thing, you could
use references which are available to you in C++:




class A
{
private:
int &i;

public:
A( int &n_i )
: i( n_i ) {}

void print ( void )
{
std::cout<< "A: " << i << '\n';
}
};

class B
{
private:
int &i;

public:
B( int &n_i )
: i( n_i ) {}

void print ( void )
{
std::cout<< "B: " << i << '\n';
}
};

int main( void )
{
int i = 0;

A a( i );
B b( i );

a.print();
b.print();

i = 42;

a.print();
b.print();

return 0;
}



Have you ever come across references before?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top