473,466 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

question about arrays

Hi,

are managed arrays of value types created on the managed heap, just as are
managed arrays of reference types ?

String* StrArray[] = new String* [5]; // Arrays of a Reference type
int NumArray __gc[] = new int __gc[5]; // // Arrays of a Value type

So both 'StrArray' and 'NumArray' are located on the heap ? and the only
difference is that 'StrArray' contains pointers to String, and 'NumArray'
contains its values directly.
Is that correct ?

thnx
Christian
Nov 17 '05 #1
4 987
> are managed arrays of value types created on the managed heap, just as are
managed arrays of reference types ?
yep. Everything is created on the managed heap.

String* StrArray[] = new String* [5]; // Arrays of a Reference type
int NumArray __gc[] = new int __gc[5]; // // Arrays of a Value type

So both 'StrArray' and 'NumArray' are located on the heap ? and the only
difference is that 'StrArray' contains pointers to String, and 'NumArray'
contains its values directly.
Is that correct ?


Not entirely since you don't have real pointers as you're used to in
native c++; rather StrArray contains handles to the gc-controlled
memory. You can think of an handle as a pointer to a lookuptable that
contains the real memory address. This is important since the memory
location of your objects doesn't neccessary stay constant (the gc moves
objects around as it compacts the heap). If you want to pass managed
objects to unmanaged code, you have to make sure the gc doesn't move
things around by pinning the objects (using __pin).

In the next version of managed c++ (download the beta at
lab.msdn.microsoft.com/vs2005) this difference between pointers and
handles is also reprsented in the syntax. Instead of String* you would
then write String^.

I hope what I'm saying here is correct :)

--
Ben
http://bschwehn.de
Nov 17 '05 #2
Hi Ben,
Not entirely since you don't have real pointers as you're used to in
native c++; rather StrArray contains handles to the gc-controlled memory.
You can think of an handle as a pointer to a lookuptable that contains the
real memory address. This is important since the memory location of your
objects doesn't neccessary stay constant (the gc moves objects around as
it compacts the heap). If you want to pass managed objects to unmanaged
code, you have to make sure the gc doesn't move things around by pinning
the objects (using __pin).


You got one aspect partially incorrect. Hanldes do not use an indirection.
They point directly to the actual memory location in the GC heap. When the
GC moves an object it updates all the handles that are pointing to it
directly. Your comment about pinning is still completely acurate since that
tells the GC not to move the momory because you have code that is holding a
direct poinjter that the GC does not know about and thus could not update.

Ronald Laeremans
Visual C++ team
Nov 17 '05 #4
> You got one aspect partially incorrect. Hanldes do not use an indirection.
They point directly to the actual memory location in the GC heap. When the
GC moves an object it updates all the handles that are pointing to it
directly.


Thanks for correcting this. I read an article or posting recently
claiming it used indirection and didn't bother thinking much about it
let alone reading the docs...

ben
Nov 17 '05 #5

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

Similar topics

7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
19
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
5
by: Wajih-ur-Rehman | last post by:
The question is about C++ (since its the C family, i posted it on this newsgroup) Lets say i declare an array int a = {1,2,3,4}; int *p = a; //This is allowed because "a" returns the address of...
3
by: James dean | last post by:
I have created algorithms in C# unsafe code and have fixed the arrays in memory for optimum performance. I use multidimensional arrays rather than jagged arrays. The algorithms i use usually read a...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
2
by: phpCodeHead | last post by:
Hello fellow codemeisters! I am needing to parse through two lists of serial numbers for parts being received into an inventory database. First, is the list of serial numbers already in the...
31
by: mdh | last post by:
I am still having a problem understanding K&RII on p 112. I have looked at the FAQs --which I am sure answer it in a way that I have missed, so here goes. A 2-dim array, (per K&R) is really a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.