473,837 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why are arrays objects (on heap) instead of structs (on stack)?


Hello

Is there any explanation why Microsoft chose to implement
arrays as objects allocated on the heap instead of structs
allocated on the stack?

For "mathematic al stuff", one normally wishes to avoid
unnecessary garbage collection, but that is exactly what's
going to happen if a lot of arrays, matrixes etc are allocated
on the heap.
Consider e.g. this line at the beginning of a function:

double[,] a = new double[n, n];

If this function is called 10000 times with n = 100, that
yields around 800 MB to garbage-collect.
Ok, I realize that the case when arrays on the heap are obviously
better than arrays on the stack, is when the array is "reallocate d"
to a bigger array, e.g.:

double[] a = new double[1000];
...
a = new double[2000];

This would obviously "break" the original array. So is this the
most significant reason for the decision to put all arrays on the
heap? (There are some alternative solutions to this particular
problem, but they are all somewhat "kludgy".)

/Gomaw

Nov 15 '05 #1
5 3649
Couple of small things. First, the stack is only but so large. I'd think
you would run out of stack space if you allocated a sufficiently large
array. Second, arrays are objects because they don't have a fixed layout in
memory necessarily. They are made so that a single array can span multiple
chunks of memory (though I don't know if this is actually put to practice).

For mathematical stuff you may need to wait until generics are fully
implemented and the Array is rewritten a bit. Or you may just want to
allocate a fixed size array that you use over and over again if you keep
calling it from the same function. If the function can get called multiple
times on multiple threads, then use some OO programming and put everything
into an object pool that each stores an array instance and you can re-use
the objects from the pool as things complete.
Ok, I realize that the case when arrays on the heap are obviously
better than arrays on the stack, is when the array is "reallocate d"
to a bigger array, e.g.:

double[] a = new double[1000];
...
a = new double[2000];


That does not reallocate a bigger array. You now have two double arrays,
one that is available for GC and is 1000 elements large and a second that is
2000 elements large and is now assigned to the local variable a. There is
no way to *grow* an array. They are always of fixed size. If you have a
logical array (let's denote it by the local variable a), and you need to
*grow* it, you have to allocate a new array, copy over elements from the
previous instance and finally assign the new array back to the local
variable. Some programmers consider this *growing* the array, but in fact
you are physically creating new arrays and doing quite a bit more work than
what would happen with a realloc statement in C.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Nov 15 '05 #2
In article <#5************ **@TK2MSFTNGP12 .phx.gbl>, Ju****@games4do tnet.com
(Justin Rogers) writes:
Couple of small things. First, the stack is only but so large. I'd think
you would run out of stack space if you allocated a sufficiently large
array. Second, arrays are objects because they don't have a fixed layout in
memory necessarily. They are made so that a single array can span multiple
chunks of memory (though I don't know if this is actually put to practice).
But in C and C++, aren't arrays by default on the stack? (I don't
know, though, exactly how memory is managed. Is the heap "bigger"
than the stack?)

For mathematical stuff you may need to wait until generics are fully
implemented and the Array is rewritten a bit.
Er? Will Array be rewritten when generics heve been implemented,
in a way that might make math stuff go faster? How?

Or you may just want to
allocate a fixed size array that you use over and over again if you keep
calling it from the same function. If the function can get called multiple
times on multiple threads, then use some OO programming and put everything
into an object pool that each stores an array instance and you can re-use
the objects from the pool as things complete.


Yes, I need to do something along these lines.

(At the moment, I just declare all "temporary" arrays as instance or
even class variables, i.e. they're only allocated once. And since I
normally only have one instance of the object, memory and
thread-safety is no big deal. However, the final code needs to be
more robust, of course.)

/Gomaw

Nov 15 '05 #3
"Gomaw Beoyr" <Go*********@no .spam.please.no > wrote in message
news:e4******** *****@TK2MSFTNG P09.phx.gbl...
In article <#5************ **@TK2MSFTNGP12 .phx.gbl>, Ju****@games4do tnet.com (Justin Rogers) writes:
Couple of small things. First, the stack is only but so large. I'd think you would run out of stack space if you allocated a sufficiently large
array. Second, arrays are objects because they don't have a fixed layout in memory necessarily. They are made so that a single array can span multiple chunks of memory (though I don't know if this is actually put to practice).

I'd be EXTREMELY surprised to ever see an array split up in memory.
Afterall, array's are fixed size (as Justin pointed out). When you create
an array (e.g. new Int32[100]) the runtime knows to allocate 400 bytes (plus
a little overhead) on the heap in one contiguous chunk. If that much room
isn't available, time for a GC. This means that indexing into the array
will only take the time for a quick bounds check (index >=0, index < length,
etc) and then take the base address and add the index to find the spot in
memory. To change that would be a rather poor choice.
But in C and C++, aren't arrays by default on the stack? (I don't
know, though, exactly how memory is managed. Is the heap "bigger"
than the stack?)


That depends on if you use the new keyword or not in C++

Stack based array:
int array1[100000];

Heap based array:
int* array1 = new int[100000];

I would tend to agree with Justin that creating large arrays on the stack
could create a stack overflow. (I got one earlier today in C++, but it was
after declaring three arrays of 1 million ints each, and then only after
calling printf, so I don't think it's an issue very often, but I'm a bit out
of touch with C++).
For mathematical stuff you may need to wait until generics are fully
implemented and the Array is rewritten a bit.


Er? Will Array be rewritten when generics heve been implemented,
in a way that might make math stuff go faster? How?


This is the first I've heard that Array would be rewritten for generics.
Isn't Array already strongly typecast:
(back to C# code:)
int[] arrayOfInts = new int[100000];

arrayOfInts[0] = new Object(); // should throw an exception...

I would have thought generics would be only to assist with collections like
ArrayList, Hashtable, etc. But I could very well be mistaken.
Or you may just want to
allocate a fixed size array that you use over and over again if you keep
calling it from the same function. If the function can get called multiple times on multiple threads, then use some OO programming and put everything into an object pool that each stores an array instance and you can re-use the objects from the pool as things complete.


Yes, I need to do something along these lines.

(At the moment, I just declare all "temporary" arrays as instance or
even class variables, i.e. they're only allocated once. And since I
normally only have one instance of the object, memory and
thread-safety is no big deal. However, the final code needs to be
more robust, of course.)


Sounds like a good solution to me. For example, if you have a class to
perform an FFT on an input array of 1024 numbers AND you plan on calling
this class often and frequently, it probably makes sense to let the FFT
class be constructed with that size (1024), have the constructor allocate
any internal arrays that are needed, and keep them in memory all the time
(until the class as a whole is GC'ed). Another choice, balancing speed of
allocating / GCing with memory consumption would be to use a "weak
reference". Look that up in msdn or google.

--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com
Nov 15 '05 #4
Michael Mayer wrote:
|| This is the first I've heard that Array would be rewritten for
|| generics. Isn't Array already strongly typecast:
|| (back to C# code:)
|| int[] arrayOfInts = new int[100000];
||
|| arrayOfInts[0] = new Object(); // should throw an exception...
||
|| I would have thought generics would be only to assist with
|| collections like ArrayList, Hashtable, etc. But I could very well
|| be mistaken.
||

You are correct, Array isn't/shouldn't be rewritten to accomodate generics.

Willy.
Nov 15 '05 #5
In article <e8************ **@TK2MSFTNGP11 .phx.gbl>, mi**@mag37.com
(Michael Mayer) writes:
Another choice, balancing speed of
allocating / GCing with memory consumption would be to use a "weak
reference". Look that up in msdn or google.


Yes, I did, and they will probably help much. Thanks!

/Gomaw

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6783
by: Wolfgang Jeltsch | last post by:
Hello, I can initialized, e.g, heap-allocated ints by writing something like new int(5). Now I'd like to know if it is possible to do the same thing for struct types. Say, I have a struct declared by the following lines: struct S { int i; const char c; }
12
1928
by: jimjim | last post by:
Hello, 1. As the subject sugests, are the objects created in the stack guarranted to have been created? 2. How can I verify this? (e.g. for objects created in the heap A *a = new A one can check as: if ( a == NULL), or try to catch the bad_alloc exception) TIA
2
13752
by: Ma Xiaoming | last post by:
Dear ladies and gentlemen, I don't understand what the difference between the Heap and the Stack is. Could you please explain the difference between the both for me? Thank you very much. Best regards.
5
1371
by: Macca | last post by:
Hi, I have a number of queue structures. I want to have multiple instances of these, i.e Instance 1 Instance 2 Instance 3 ... ------------ ------------ ------------ queueA queueB queueC I was thinking of usind a structure or a class to store the queues and then
4
2435
by: k04jg02 | last post by:
I was thinking that it would be cool if a programming language could implement an array type in its standard library rather than depending on arrays being a builtin type. I wondered if C++ could do this. Obviously, writing a class that just used a C array under the hood would be cheating. As would simply allocating a big chunk of memory on the heap -- for it to be a true equivalent of a C array it should be allocate memory on the stack. ...
1
1825
by: rajesh6695 | last post by:
Hai I want to know wheather the user defined function local variables are stored in the stack or in heap.... For example i have a function add() int add(int i, int j) { int m ,n;
0
10875
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10623
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10272
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9401
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7001
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5848
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4040
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3124
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.