Solved

an array of pointers to a managed class

Posted on 2004-04-23
2
2,855 Views
Last Modified: 2013-12-03
I am having trouble declaring an array of pointers to a managed class.  I am REQUIRED to make all my codes managed.
I think my problem is best explained by some code:

namespace ns{
public __gc class node{  ... };  //this is the class i want the array of pointers to point to

public __gc class X{  //I want to use the array of pointers in this clas
  node *ptrArray[];   //if i put a constant value in there, the compiler complains
//if i leave it like this it doesnt complain but what do i do with it??

};
}

please help me with this problem
0
Comment
Question by:bugzpodder
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 10906365
I hope this small test can help:

public __gc class node
{
public:
    node(int n)
    {
        m_n = n;
    }

    void Print()
    {
        Console::WriteLine(m_n);
    }

private:
    int m_n;
};  

public __gc class X
{
public:
    X(int n)
    {
        ptrArray = new node*[n];

        for ( int i = 0; i < n; i++ )
        {
            ptrArray[i] = new node(i);
        }
    }

    void Print()
    {
        for ( int i = 0; i < ptrArray->Count; i++ )
            ptrArray[i]->Print();
    }

private:
    node* ptrArray[];
};


int _tmain()
{
    X* p = new X(5);
    p->Print();

    return 0;
}
0
 

Expert Comment

by:rtd1983
ID: 11230917
Managed class instances reside on the managed heap. This heap is not like the unmanaged heap. The managed heap is controlled by the .NET garbage collector. On the managed heap the GC sometimes moves instances to other places on the heap. This kind of behavior causes pointers of managed types to change during time. These actions are performed transparrantly for the programmer. When you like to use managed pointers in unmanaged code you might run into some little problems :P.

So how do we use a managed pointers in unmanaged code?

Fortunatly .NET was expecting this problem and came up with a solution. You have to use the gcroot class. The gcroot class keeps track of a managed class instance on the managed heap. It provides a handle which can be used in unmanaged code to work with the managed object.

Example :

#include "stdafx.h"
#include <gcroot.h>
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;

public __gc class MyClass
{
};

int _tmain()
{
      // two methods to make an array of managed objects

      // method #1 : make the array aware that it is pointing to managed types
      // an array in a managed project is by default __gc[]
      // so __gc was not needed in the line below
      MyClass * MyArrayA __gc [] = {new MyClass()};

      // method #2 : make the array aware that is is pointing to unmanaged types
      // this method also works when you are trying to work with managed objects
      // in unmanaged onces
      gcroot <MyClass *> MyArrayB __nogc [] = {new MyClass()};

      // concluding using managed objects in unmanaged objects :
      // gcroot <MyClass *> MyObject = new MyClass();
      return 0;
}
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Fonts cannot be displayed with Firefox and Internet Explorer 6 267
Font that uses "minimal ink" when printing 27 61
Color? 8 102
Visual Studio 2013 and Eclipse Neon 6 37
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question