Comments (11)
Hi~ @Surendra154 Good to see you.
Apparently , the code you pasted doesn't compile. I modified it as this:
#include <memory>
#include <iostream>
int main()
{
{
auto sp = std::make_shared<int>(42);
auto p = sp.get();
std::cout<< "before delete "<< *(sp.get()) <<std::endl;
delete p;
std::cout<< "after delete " << sp.use_count() <<std::endl;
std::cout<< "after delete " << *sp <<std::endl;
}
return 0;
}
Run it on Ubuntu 14.04 LTS
:
before delete 42
*** Error in `/home/alan/codesNprojs/just_test_cpp/build-just_test_cpp-Desktop_gcc-Debug/just_test_cpp': double free or corruption (out): 0x099bc018 ***
Let's talk about your issue:
why here dereferencing sp not giving me any error??
If you were referring to compiling time, AFAIK no compiler complains such error.
For run time, dereferencing a deleted object could cause run time crash, but it depends on the object type being dereferenced as well as the context like architecture and operating system .
Your code were trying to dereference a POD type. Ordinarily, such case would not crash. If dereferencing a class type, things are different. You can try this :
#include <iostream>
#include <string>
int main()
{
auto p = new std::string{"hello"};
delete p;
std::cout << *p << std::endl;
std::cout << "\nexit\n";
return 0;
}
from cpp-primer.
@Mooophy I can not agree with you.
Try this code:
#include <iostream>
int main()
{
auto p = new int(42);
std::cout << "before delete:" << *p << std::endl;
delete p;
return 0;
}
It would not crash too. The reason of crash is not the POD type. delete
after a call to get()
of std::share_ptr
is the real reason. The smart pointer still has a copy and will call delete when it releases the object.
You can use reset()
of std::share_ptr
to dereference. Try this way:
#include <iostream>
#include <memory>
int main()
{
auto sp = std::make_shared<int>(42);
auto p = sp.get();
std::cout << "before delete:" << *p << std::endl;
sp.reset();
std::cout << "after delete: " << sp.use_count() << std::endl;
std::cout << "after delete: " << *p << std::endl;
return 0;
}
There isn't run error anymore.
Reference: share_ptr::reset
from cpp-primer.
@pezy
You are absolutly right, but you didn't get what I was trying to say.
In his code, there are two places that could cause run time crash.The first one is dereferencing a deleted std::shared_ptr<int>
. as following:
std::cout<< "after delete " << *sp <<std::endl;
//! ^^^
The second one is the double delete
. As you pointed out, this crash has nothing to do with POD.But his question is not about double delete
.
He's asking:
why here deferencing sp not giving me any error??
Accessing memory through a deleted pointer is undefined behavior . Ordinarily, when this pointer was pointing to a POD type, what we get is garbage rather than a run time crash.
from cpp-primer.
std::cout<< "before delete "<< *(sp.get()) <<std::endl;
Is there any problem? ❓
from cpp-primer.
@pezy I pasted the wrong statement, reload this page. I should have pasted the statement :
std::cout<< "after delete " << *sp <<std::endl;
//! ^^^
from cpp-primer.
maybe you mean that:
std::cout << *sp << std::endl;
I know your point already. Let's wait for @Surendra154 to say something.
from cpp-primer.
Exactly~
from cpp-primer.
sorry i was unable to reply .
and thanks for the reply
can you tell me what is POD??
i was unable to get it .
from cpp-primer.
@Surendra154
Check here : What are POD types in C++?
from cpp-primer.
@Surendra154 We called it "built-in type" in CP5.
from cpp-primer.
thanks @pezy
On Fri, Nov 14, 2014 at 6:10 AM, pezy [email protected] wrote:
@Surendra154 https://github.com/Surendra154 We called it "built-in
type" in CP5.—
Reply to this email directly or view it on GitHub
#29 (comment).
from cpp-primer.
Related Issues (20)
- exercise12.8 memory leakage reason?
- Got this issue
- Typo in exercise 15.34 / 15.36
- Typo in exercise 16.36
- Cpp primer answer
- Access to private member
- Cannot understand exercise 9.52, please help. HOT 1
- ex13.44 about size and length function HOT 1
- D HOT 1
- ex2.5 notation vs type? HOT 1
- Spelling error in "Exercise 4.7" HOT 1
- Spelling error in Exercise 4.14 HOT 1
- Spelling error in Exercise 4.28 HOT 1
- "Include" error in ex15.4.5.6
- Cpp
- Ch 16 - some further reading for ex65 and 66
- A issue made by mistake.
- Ex 14.28 problem
- Ex13.42 HOT 1
- Exercise 10_32.cc (G++ giving error on the accumulate algorithm)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpp-primer.