dinamicly memory allocation..

does c# supprt dinamic memory allocation like c++ how

Thanks!



Answer this question

dinamicly memory allocation..

  • Laurent4x

    Uhm, I guess arrays are fixed size only in C# (shame, in VB there is a ReDim()), well use collections then, with them you may add items with Add() and don't need to care about some buffer limit:

    Or allocate a fixed size array, and when the buffer is full, allocate a bigger one and forget about the old :) Sounds nasty heh Depends on the size of your buffer for smaller bufs I wouldn't be afraid to do this.

    Collections are found in System.Collections (.Generic)

    I mostly use the generic List<T> or SortedList<T> but here what MSDN says (had it right in other window so why not paste :)

    Be sure to choose your System.Collections class carefully. Using the wrong type can restrict your use of the collection.

    Consider the following questions:

    • Do you need a sequential list where the element is typically discarded after its value is retrieved

      • If yes, consider using the Queue class or the Queue generic class if you need first-in-first-out (FIFO) behavior. Consider using the Stack class or the Stack generic class if you need last-in-first-out (LIFO) behavior.

      • If not, consider using the other collections.

    • Do you need to access the elements in a certain order, such as FIFO, LIFO, or random

      • The Queue class and the Queue generic class offer FIFO access.

      • The Stack class and the Stack generic class offer LIFO access.

      • The LinkedList generic class allows sequential access either from the head to the tail or from the tail to the head.

      • The rest of the collections offer random access.

    • Do you need to access each element by index

      • The ArrayList and StringCollection classes and the List generic class offer access to their elements by the zero-based index of the element.

      • The Hashtable, SortedList, ListDictionary, and StringDictionary classes, and the Dictionary and SortedDictionary generic classes offer access to their elements by the key of the element.

      • The NameObjectCollectionBase and NameValueCollection classes, and the KeyedCollection and SortedList generic classes offer access to their elements by either the zero-based index or the key of the element.

    • Will each element contain one value, a combination of one key and one value, or a combination of one key and multiple values

      • One value: Use any of the collections based on the IList interface or the IList generic interface.

      • One key and one value: Use any of the collections based on the IDictionary interface or the IDictionary generic interface.

      • One value with embedded key: Use the KeyedCollection generic class.

      • One key and multiple values: Use the NameValueCollection class.

    • Do you need to sort the elements differently from how they were entered

      • The Hashtable class sorts its elements by their hash codes.

      • The SortedList class and the SortedDictionary and SortedList generic classes sort their elements by the key, based on implementations of the IComparer interface and the IComparer generic interface.

      • ArrayList provides a Sort method that takes an IComparer implementation as a parameter. Its generic counterpart, the List generic class, provides a Sort method that takes an implementation of the IComparer generic interface as a parameter.

    • Do you need fast searches and retrieval of information

      • ListDictionary is faster than Hashtable for small collections (10 items or fewer). The SortedDictionary generic class provides faster lookup than the Dictionary generic class.

    • Do you need collections that accept only strings

      • StringCollection (based on IList) and StringDictionary (based on IDictionary) are in the System.Collections.Specialized namespace.

      • In addition, you can use any of the generic collection classes in the System.Collections.Generic namespace as strongly typed string collections by specifying the String class for their generic type arguments.


  • Phil RCM

    ya but what if i dint know exactly the buffer size


  • AngelOnLine

    >c# can rezise to an array

    >Array.Rezise it will resize the array to the new specified lenth..

    Well this was new to me, but is correct (is a new static method in the 2.0)


    >But my problem is.. I wanna make and array that is not limited.. that you can continously add something..

    If dynamic resizing you were asking for is solved, what's the problem now :) I think you can either do this in the loop which stores the decimal digits as characters or why not use that generic TList<T>

    TList<char> digits = new TList<char>;

    I'm sure you can store at least 2^32-1 characters in there and there are sure options to store even more (that makes a number that when represented as pow(2, e) the e would be...errrr...exactly...some larger number...:)

    Add new items with the Add() method the collection takes care for resizing.

    digits.Add(digitChar);

    I can't talk about how performant the realocation will be on large amount of data though.

    Maybe you could estimate somehow, given an exponent you first calculate how many digits will be needed to store the result if possible. Then you can create an array of the appropriate size or set List.Capacity properly so reallocation doesn't take place at all.

    (the Capacity prop specifies the number of elements that the list can contain before resizing is required)


  • Matt Gutberlet

    You allocate everything with the new keyword. That's how whatever objects are being allocated.

    Depends on what kind of data you want to store. If you want something similar to a traditional memory buffer you may use something like this:

    byte[] buffer = new byte[BUFFER_SIZE];

    That creates an array of bytes, see MSDN about C# arrays to find out more.

    Generally:

    MyType[] buf = new MyType[1024];

    Holds 1024 instances of MyType.

    The garbage collector decides how the buffer will be allocated and when it will be disposed. If you want to deallocate you object manually (rare cases, may also break overall performance), you have to define you own type which implements IDisposable interface. Then I believe Dispose() member method can be used to deallocate the object.


  • SAMSQL2005

    c# can rezise to an array

    Array.Rezise it will resize the array to the new specified lenth..

    But my problem is.. I wanna make and array that is not limited.. that you can continously add something..

    i need it for large numbers like pow(2,1000000)(c++ code), and the return should be o\in an array

    example: pow(2,5) =32
    char[] a=new char[1000];
    a[0]=3, a[1]=2;

    another example pow(2, 10)=1024
    char[] a=new char[1000];
    a[0]=1, a[1]=0, a[2]=2, a[3]=4

    and so on..

    and what if i have pow(2, 100000)
    i cannot define char[] a=new char[1000]; coz this is too smaller


  • dinamicly memory allocation..