Git Product home page Git Product logo

Comments (11)

Mooophy avatar Mooophy commented on May 26, 2024

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.

pezy avatar pezy commented on May 26, 2024

@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.

Mooophy avatar Mooophy commented on May 26, 2024

@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.

pezy avatar pezy commented on May 26, 2024
std::cout<< "before delete "<<  *(sp.get())     <<std::endl;

Is there any problem? ❓

from cpp-primer.

Mooophy avatar Mooophy commented on May 26, 2024

@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.

pezy avatar pezy commented on May 26, 2024

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.

Mooophy avatar Mooophy commented on May 26, 2024

Exactly~

from cpp-primer.

Surendra154 avatar Surendra154 commented on May 26, 2024

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.

Mooophy avatar Mooophy commented on May 26, 2024

@Surendra154
Check here : What are POD types in C++?

from cpp-primer.

pezy avatar pezy commented on May 26, 2024

@Surendra154 We called it "built-in type" in CP5.

from cpp-primer.

Surendra154 avatar Surendra154 commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.