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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:22:13+00:00 2026-06-04T06:22:13+00:00

Following results are very interesting and I am having difficulty understanding them. Basically I

  • 0

Following results are very interesting and I am having difficulty understanding them. Basically I have a class which has an int:

class TestClass{
public:
    int test;
    TestClass() { test = 0; };
    TestClass(int _test) { test = _test; };
    ~TestClass() { /*do nothing*/ };
};

A test function which accepts a pointer of TestClass

void testFunction1(TestClass *ref){
    delete ref;
    TestClass *locTest = new TestClass();
    ref = locTest;
    ref->test = 2;
    cout << "test in testFunction1: " << ref->test << endl;
}

This is what I am doing in main:

int main(int argc, _TCHAR* argv[])
{
    TestClass *testObj = new TestClass(1);
    cout << "test before: " << testObj->test << endl;
    testFunction1(testObj);
    cout << "test after: " << testObj->test << endl;
    return 0;
}

I was expecting output to be:

test before: 1
test in testFunction1: 2
test after: 1

But I get the following output:

test before: 1
test in testFunction1: 2
test after: 2

Can someone explain this. Interesting thing is that changing testFunction1 to:

void testFunction1(TestClass *ref){
    //delete ref;
    TestClass *locTest = new TestClass();
    ref = locTest;
    ref->test = 2;
    cout << "test in testFunction1: " << ref->test << endl;
}

i.e. I do not delete ref before pointing it to new location, I get the following output:

test before: 1
test in testFunction1: 2
test after: 1

I would really appreciate if someone can explain me this strange behavior. Thanks.

  • 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-06-04T06:22:14+00:00Added an answer on June 4, 2026 at 6:22 am

    When you access the object after deleting it, the behaviour is undefined.

    The behaviour that you see follows from the memory allocation algorithm in your system.

    That is after deleting your first testclass object, you allocate memory for a new object. The runtime simply reuses the memory.

    To check what is going on, print the values of the pointers:

    void testFunction1(TestClass *ref){
        cout << ref << endl;
        delete ref;
        TestClass *locTest = new TestClass();
        cout << locTest << endl;
        ref = locTest;
        ref->test = 2;
        cout << "test in testFunction1: " << ref->test << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found a very interesting question. When I'm using following code: int main() {
I have the following very simple code snippet which is loaded from a separate
Suppose I have the following results: a=FindInstance[2*b^2 + b^3 == b^4 + t &&
Trying to run my program in FreeBSD OS, I have the following results: $
Dear all, I have a select query that currently produces the following results: DoctorName
I have a very interesting LINQ question. I have a document, that I am
I am very confused by the following results: PRINT 3.1415926535897931 /180 Console result =
I'm new and this is probably very trivial but I have the following method
I have Googled it, and found the following results: http://icfun.blogspot.com/2008/03/regular-expression-to-handle-negative.html http://regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3 With some (very
I have the following code $getchats = $api->call(getLatestChats); echo '<pre>'; print_r(array_values($getchats)); echo '</pre>'; which

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.