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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:13:54+00:00 2026-05-26T05:13:54+00:00

I know C, but I’m not good at C++. The following code will crash

  • 0

I know C, but I’m not good at C++.

The following code will crash (In getval(), using reference as a parameter is ok).
And value of *p is changed after first cout statement. It looks there is some overwriting caused by out of bound of memory.

My question is why it crashed (or why its value is changed).
It’s ‘call by value’ of object, so should it work anyway?

class myclass { 
  int *p; 

  public: 
    myclass(int i); 
    ~myclass() { delete p; } 
    int getval(myclass o); 
}; 

myclass::myclass(int i) 
{ 
  p = new int; 

  if (!p) { 
    cout << "Allocation error\n"; 
    exit(1); 
  } 

  *p = i; 
}

int myclass::getval(myclass o) 
{ 
  return *o.p; 
} 

int main() 
{ 
  myclass a(1), b(2); 

  cout << a.getval(a) << " " << a.getval(b) << endl; 
  cout << b.getval(a) << " " << b.getval(b) << endl; 

  return 0; 
} 
  • 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-26T05:13:55+00:00Added an answer on May 26, 2026 at 5:13 am

    My question is why it crashed (or why its value is changed).

    This is a very common problem of shallow copy and double delete. Compiler picks up the default copy constructor and both a.p and o.p are pointing to same memory location. When both objects invoke their destructor, the delete p; statement is executed twice. Freeing the same memory multiple times is an undefined behavior and it results in crash in your system.

    It’s ‘call by value’ of object, so should it work anyway?

    If properly coded, then yes it would work. Make a deep copy of p‘s content and then it should work. However, it’s better to pass by reference till it’s possible.

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

Sidebar

Related Questions

i dont know but this not working for me im getting garbege value when
I don't know why but for some reason I am not able to refer
I know XHTML CSS but PHP knowledge is not much and i want to
Ok really easy I know but not for me... I can not figure this
Just curious (maybe this is an implementation detail so you may not know) but
I know the question was asked before but I'm still not sure. I'm comfortable
I couldnt make good question i know,but it is what i could.When user write
I dont know but since using iOS5 (now 5.1) and Xcode 4 (now 4.3)
This one goes round and round I know but I can't seem to find
I get the feeling this is probably something I should know but I can't

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.