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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:24:33+00:00 2026-05-23T07:24:33+00:00

I have a question about why I can access certain pieces of memory, and

  • 0

I have a question about why I can access certain pieces of memory, and I think it has to do with the way I understand (or don’t understand) how the compiler saves things in memory. This is the sample code I’m working with:

The header file:

#include <iostream>
using namespace std;

class A
{
public:
int value;
A (int newValue = 5)
{
    value = newValue;
    cout << "A()" << endl;
}
~A() { cout <<"~A()" << endl; }
void func1() {cout << "A::func1()" << endl; }

};


class B : public A
{
public:
B() { A::value = 0; cout << "B()" << endl; }
~B() { cout << "~B()" << endl; }
virtual void func1 () { cout << "B::func1()" << endl; }

};

class C : public B
{
public:
C() { cout << "C()" << endl; }
~C() { cout << "~C()" << endl; }
virtual void func1() { cout << "C::func1()" << endl; }
};

The .cpp file:

int main()
{
int i;

cout << endl;
A* pA = new A (5);
B* pB = new B;
C* pC = new C;

pA->func1();
pB->func1();
pC->func1();

delete pA;
delete pB;
delete pC;

cout << endl;

    //here is my first question - why don't I get a compiler error here?
    //why does the compiler know what this is? Didn't I delete it?
A* ppA = pC;

    //here is my second question - why does this work?  
    //didn't I clear this memory?
ppA->func1();
B bObject;
B* ppB = &bObject;
ppB->func1();


cin >> i;

    return 0;
}

My questions are right there in the comments – why am I not getting errors on those lines?

If I change the .h file such that func1() is virtual in A as well, I do get an access violation on that line, but still no compile-time errors.

Thanks for any explanations 🙂

  • 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-23T07:24:34+00:00Added an answer on May 23, 2026 at 7:24 am

    The answer to the first question is easy: the instruction is written at compile time. The compiler obviously won’t know whether that value has been deleted or not, so it just blindly copies the value of pC over ppA.

    The answer to your second question is: it might not work. You have entered scary undefined-behavior-land. Think Alice in Wonderland, without the cat to guide you.

    Basically when you call operator delete on a pointer, you’re telling the OS that it can overwrite the memory if needed. “If” needed. There’s no actual memory erasure going on, just a promise that you won’t be using that block of memory anymore.

    So when you try to access the virtual table to call func1, the data might still be there. Or not. In which case you get a crash (if you’re lucky). But you deserve it, you broke your promise to the OS, so anything goes at this time.

    Edit: Note that func1 never actually uses this, so if it wasn’t virtual you could have said ((A*)0)->func1() and it would have worked fine.

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

Sidebar

Related Questions

I have the following question about JPA: Can I save the order of the
I have a question about JSF and GET http request. How can I get
I have a question about a problem I'm struggling with. Hope you can bear
I have a question about joptionpane. Using JOptionPane.showMessageDialog(...), we can create a message dialog.
So, i have some question about xml Documents in Java. Can i get all
Can anyone help me with a question about webservices and scalability? I have written
I have a question about the fastest way to perform a SQL Server query
I have questions about System.Threading.ThreadStart Class : where can I find its specifications (
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass

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.