470,864 Members | 2,005 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,864 developers. It's quick & easy.

Question Regarding Pointers

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?

May 25 '06 #1
8 1548
I wanted to know the syntax.

May 25 '06 #2
* 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>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 25 '06 #3
someone wrote:
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
May 25 '06 #4

"someone" <bo***********@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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.
How could this be implemented efficiently?

May 25 '06 #5

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:lz*************@fe07.lga...

"someone" <bo***********@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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;


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

delete[] A;
or
delete[] C;
May 25 '06 #6
1. Free up memory that A points to
delete A;
2. Have A point towards D
A = &D;
3. Have B point to nothing.


B = 0;
-Tomás
May 25 '06 #7
Thanks everyone. That helps.

May 26 '06 #8
someone wrote:
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

May 26 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by regisser | last post: by
20 posts views Thread by __PPS__ | last post: by
7 posts views Thread by rs | last post: by
9 posts views Thread by Alfonso Morra | last post: by
2 posts views Thread by akshayrao | last post: by
21 posts views Thread by Bo Yang | last post: by
2 posts views Thread by Renji Panicker | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.