LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Number of elements in an array

Solved!
Go to solution

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

0 Kudos
Message 1 of 8
(5,366 Views)

You stopped half the way....Smiley Wink You get the desired result by normalising it to the size of one element:

 

sizeof ( array ) / sizeof ( array [ 0 ] )

 

0 Kudos
Message 2 of 8
(5,365 Views)

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

0 Kudos
Message 3 of 8
(5,362 Views)

?

 

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.

0 Kudos
Message 4 of 8
(5,360 Views)

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

 

 

0 Kudos
Message 5 of 8
(5,353 Views)

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

0 Kudos
Message 6 of 8
(5,351 Views)
Solution
Accepted by dotNet_to_LabW

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

Message 7 of 8
(5,340 Views)

Thanks Luis and Wolfgang!

 

I am now using returnValueLenght. Luis, your answer was very detailed and convincing.

0 Kudos
Message 8 of 8
(5,314 Views)