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? 8 1693
I wanted to know the syntax.
* 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.c om/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.c om/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?
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
"someone" <bo***********@ gmail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.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?
"Jim Langston" <ta*******@rock etmail.com> wrote in message
news:lz******** *****@fe07.lga. .. "someone" <bo***********@ gmail.com> wrote in message news:11******** **************@ g10g2000cwb.goo glegroups.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; 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
Thanks everyone. That helps.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: regisser |
last post by:
I have a quastion for the C++ professionals and members of the C++ standartization commetee.
As i know C++ standart requires an allocator to be a templated parameter in every STL container.
Besides that one can pass reference to allocatior into every STL container. This raises the following questions:
1. Containers that were templatized with different allocators are considered different types and thus cannot be assigned to each other....
|
by: Squignibbler |
last post by:
Hi all,
I have a question regarding the C++ programming language
regarding the nature of the relationship between pointers and arrays.
If the statement MyArray is functionally identical to
*(MyArray+x), what statement is functionally identical to
MyArray?
I ask this question because when I create a dynamic array with one
|
by: __PPS__ |
last post by:
Hello everybody
in a quiz I had a question about dangling pointer:
"What a dangling pointer is and the danger of using it"
My answer was:
"dangling pointer is a pointer that points to some area of ram that's
not reserved by the program. Accessing memory through such pointer is
likely to result in core dump (memory access violation)"
|
by: rs |
last post by:
Just out of idle curiosity:
1)Regarding pointers; I don't have the standard/grammar with me, but T* for
type T mean pointer-to-T. Does that mean that with a declaration of type
T** is actually a pointer to a type pointer, T*** is a pointer to type
pointer to type pointer and so on?
2) Is there any history behind the use of the "*" operator in providing
dereferencing capabilities or is it arbitrary? How did B handle pointers?
(if it...
|
by: Alfonso Morra |
last post by:
Hi,
I am having some probs with copying memory blocks around (part of a
messaging library) and I would like some confirmation to make sure that
I'm going about things the right way.
I have some data types defined thus:
typedef enum {
ONE ,
| |
by: akshayrao |
last post by:
could someone point me in the right direction regarding this question??
http://groups.google.com/group/programpara/browse_thread/thread/75b58c897f890930
thanks.
|
by: Pravesh |
last post by:
Hello All,
I had some query regarding virtual functions/destructors. If a class is
having some/all of its methods that are virtual then is it recommended
that it should also have virtual destructor?
When I am defining such a class with default destructor then my
compiler is giving warning that class XXX has virtual functions but
non-virtual destructor.
|
by: Bo Yang |
last post by:
As long as I write c++ code, I am worry about the
pointer. The more the system is, the dangerous the pointer
is I think.
I must pass pointers erverywhere, and is there a way to
ensure that every object pointered by any pointer will
be deleted and only be deleted once?
|
by: Renji Panicker |
last post by:
I have a question regarding references. Consider the following code:
<code>
#include <iostream>
class A
{
int _a1;
int _a2;
int _a3;
};
|
by: Ben Bacarisse |
last post by:
candide <toto@free.frwrites:
These two statements are very different. The first one is just wrong
and I am pretty sure you did not mean to suggest that. There is no
object in C that is the same as its address.
The second one simply depends on a term that is not well-defined.
Most people consider the type to be an important part of the notion of
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |