03-30-2011 04:10 AM
Hi,
I have been trying looking for a simple function to get number of elements is an array. Have gone though older posts but could not find answer to it.
The function should work for following examples:
char* name = "nInstruments"
float state[] = {1.0, 2.0}
I tried sizeof but it returns size of "type" in bytes. For example in example 2, it will just return 4.
Does someone know a way to do it?
Ciao
RB
Solved! Go to Solution.
03-30-2011 04:26 AM
You stopped half the way.... You get the desired result by normalising it to the size of one element:
sizeof ( array ) / sizeof ( array [ 0 ] )
03-30-2011 04:32 AM
That was possible if I had got sizeof(array)/sizeof(state) in above example as 8 and not as 4 😞
Now since size of state[0] is also 4.. so I get result 4/4 = 1, although number of elemtens is 2
03-30-2011 04:38 AM
?
I have tried your example
float state[] = {1.0, 2.0}
and in my case sizeof ( state ) yields 8 (bytes), while the size of a single float is 4 bytes.
03-30-2011 07:04 AM
Hi,
Sorry, for wrongly explaining my problem. Actually, I am getting array from a .NET dll (as a return parameter) and not declaring directly it like I have shown above. Can this be the reason why I am getting different result than yours?
Thanks
RB
03-30-2011 07:21 AM
I have no conception of NET and intuitively would have assumed that both the size of a float and the size of an array of floats should be the same for C and NET - but you better hope for some expert advice...
Wolfgang
03-30-2011 11:14 AM
RB,
In C, if an array is declared on the heap, as opposed to on the stack as in your example above, the sizeof operator only returns the length of the pointer that holds the array which, in a 32-bit program, is always 4 bytes. This is because the compiler has no way of knowing how many elements that array actually holds. The length of a C array is a fairly fluid concept. The real size of the array is usually determined by some memory manager somewhere (usually, malloc), but it's not something that the language itself has anything to say about.
When you declare the entire array as a local variable, however, the compiler must create room for the full array on the stack, and therefore it is able to return the true size of the array via the sizeof operator. (The same is true if it's a global variable, even if it's not on the stack, in that case)
For this reason, using the sizeof(array) / sizeof(array[0]) trick is iffy. It might work, or it might not work, depending on how the array is declared.
For this reason, most C APIs that accept or return arrays, will usually also have some method of communicating the length of the array, usually as an additional parameter of some function. Because .NET APIs usually have no need to do this, the C wrapper for a .NET API in CVI does this for you when it handles .NET arrays. For example, here's what a function signature might look like:
int CVIFUNC Namespace1_Class1_Test1DArrays (Namespace1_Class1 __instance, int * inA, int __inALength, char *** outA, int * __outALength, Namespace1_Class2 ** refA, int * __refALength, double ** __returnValue, int * ____returnValueLength, CDotNetHandle * __exception);
Luis
03-31-2011 01:06 AM
Thanks Luis and Wolfgang!
I am now using returnValueLenght. Luis, your answer was very detailed and convincing.