recently i want to convert some codes from opencv to c#. But I have some problem in declaring the dynamic array in c++.
int* memorySteps = 0;
CV_CALL( memorySteps = new int [ 10*sizeof(memorySteps[0]) ]);
I want to convert it to a normal c# int array. The problem is the length.In c++, the declaration above seems that the compilor will create an array with 40 int elements(coz the sizeof the type memorySteps[0] is 4, 4 bytes length.). Is my understanding correct? However, it doesnt make sense because actually in the algorithm I only need an array of 10 int elements. So can anyone give me a hint which is right in the following(in c#), or can anyone show me other examples:
int[] memorySteps =new int [10*sizeof(int)];
or
int[] memorySteps =new int [10];
Your understanding is correct; the C++ version will create an array of 40 integers, assuming sizeof(int)==4
. It may be that the author was getting mixed up with C's malloc
, which takes a size in bytes.
So your first version will give the same behaviour, and the second is more likely to give the intended behaviour - but there is a danger that the extra-large buffer was hiding other bugs, which might suddenly be exposed if you reduce the size.