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 7650341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:13:37+00:00 2026-05-31T11:13:37+00:00

Given the code : class Sample { public: int *ptr; Sample(int i) { ptr

  • 0

Given the code :

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();
 }

The output is :

Say i am in someFunc
Null pointer assignment(Run-time error)

I’m not able to understand why the second line of output comes. The second line of the output. I think that the compiler supplies a copy constructor when it is not explicitly specified. So, in the function SomeFunc(Sample x), the local object to SomeFunc() which is X of Sample type should be created and destroyed and the Sample type object (s1) in main() should remain intact and should be freed only after main exits. Please answer why the above behaviour is happening?

  • 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-31T11:13:39+00:00Added an answer on May 31, 2026 at 11:13 am

    Why the above behaviour is happening??

    Short Answer:
    Because You are not following the Rule of Three.

    Long Answer:
    Your class has a pointer member ptr with dynamic memory allocation in constructor and deallocation in destructor while your code creates temporary copies of the object while passing to the function SomeFunc() by calling copy constructor, implicitly generated by the compiler, which creates a shallow copy of the pointer member. Once the temporary is destroyed at the end of function call the memory is deallocated in destructor and you are left with a dangling pointer.This invalid pointer is dereferenced further when you call the function PrintVal() resulting in a Undefined Behavior, which manifests in the form of a segmentation fault.

    How to avoid this problem?

    Short Answer:
    Follow the Rule of Three.

    Long Answer:
    You should provide a copy constructor which creates a deep copy of the pointer member ptr. This ensures that the pointer member of the object created in member remains valid through out the lifetime of the program.

    EDIT:
    Actually, the problem might even occur even before the function gets called, specifically when you call:

    Sample s1= 10;
    

    This calls the conversion constructor,

     Sample(int i)
    

    to create a temporary Sample object which is then used for constructing s1 object by calling the implicit copy constructor, If this is the case the temporary object created will be destroyed after creation of s1 does leaving the pointer member ptr in a dangling state.

    However, most of the compilers will apply optimizations through copy elision using Return Value Optimization(RVO) thus eliminating the need of calling the copy constructor & hence this might not be a problem.

    In either case solution to the problem remains the same.

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

Sidebar

Related Questions

Given the code below: class Sample { public static void Run() { int i
Given the following code sample: public class WeirdStuff { public static int doSomething() {
Given the sample code: class Base { public: bool pub; protected: bool prot; };
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
In c#, Given the following code: public class Person { public int PersonID {
Given this code: public class Car { public virtual int CarId { get; set;
Given 2 classes: ... class Grades{ public: Grades(int numExams) : _numExams(numExams){ _grdArr = new
Problem One friend suggested an interesting problem. Given the following code: public class OuterClass
Given is the following code: import java.io.Serializable; import java.util.concurrent.locks.ReentrantLock; class Data { private int
Given this code sample: complex.h : #ifndef COMPLEX_H #define COMPLEX_H #include <iostream> class Complex

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.