Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 983533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:52:23+00:00 2026-05-16T04:52:23+00:00

In the following code, why does s1.printVal causes a dangling pointer error? Isn’t the

  • 0

In the following code, why does s1.printVal causes a dangling pointer error? Isn’t the s1 object, i.e. its pointer, still accessible until it’s destroyed?

class Sample
{
  public:
    int *ptr;
    Sample(int i)
    {
        ptr = new int(i);
    }

    ~Sample()
    {
        delete ptr;
    }
    void PrintVal()
    {
        cout << "The value is " << *ptr;
    }
};

void SomeFunc(Sample x)
{
    cout << "Say i am in someFunc " << endl;
}

int main()
{
    Sample s1 = 10;
    SomeFunc(s1);
    s1.PrintVal(); // dangling pointer
}

Source

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:52:23+00:00Added an answer on May 16, 2026 at 4:52 am

    The problem here is the copy that is done for argument of the SomeFunc(). That copy de-allocates your pointer when destroyed. You need to also implement a copy constructor, and copy assignment operator. See rule of three.

    Edit:

    Here’s “expanded” pseudo-code, i.e. what the compiler does for you in the main() function:

    // main
    addr0 = grab_stack_space( sizeof( Sample )); // alloc stack space for s1
    Sample::ctor( addr0, 10 );                   // call ctor of Sample
    addr1 = grab_stack_space( sizeof( Sample )); // alloc stack for argument
    Sample::ctor( addr1, addr0 );                // call COPY-ctor of Sample
    SomeFunc( addr1 );                           // call SomeFunc
    Sample::dtor( addr1 );                       // XXX: destruct the copy
    free_stack_space( addr1, sizeof( Sample ));  // free stack taken by copy
    Sample::PrintVal( addr0 );                   // call member func on s1
    Sample::dtor( addr0 );                       // destruct s1
    free_stack_space( addr0, sizeof( Sample ));  // YYY: free stack taken by s1
    

    This is not the exact representation, but a conceptual explanation. It just helps to think in terms of what compiler has to do with your code.

    The pointer member of Sample is delete-ed at the step marked with XXX, and then delete-ed again at the step YYY.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 505k
  • Answers 505k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have you tried the same trick as with the string,… May 16, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer You can use buffer and insert the encPwd as a… May 16, 2026 at 3:28 pm
  • Editorial Team
    Editorial Team added an answer Just to update the solution we went with - eventually… May 16, 2026 at 3:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

The following code does not want to compile. See the included error message. Code:
Following code does NOT work, but it expresses well what I wish to do.
The following code does not compile: public class GenericsTest { public static void main(String[]
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,
The following code does not work scp /home/username/public_html/site/pictures/* myUsernameAtMyMac@myIpAddress:/home/Masi/Desktop/ I have Allow only essential
The following code does not produce an exception but instead passes the value 4
The following code does not run as rootNode is null when retrieved by name
The following code does not compile. int a = 1, b = 2, c
The following code does not set the comment: string userName = yrtre.etre.423369a9-3e57-42da-934d-dae91f87a1e4; MembershipUser user
Following code does compile whereas name aNumber is not declared before use. class A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.