PC Review


Reply
 
 
Neil Zanella
Guest
Posts: n/a
 
      30th Dec 2004
Hello,

It seems that in C# arrays are always created on the heap. This seems
like it could prove somewhat inefficient in some situations. I would
like to know whether it is possible to create an array on the stack
in C# as it is possible in C and C++.

Thanks,

Neil
 
Reply With Quote
 
 
 
 
Daniel O'Connell [C# MVP]
Guest
Posts: n/a
 
      30th Dec 2004

"Neil Zanella" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> It seems that in C# arrays are always created on the heap. This seems
> like it could prove somewhat inefficient in some situations. I would
> like to know whether it is possible to create an array on the stack
> in C# as it is possible in C and C++.


It is not possible to allocate an array itself on the stack. However, when
using unsafe code you can generate a local buffer on the stack using a
rarely used keyword, stackalloc to return a pointer to that memory block.


 
Reply With Quote
 
Helge Jensen
Guest
Posts: n/a
 
      31st Dec 2004
Neil Zanella wrote:
> Hello,
>
> It seems that in C# arrays are always created on the heap. This seems
> like it could prove somewhat inefficient in some situations. I would
> like to know whether it is possible to create an array on the stack
> in C# as it is possible in C and C++.


For now, you could try writing:

T[] Tarray = { t1, t2, ... };
f(Tarray);

And see if that hurts your performance too much... if it does, you'll
have to start doing something, but I think almost all things you could
try would slow things down more that it would speed them up.

The above way of writing has the nice effect of stating that you are
thinking of the array as being on the stack.

If the compiler grows clever enough and knows enough about the
side-effects of called functions, it might one day decide to allocate
that array on the stack.

--
Helge
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting native arrays to managed arrays Bob Altman Microsoft VC .NET 8 27th Feb 2008 11:33 PM
Trouble with arrays (transferring values between two arrays) Keith R Microsoft Excel Programming 4 14th Nov 2007 12:00 AM
Jagged Arrays Problem - How to Assign Arrays to an Array Zigs Microsoft Excel Programming 3 11th Apr 2007 01:39 AM
Working with ranges in arrays... or an introduction to arrays =?Utf-8?B?R2xlbg==?= Microsoft Excel Programming 5 10th Sep 2006 08:32 AM
Arrays - declaration, adding values to arrays and calculation Maxi Microsoft Excel Programming 1 17th Aug 2006 04:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:24 AM.