LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

A trick to know the size of passing array?

I have verified most of discussions about knowing a size of a array (or passing array in a function call), and looks like it is impossible to know it in C. However,  in run-time when we access over the size of a passing array,CVI knows immediately ---> generates an exception (please see my attachment) & the following codes:
 
 while(Fmt(str,"%s<%f",db_arrX[i]) > -1)      // str is a calloc char pointer & db_arrX is a float passing array
         i++;
 
Can we disable the exception (not necessary only with Fmt command, might be with other similar commands)? In this case, we know the size of that array afterward (latest i) when the exception happen!
Or any other trick to know the passing aray size?
0 Kudos
Message 1 of 6
(3,992 Views)

Hi AnhTruong,

Normally, the code 'sending' the array should inform you of the size of this array. If not, you should check if a 'end of array' special character is added at the end of the array an should look for it in order to determine the array size.

If not, I don't believe you can retrive this information. Note that being out-of-bound must be prohibited. It can lead to unexpected behaviors.

Regards,

Message 2 of 6
(3,974 Views)

Hi Mathieu,

Is there other ways without including an extra end data of the sending array (might cause the error if array has some values matching the end data) or including the size of the array (an extra parameter)?

Since I want to keep the function call as it is (the caller passing only a raw unknown-size array from other function)

 

Message Edited by plit string on 01-22-2007 08:36 AM

0 Kudos
Message 3 of 6
(3,951 Views)

Hi back,

When you pass an array, you pass a pointer to the first element - or to an element you specify. So that if you do not pass the array size, you have no other way than looking for an eventual end of array char to know its size.

Regards,

Message 4 of 6
(3,943 Views)

Hi Mathieu,

It is very inconvenient way to do that, because the caller will have the similiar problem, he/she can not add more data to a fix array or he/she can not detect array size ... then we are in a circle again!

Anyway, thanks for the explainations ... might be we have to live with it!

0 Kudos
Message 5 of 6
(3,933 Views)
A hack around this problem that in certain cases you can put the
array-length information in the zero-th element (array[0]=(type) array_len).
Then on the receiver side, instead of the array, use a pointer that points
to the first element of the array, but inly if it the length is >=1,zero
lenght arrays will crash your system:
type *p;
p=&array[1];

"AnhTruong" <x@no.email> wrote in message
news:1169390412359-466467@exchange.ni.com...
>I have verified&nbsp;most of&nbsp;discussions about knowing a size of a
>array (or passing array in a function call), and looks like it is
>impossible to know it in C. However,&nbsp; in run-time when we access over
>the size of&nbsp;a passing array,CVI knows immediately ---&gt; generates an
>exception (please see my attachment) &amp; the following codes:
> &nbsp;
> &nbsp;while(Fmt(str,"%s&lt;%f",db_arrX[i])
> &gt; -1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // str is a calloc char pointer
> &amp; db_arrX is a float passing
> array&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i ++;
> &nbsp;
> Can we disable the exception (not necessary only with Fmt command, might
> be with other similar commands)? In this case, we know the size of that
> array afterward (latest i ) when the exception happen!
> Or any other trick to know the passing aray size?
>
>
> pass_end_of_array.JPG:
> http://forums.ni.com/attachments/ni/180/27210/1/pass_end_of_array.JPG


0 Kudos
Message 6 of 6
(3,895 Views)