C#
  Home arrow C# arrow Page 3 - Pointers and Arrays in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Pointers and Arrays in C#
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2008-11-13

    Table of Contents:
  • Pointers and Arrays in C#
  • The fixed Statement
  • Arrays
  • Preprocessor Directives

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Pointers and Arrays in C# - Arrays


    (Page 3 of 4 )

    The stackalloc keyword

    Memory can be allocated in a block on the stack explicitly using the stackalloc keyword. Since it is allocated on the stack, its lifetime is limited to the execution of the method, just as with any other local variable. The block may use the [] operator to index into memory.

      int* a = stackalloc int [10];
      for (int i = 0; i < 10; ++i)
         onsole.WriteLine(a[i]); // print raw memory

    Fixed-size buffers

    Memory can be allocated in a block within a struct using the fixed keyword:

    fixed keyword:Memory can be allocated in a block within a struct using the fixed keyword:

      unsafe struct UnsafeUnicodeString
     
    {
        public short Length;
        public fixed byte Buffer[30];
      }

      unsafe class UnsafeClass
     
    {
        private UnsafeUnicodeString uus;
        public UnsafeClass (string s)
        {
         
    uus.Length = (short)s.Length;
          fixed (byte* p = uus.Buffer)
            for (int i = 0; i < s.Length; i++)
              p[i] = (byte)s[i];
        }
      }
      class Test
      {
       
    static void Main() {new UnsafeClass("Christian Troy");}
      }

    Thefixedkeyword is also used in this example to pin the object on the heap that contains the buffer (which will be the instance ofUnsafeClass).

    void*

    Rather than pointing to a specific value type, a pointer may make no assumptions about the type of the underlying data. This approach is useful for functions that deal with raw memory. An implicit conversion exists from any pointer type to void*. A void* cannot be dereferenced, and arithmetic operations cannot be performed on void pointers. For example:

      class Test
     
    {
       
    unsafe static void Main ()
       
    {
         
    short[ ] a = {1,1,2,3,5,8,13,21,34,55};
           
    fixed (short* p = a)
           
    {
             
    //sizeof returns size of value-type in bytes
              Zap (p, a.Length * sizeof (short));
            }
          foreach (short x in a)
            System.Console.WriteLine (x); // prints all zeros
        }

        unsafe static void Zap (void* memory, int byteCount)
        {
          byte* b = (byte*)memory;
            for (int i = 0; i < byteCount; i++)
              *b++ = 0;
        }
      }

    Pointers to Unmanaged Code

    Pointers are also useful for accessing data outside the managed heap (such as when interacting with C DLLs or COM), or when dealing with data not in the main memory (such as graphics memory or a storage medium on an embedded device).

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "C# 3.0 in a Nutshell, Third Edition, A...
     

    Buy this book now. This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek