Question Regarding Pointers

S

someone

Hey All:

I have two pointers A & B pointing towards data objects C & D resp. Now
I want to achieve the following:

1. Free up memory that A points to
2. Have A point towards D
3. Have B point to nothing.

How could this be implemented efficiently?
 
A

Alf P. Steinbach

* someone:
Hey All:

I have two pointers A & B pointing towards data objects C & D resp. Now
I want to achieve the following:

1. Free up memory that A points to
2. Have A point towards D
3. Have B point to nothing.

How could this be implemented efficiently?

Please post minimal example code that compiles.

See the FAQ at
<url: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

Also, please don't post HOMEWORK questions; see the FAQ at
<url: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2>.
 
L

Larry I Smith

someone said:
Hey All:

I have two pointers A & B pointing towards data objects C & D resp. Now
I want to achieve the following:

1. Free up memory that A points to
2. Have A point towards D
3. Have B point to nothing.

How could this be implemented efficiently?

If 'C' was created with 'new':

delete A;

If 'C' was created with 'new[]':

delete[] A;

If 'C' was created with 'malloc' or 'calloc':

free(A);

If 'C' was NOT created via a memory allocation:
(e.g. someclass C; someclass *A = &C;), then there
is no need to free the memory for 'C'. it will be
released when 'C' goes out of scope.

Once 'A' (i.e. the memory used by 'C') is handled
in one of the above ways, then:

A = B;
B = 0;

Larry
 
J

Jim Langston

someone said:
Hey All:

I have two pointers A & B pointing towards data objects C & D resp. Now
I want to achieve the following:

1. Free up memory that A points to

The problem here is who "owns" the data, C or A? So if you had done this:

MyObject* C = new MyObject();
A = C;

it would make more sense to delete C since C "owns" the memory. But, if you
have C not "own" it anymore, perhaps you do:

C = NULL;

Then A is the only one with a pointer to the memory so A "owns" it.
Reguardless, the syntax is:

delete A;
or
delete C;

which frees up the memory A (or C) points to that was allocated without
changing where A (or C) is pointing to, so it points to memory that is no
longer valid.

of course, if the object A points to wasn't allocated with new, you dont'
want to use delete. If it was allocated with malloc, you use free. If it
was dynamically created don't do anything. I.E.

MyObject C;
A = &C;

A points to object C, but you dont' need to free the memory. It will
automatically be freed when C goes out of scope (making A point to invalid
memory).
2. Have A point towards D

if D is a pointer:
A = D;
if D is an instance:
A = &D;

&D means the address of D.
3. Have B point to nothing.

B = NULL;

Some people would argue that this isn't a good idea, some would argue that
it is. It all depends on why you want it to point to nothing. It is safe
to delete a NULL pointer, however.

MyClass* B = new MyClass();
delete B;
delete B; // Error, B points to memory that was already freed.

MyClass* B = new MyClass();
delete B;
B = NULL;
delete B; // Does nothing. Deleting a null pointer is safe and nothing is
actually done.
 
J

Jim Langston

Jim Langston said:
The problem here is who "owns" the data, C or A? So if you had done this:

MyObject* C = new MyObject();
A = C;

it would make more sense to delete C since C "owns" the memory. But, if
you have C not "own" it anymore, perhaps you do:

C = NULL;

Then A is the only one with a pointer to the memory so A "owns" it.
Reguardless, the syntax is:

delete A;
or
delete C;

Just pointing out before anyone else does, if you newed an array they syntax
is

delete[] A;
or
delete[] C;
 
L

Luke Meyers

someone said:
I have two pointers A & B pointing towards data objects C & D resp. Now
I want to achieve the following:

1. Free up memory that A points to
2. Have A point towards D
3. Have B point to nothing.

Shall we assume that you are only giving C and D names for
conversational convenience? If they have actual names in the program
like this, that's suggestive of something wrong with what you're trying
to do. For example:

void f()
{
int C, D;
int * A(&C);
int * B(&D);
delete A; // <-- error!
A = B;
B = NULL;
}

You don't want to delete C (via A) because it's an automatic ("stack")
variable, and will be destroyed automatically when it goes out of
scope. Deleting it yourself causes double-deletion, resulting in
undefined behavior (e.g. SIGSEGV). However, the version below is just
fine:

void f()
{
int * A(new int); // "C"
int * B(new int): // "D"
delete A;
A = B;
B = NULL;
}

Of course, in this example nothing worthwhile happens, but it's just an
example.
How could this be implemented efficiently?

Efficiency isn't a concern here.

Luke
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top