Connecting Tech Pros Worldwide Help | Site Map

Question Regarding Pointers

someone
Guest
 
Posts: n/a
#1: May 25 '06
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?

someone
Guest
 
Posts: n/a
#2: May 25 '06

re: Question Regarding Pointers


I wanted to know the syntax.

Alf P. Steinbach
Guest
 
Posts: n/a
#3: May 25 '06

re: Question Regarding Pointers


* someone:[color=blue]
> 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?[/color]

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?
Larry I Smith
Guest
 
Posts: n/a
#4: May 25 '06

re: Question Regarding Pointers


someone wrote:[color=blue]
> 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?
>[/color]

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
Jim Langston
Guest
 
Posts: n/a
#5: May 25 '06

re: Question Regarding Pointers



"someone" <born.2be.wyld@gmail.com> wrote in message
news:1148525967.176010.143400@g10g2000cwb.googlegr oups.com...[color=blue]
> 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[/color]

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).
[color=blue]
> 2. Have A point towards D[/color]

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

&D means the address of D.
[color=blue]
> 3. Have B point to nothing.[/color]

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.
[color=blue]
> How could this be implemented efficiently?[/color]


Jim Langston
Guest
 
Posts: n/a
#6: May 25 '06

re: Question Regarding Pointers



"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:lz9dg.122$z_.45@fe07.lga...[color=blue]
>
> "someone" <born.2be.wyld@gmail.com> wrote in message
> news:1148525967.176010.143400@g10g2000cwb.googlegr oups.com...[color=green]
>> 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[/color]
>
> 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;[/color]

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

delete[] A;
or
delete[] C;


Tomás
Guest
 
Posts: n/a
#7: May 25 '06

re: Question Regarding Pointers


[color=blue]
> 1. Free up memory that A points to[/color]

delete A;
[color=blue]
> 2. Have A point towards D[/color]

A = &D;
[color=blue]
> 3. Have B point to nothing.[/color]

B = 0;


-Tomás
someone
Guest
 
Posts: n/a
#8: May 26 '06

re: Question Regarding Pointers


Thanks everyone. That helps.

Luke Meyers
Guest
 
Posts: n/a
#9: May 26 '06

re: Question Regarding Pointers


someone wrote:[color=blue]
> 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.[/color]

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.
[color=blue]
> How could this be implemented efficiently?[/color]

Efficiency isn't a concern here.

Luke

Closed Thread