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

  • Home
  • SEARCH
  • 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 4023118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:33:34+00:00 2026-05-20T10:33:34+00:00

Let’s say I have: class Animal { } class Dog : Animal { }

  • 0

Let’s say I have:

class Animal { }
class Dog : Animal { }

And in the main method:

Dog dog = new Dog();
Animal animal = dog;

Now there is one instance of Dog on the heap, and two variables on the stack, with the same reference stored in them. The next step is

   //Try to invoke dog.Bark() programatically
   //Try to invoke animal.Bark() programatically

The latter fails at run time, because animal hasn’t a Bark method. So there must be something different on stack, for the two variables. What’s the difference resulting in the run time error?

  • 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-20T10:33:35+00:00Added an answer on May 20, 2026 at 10:33 am

    You are correctly deducing a number of implementation details.

    Before going into those details, some terms. There are three kinds of storage:

    • unmanaged storage
    • managed storage
    • temporary storage

    Unmanaged storage is outside the scope of your question. Managed storage is what we’ll call “the heap”. Temporary storage is “the stack” or registers if the jitter is clever and registers are available, but as a simplifying assumption lets ignore registers and just call the temporary storage pool “the stack”.

    There are four kinds of types:

    • unmanaged pointers
    • value types
    • references, aka, managed pointers
    • the actual instances of reference types

    Unmanaged pointers are outside the scope of your question. C# allows you to manipulate references (“managed pointers”) and value types directly; contents of reference type instances are only manipulated via references and are always on the heap in the CLR implementation. (Nothing stops the jitter from creating an instance of reference type on the stack if it has enough smarts to know that the object never needs garbage collection and does not survive the method, but in practice our jitters do not do so to my knowledge.)

    The other three kinds of things can be on the stack or the heap as necessary, depending on their lifetimes.

    In my main method an instance of Dog is created and assigned to a variable named dog. I know what happens on the heap, assuming it locates from the address 0x00FFFF.

    You are assuming (correctly) that managed references are implemented as addresses in a flat 32 bit address space. (That would not actually be a legal heap address, but we’ll let it slide.) Of course, an implementation of the CLR need not use raw addresses as references. They could be opaque handles into a table controlled by the GC, for example.

    Currently I only know on the stack the space for dog has its value 0x00FFFFF, but I’m 100% sure somewhere else in the space for dog has its declaring type.

    By its “declaring” type I think you mean the actual runtime type of the instance, Dog. This is a confusing use of “declaring”. Instances are not “declared”; instances are ” allocated”. Variables are “declared” and the variable has the declared type “Animal”.

    Yes, somewhere in the actual instance data allocated for “new Dog()” there is a “token” that describes the runtime type of the object. If the exact details of how the token works are important to you, ask another question.

    I want to know a detail explaination of the stack space for dog, currently I know there is a reference address there, anything else?

    Your question is ambiguous because there are two variables on the stack and neither of them are named “dog”.

    At runtime the stack space just contains the managed reference to the heap space allocated for the instance. The C# compiler knows that the stack space for dog1 was of compile-time type managed-reference-to-Animal and the stack space for dog2 was of compile time type managed-reference-to-Dog. The jitter therefore knows that information as well. The jitter keeps information about that in its own data structures; it does not pollute the stack. The jitter can keep this information around for optimizations, or for debugging purposes. Or it can throw it away if it doesn’t need that information anymore.

    This again is an implementation detail; the jitter is perfectly free to put any additional information on the stack it pleases.

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

Sidebar

Related Questions

Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's say I have the following classes : public class MyProductCode { private String
Let's say you have a method that expects a numerical value as an argument.
Let's say you have an array like this: array ( ['one'] => array (
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say you have a class, with certain properties, and that you tried your
Let's say I have two objects, Master and Slave . Slave has a method

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.