キャンセル
次の結果を表示 
次の代わりに検索 
もしかして: 

Number of elements in an array

解決済み
解決策を見る

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 件の賞賛
メッセージ1/8
5,623件の閲覧回数

You stopped half the way....スマイリー ウインク You get the desired result by normalising it to the size of one element:

 

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

 

0 件の賞賛
メッセージ2/8
5,622件の閲覧回数

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 件の賞賛
メッセージ3/8
5,619件の閲覧回数

?

 

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 件の賞賛
メッセージ4/8
5,617件の閲覧回数

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 件の賞賛
メッセージ5/8
5,610件の閲覧回数

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 件の賞賛
メッセージ6/8
5,608件の閲覧回数
解決策
受理者 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

メッセージ7/8
5,597件の閲覧回数

Thanks Luis and Wolfgang!

 

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

0 件の賞賛
メッセージ8/8
5,571件の閲覧回数