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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:44:37+00:00 2026-06-16T07:44:37+00:00

It seems that ‘delete’ (free memory in C++) does not work when I’m trying

  • 0

It seems that ‘delete’ (free memory in C++) does not work when I’m trying the following code…
Well, I know the reference is not suitable for “refer to an object which will be freed later on”. I am just playing the code..

class A{
public:
    int val;
    A(int val_=0):val(val_){}
};

A* ptrA = new A(10);
A &refA = *ptrA;
printf("%d\n", refA.val);
delete ptrA;
refA.val = 100;
printf("%d\n", refA.val);

The output is :
10
100

  • 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-16T07:44:38+00:00Added an answer on June 16, 2026 at 7:44 am

    It does work, and everything you do on refA causes undefined behavior, i.e., as far as the standard is concerned, anything may happen, including “it seems to work”.

    In practice, for the moment it may seem to work because that memory hasn’t been reused yet, but wait a few allocations and you’ll see you’ll be overwriting other, unrelated objects.

    Remember, when you go into “undefined behavior”-land you get a crash or a failed assertion if you are lucky; often, you’ll get strange, non-reproducible bugs that happens once in a while driving you mad.

    Well, I know the reference is not suitable for “refer to an object which will be freed later on”. I am just playing the code..

    There’s nothing bad in having a reference to stuff that will be freed… the important point is that it has to be freed after the reference goes out of scope. For example, in this case it’s perfectly fine:

    A* ptrA = new A(10);
    {
        A &refA = *ptrA;
        std::cout<<refA.val<<"\n";
    }
    delete ptrA;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems that a site I made for fun does not like to work in
Seems that CPPPATH does not work env = Environment() target = 'app' sources =
It seems that when I run the following code: #include <stdio.h> #include <stdlib.h> int
Seems that it may not be possible, but hey I might as well ask,
Seems that Grails does automatically recompile my GSP pages, but not my controllers. It
Seems that there are not much work done on Haskell for web. People uses
Seems that lot of people already know that issue but I can not find
It seems that HTMLCollection and NodeList is undefined in ie6/7,and following code will result
It seems that the android TTS classes found in the sdk does not support
It seems that runClasses() doesn't terminate the code being tested even after the test

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.