Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Where is this managed object stored?

Where is this managed object stored?

Scheduled Pinned Locked Moved Managed C++/CLI
c++data-structuresdotnetquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Frank__Q
    wrote on last edited by
    #1

    value class ValBase
    {
    public:
    int a;
    };

    ref class RefBase
    {
    public:
    int a;
    };

    int main(array ^args)
    {

    RefBase^ RefBase1 = gcnew RefBase; //LEGAL. Ref type Managed Obj created on CLR heap.
    ValBase^ ValBase1 = gcnew ValBase; //LEGAL. Value type Managed Obj created on CLR heap.

    RefBase* RefBase2 = new RefBase; //ILLEGAL: new cannot be used on Managed Ref Class
    ValBase* ValBase2 = new ValBase; //This compiles okay but where is this "Managed Object" stored ? CLR heap or Native heap ?

    }

    In the last assignment where is the managed object stored ? I am totally new to C++ CLI. Also, is it true that value types should use stack semantics to make code efficient ? i.e instead of ValBase^ ValBase1 = gcnew ValBase, I should just use ValBase ValBase1;

    G 1 Reply Last reply
    0
    • F Frank__Q

      value class ValBase
      {
      public:
      int a;
      };

      ref class RefBase
      {
      public:
      int a;
      };

      int main(array ^args)
      {

      RefBase^ RefBase1 = gcnew RefBase; //LEGAL. Ref type Managed Obj created on CLR heap.
      ValBase^ ValBase1 = gcnew ValBase; //LEGAL. Value type Managed Obj created on CLR heap.

      RefBase* RefBase2 = new RefBase; //ILLEGAL: new cannot be used on Managed Ref Class
      ValBase* ValBase2 = new ValBase; //This compiles okay but where is this "Managed Object" stored ? CLR heap or Native heap ?

      }

      In the last assignment where is the managed object stored ? I am totally new to C++ CLI. Also, is it true that value types should use stack semantics to make code efficient ? i.e instead of ValBase^ ValBase1 = gcnew ValBase, I should just use ValBase ValBase1;

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      ValBase2 is stored on the unmanaged heap since you are using new instead of gcnew. Thus, you will have to explicitly delete it: delete ValBase2;.

      "We make a living by what we get, we make a life by what we give." --Winston Churchill

      F 1 Reply Last reply
      0
      • G George L Jackson

        ValBase2 is stored on the unmanaged heap since you are using new instead of gcnew. Thus, you will have to explicitly delete it: delete ValBase2;.

        "We make a living by what we get, we make a life by what we give." --Winston Churchill

        F Offline
        F Offline
        Frank__Q
        wrote on last edited by
        #3

        Thanks George. So, is there any way for a Native pointer to point to Managed Heap. I know this is not the correct thing to do but is it possible ? An example would be appreciated. I also think there is no way for a Handle (^) to point to native heap, is this correct ?

        L S 2 Replies Last reply
        0
        • F Frank__Q

          Thanks George. So, is there any way for a Native pointer to point to Managed Heap. I know this is not the correct thing to do but is it possible ? An example would be appreciated. I also think there is no way for a Handle (^) to point to native heap, is this correct ?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          The GCHandle class could help you out. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          1 Reply Last reply
          0
          • F Frank__Q

            Thanks George. So, is there any way for a Native pointer to point to Managed Heap. I know this is not the correct thing to do but is it possible ? An example would be appreciated. I also think there is no way for a Handle (^) to point to native heap, is this correct ?

            S Offline
            S Offline
            slawomir_orlowski
            wrote on last edited by
            #5

            The native pointer can point to any address. Please explain why you want to use native pointer to managed resource? It is very incorrect way. I think you should change your architecture instead of making such a thing. If you want to call managed code (method of managed object) from unmanaged context you should use callback. Handle can not point to native heap, but IntPtr can.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups