Illustration of Operator Keyword stackalloc with Examples in C# (C Sharp)

Why do you prefer C# when compared to C++? You might come up with different answers for it. I have mentioned two answers for this question which are related to this article discussion:


• C++ doesn’t bother about code management but C# does

• C++ allows pointer usage but C# has very restricted access to it

As mentioned above, C# has all its code managed in heap and its memory management is done automatically by garbage collection. In general, usage of pointers is dangerous as it gives way to malicious code. But C++ encourages Users to use pointers. But C# doesn’t do that. C# will not allow Users to directly access the memory via coding.

However there are situations when pointer usage is really needed to perform optimizations and few other activities. In those cases, C# gives the flexibility to use pointers but at User’s risk. User has to mark the code using pointers as unsafe code and while compiling, User has to explicitly mention /unsafe option. Here is a simple example in which a block of code is marked as unsafe:

public class sampleClass {
public static void Main( ) {
int intVar = 109;
unsafe {
int* address = & intVar;
Console.WriteLine("Address of intVar is " + (int) address);
}
}
}

This example doesn’t demonstrate the importance of pointers. It is just used to show the syntax and usage of unsafe code. Unsafe code will not be managed in heap and it will not be automatically garbage collected. Then where do you store it? How do you manage memory for it? Block of memory accessed in unsafe code will be allocated in the stack. This allocation is taken care of using stackalloc keyword.

Assume that you are using array of pointers in your unsafe code for which memory has to be allocated in the stack. How do you do it using stackalloc keyword? Here is a code sample:
public class sampleClass {
public static unsafe void Main() {
int intVar = 105;
int *ptrArray = stackalloc int[15];
for (int index = 0; index<5; index++) {
intVar += 5;
ptrArray[index] = intVar;
}
}
}

In this example, you allocated block of memory for an array containing 15 elements in the stack. Now one question might arise to you, when will this memory be freed? You don’t have any automatic garbage collection happening here. However, memory is freed when the control is returned from the function in which the allocation has happened.

Limitations in using stackalloc Keyword:

• This keyword should only be used inside unsafe code context

• Stack size is very limited and therefore the array you are trying to allocate should be of minimal size

• If the stack is full then allocation will not happen and StackOverflowException will occur

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How does Yield Keyword of C# Use Lazy Evaluationm: Reference Types |How to Create Iterator to Process Generic List in C# | How to Use “add” and “remove” Contextual Keywords of C# | How to Use ForEach with Arrays in C# |Illustration of Operator Keywords (as, is) with Examples in C# | Illustration of Operator Keywords (new, sizeof) with Examples in C# |Illustration of Operator Keyword stackalloc with Examples in C# |Illustration of Operator Keywords (true, false) with Examples in C#|Illustration of Operator Keyword typeof with Examples in C# |List of Contextual Keywords in C#|


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.