benefits of unmanaged array vs. managed array? (visual c++)

  • Thread starter Thread starter buu
  • Start date Start date
Unmanaged arrays should come witha performance boost. Everytime you
reference an element in a Managed array the framework needs to do a runtime
check in order to make sure you have not gone outside the bounds of the
array, whereas referencing an unmanaged array is just pointer arithmitec.
That being said I don't believe the overhead of a managed array is
significant and the added safety is extremely beneficial. Personally I use
managed arrays whenever possible.
 
I have a unmanaged array declared with:

int* arr;

instanced with:
arr = new int[100];

how could I create property that would return managed array?
sorry, but I'm newbie
 
You will need to convert your unmanaged array to a managed array.
Fortunately there is a class in the System::Runtime::InteropServices
namespace that will help with this called Marshal. Here is code snippet that
should work:

property array<int>^ ManagedArr{
array<int>^ get() {
//We need to create an IntPtr that wraps arr in order to use Marshal
IntPtr arrPtr(arr);
array<int>^ managedArr = gcnew array<int>(ARRAY_SIZE);
Marshal::Copy(arrPtr, managedArr, 0, ARRAY_SIZE);
return arrPtr;
}
}

buu said:
I have a unmanaged array declared with:

int* arr;

instanced with:
arr = new int[100];

how could I create property that would return managed array?
sorry, but I'm newbie


anonymous said:
Unmanaged arrays should come witha performance boost. Everytime you
reference an element in a Managed array the framework needs to do a
runtime
check in order to make sure you have not gone outside the bounds of the
array, whereas referencing an unmanaged array is just pointer arithmitec.
That being said I don't believe the overhead of a managed array is
significant and the added safety is extremely beneficial. Personally I
use
managed arrays whenever possible.
 
it seems ok, but after that, inside the class, I have a memset(arr) command,
and I got an memory corruption error.

anonymous said:
You will need to convert your unmanaged array to a managed array.
Fortunately there is a class in the System::Runtime::InteropServices
namespace that will help with this called Marshal. Here is code snippet
that
should work:

property array<int>^ ManagedArr{
array<int>^ get() {
//We need to create an IntPtr that wraps arr in order to use Marshal
IntPtr arrPtr(arr);
array<int>^ managedArr = gcnew array<int>(ARRAY_SIZE);
Marshal::Copy(arrPtr, managedArr, 0, ARRAY_SIZE);
return arrPtr;
}
}

buu said:
I have a unmanaged array declared with:

int* arr;

instanced with:
arr = new int[100];

how could I create property that would return managed array?
sorry, but I'm newbie


anonymous said:
Unmanaged arrays should come witha performance boost. Everytime you
reference an element in a Managed array the framework needs to do a
runtime
check in order to make sure you have not gone outside the bounds of the
array, whereas referencing an unmanaged array is just pointer
arithmitec.
That being said I don't believe the overhead of a managed array is
significant and the added safety is extremely beneficial. Personally I
use
managed arrays whenever possible.

:

are there any benefits?

performance?
 
it seems ok, but after that, inside the class, I have a memset(arr) command,
and I got an memory corruption error.

anonymous said:
You will need to convert your unmanaged array to a managed array.
Fortunately there is a class in the System::Runtime::InteropServices
namespace that will help with this called Marshal. Here is code snippet
that
should work:

property array<int>^ ManagedArr{
array<int>^ get() {
//We need to create an IntPtr that wraps arr in order to use Marshal
IntPtr arrPtr(arr);
array<int>^ managedArr = gcnew array<int>(ARRAY_SIZE);
Marshal::Copy(arrPtr, managedArr, 0, ARRAY_SIZE);
return arrPtr;
}
}

buu said:
I have a unmanaged array declared with:

int* arr;

instanced with:
arr = new int[100];

how could I create property that would return managed array?
sorry, but I'm newbie


anonymous said:
Unmanaged arrays should come witha performance boost. Everytime you
reference an element in a Managed array the framework needs to do a
runtime
check in order to make sure you have not gone outside the bounds of the
array, whereas referencing an unmanaged array is just pointer
arithmitec.
That being said I don't believe the overhead of a managed array is
significant and the added safety is extremely beneficial. Personally I
use
managed arrays whenever possible.

:

are there any benefits?

performance?
 
it seems ok, but after that, inside the class, I have a memset(arr) command,
and I got an memory corruption error.

anonymous said:
You will need to convert your unmanaged array to a managed array.
Fortunately there is a class in the System::Runtime::InteropServices
namespace that will help with this called Marshal. Here is code snippet
that
should work:

property array<int>^ ManagedArr{
array<int>^ get() {
//We need to create an IntPtr that wraps arr in order to use Marshal
IntPtr arrPtr(arr);
array<int>^ managedArr = gcnew array<int>(ARRAY_SIZE);
Marshal::Copy(arrPtr, managedArr, 0, ARRAY_SIZE);
return arrPtr;
}
}

buu said:
I have a unmanaged array declared with:

int* arr;

instanced with:
arr = new int[100];

how could I create property that would return managed array?
sorry, but I'm newbie


anonymous said:
Unmanaged arrays should come witha performance boost. Everytime you
reference an element in a Managed array the framework needs to do a
runtime
check in order to make sure you have not gone outside the bounds of the
array, whereas referencing an unmanaged array is just pointer
arithmitec.
That being said I don't believe the overhead of a managed array is
significant and the added safety is extremely beneficial. Personally I
use
managed arrays whenever possible.

:

are there any benefits?

performance?
 
Back
Top