Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Using a generic to create a pointer array

Afternoon all,

a little help if you please. In order to circumvent the 2Gb object limit in .NET I have made a class that allocates memory on the heap and this allows me to create arrays up to the limit of my free RAM. However, for ease of development (as it was a proof of concept) it was hard coded for longs. Now that it works I've been trying to alter the code to use generics so I can use the same code for multiple types.

In allocating the memory and correctly index the array I need an array of pointers of the same type that the array will hold i.e. a long array needs long*[] myLargeArray. The problem is when I use generics this declaration becomes T*[] myLargeArray, which always produces the error 'Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')'

Thanks in advance.

PS Before anyone asks, yes I really do need such large arrays.

Code example for a 2D array:


    LargeArray <int> myArray = new LargeArray<int>(x, y);

    public unsafe class LargeArray where T : struct
    {
        ...
        private T*[] tArr;
        ...
        public LargeArray(long sizeI, long sizeJ)
        {
            ...
            myLargeArray = new T*[sizeI];
            ...
        }
    }

like image 571
Brendan Avatar asked Oct 27 '09 15:10

Brendan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

According to the C# programming guide:

Any of the following types may be a pointer type:

  • sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool
  • Any enum type.
  • Any pointer type.
  • Any user-defined struct type that contains fields of unmanaged types only.

When you place the struct constraint on your generic type, the compiler does not have enough information to infer that all of the above requirements will be met (specifically the last point).

Since we don't have templates in C#, you may want to consider creating overloads of your array/pointer adapter for the numeric types that make sense, or a factory class that creates a LargeArray given a size of a certain type.

like image 77
Steve Guidi Avatar answered Oct 10 '22 13:10

Steve Guidi