Hi ALL.
Basic Question.
sample:
class MyClass
{
....
MyClass(int* int_p_) : (int_p_ = p_int_) ;
private:
int* p_int_;
};
=============== ===========
//somewhere on system; I have
int real_value; // may be = 100
//so after I instance a MyClass;
MyClass mc_(&real_value );
//and deleted it
delete mc_;
//the real_value will also be deleted.and leave me a hang prt.I don't want
that.
//any suggestion? Use auto_prt or reference?
//what's the right expression?
//Sorry ,new c++er.
//Thank you very much. 7 1722
key9 wrote: int real_value; // may be = 100
//so after I instance a MyClass;
MyClass mc_(&real_value );
You are creating mc_ on the stack here, so you can't delete it. Did you
mean:
MyClass* mc_ = new MyClass(&real_v alue)
//and deleted it
delete mc_;
//the real_value will also be deleted.and leave me a hang prt.I don't want that.
Deleting a class containing a pointer does not delete the object
pointed to, unless you explicitly do so in a destructor. So shouldn't
have a problem with a dangling pointer here. The problem would arise if
you did something like this:
MyClass some_function()
{
int real_value;
mc = MyClass(&real_v alue);
return mc; // oops - real_value goes out of scope at the end of
the
// function, so the pointer in mc is no
longer valid
}
* key9: sample:
class MyClass { ...
MyClass(int* int_p_) : (int_p_ = p_int_) ;
private: int* p_int_; };
=============== ===========
//somewhere on system; I have
int real_value; // may be = 100
//so after I instance a MyClass;
MyClass mc_(&real_value );
//and deleted it
delete mc_;
That will not compile.
'mc_' will be destroyed when the program execution exits the scope it's
declared in, or if it is a namespace scope variable, when the program
execution leaves 'main'.
//the real_value will also be deleted.
No.
and leave me a hang prt.
No.
I don't want that. //any suggestion? Use auto_prt or reference? //what's the right expression?
Right expression for what?
--
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? "I V" <wr******@gmail .com>
Deleting a class containing a pointer does not delete the object pointed to, unless you explicitly do so in a destructor. So shouldn't have a problem with a dangling pointer here. ...
OK I know where is my problem.
I tested in 2 general int point and assume that delete point in a class
will delete "object" as a matter of course .
thank you very much.
"Alf P. Steinbach" <al***@start.no >
: Right expression for what?
a class container a pointer ,when delete instance of that class, will the
object that pointer pointed to also be deleted?
and I've find the answer. It will NOT.
also thanks a lot ^_^
BTW:
After known these,confuse again:
MyClass
{
public:
MyClass(){
p_i_ = new int();
}
private:
int* p_i_;
};
MyClass* mc_ = new MyClass();
delete mc_;
Is that means
p_i_ occupied a memory block sample "0x43214321 "
will not be freed after delete mc_?
So I have to write code on dtor, or use some tech like auto_prt?
OK I consider just now
Is that means concept "scope" I wroted:
What ever you created on a "scope"
{
};
after it finished, it will delete whatever created on this scope.
If you did not write code manually, these "objects" pointed,referen ced ...
which not belonged to this scope will not be deleted?
YES/NO?
thank you very much.
key9 wrote: BTW: After known these,confuse again:
MyClass { public: MyClass(){ p_i_ = new int(); } private: int* p_i_;
};
MyClass* mc_ = new MyClass(); delete mc_;
Is that means p_i_ occupied a memory block sample "0x43214321 "
I'm not sure what you mean,
will not be freed after delete mc_?
The memory allocated by p_i_ = new int() will not be freed.
So I have to write code on dtor, or use some tech like auto_prt?
You could use an auto_ptr, or call delete in the destructor.
Either way, you will need to also provide a copy-constructor and
assignment operator for MyClass. (start by declaring them private and
not defining them to prevent copying of the class).
Ben Pope
--
I'm not just a number. To many, I'm known as a string... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: NKOBAYE027 |
last post by:
FIRST POST
Hi All: I'm trying to write a simple specialization before moving on to
something a bit more complex - always a good idea in my case, at least. :o)
I'm trying to adapt the example from Stroustrup, 3rd ed., The C++
Programming Language p. 344
I'm using MSDev 6.0 in case that's an issue. Here's the source...
|
by: bo |
last post by:
And why and where one should use one vs. the other?
Verbally, it seems like semantics to me--but obviously there is some
actual difference that makes references different and or preferable
over pointers in some cases...
TIA
|
by: key9 |
last post by:
Hi All
simple question:
1.====================================
{
int* p_1 = new int();
};
//will p1 leak?
|
by: toton |
last post by:
HI,
One more small doubt from today's mail.
I have certain function which returns a pointer (sometimes a const
pointer from a const member function).
And certain member function needs reference (or better a const
reference).
for eg,
const PointRange* points = cc.points(ptAligned);
|
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...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |