Checking size of array argument 
Author Message
 Checking size of array argument

Is there a way for a function to check the size of an array which is
passed as an argument, before manipulating it?
I havew a DLL function:
__declspec(__dllexport) int MyFnc(double *a).
A is supposed to be manipulated by the function, but, being a DLL I
cannot be sure at program time that the call will be done correctly, so
I'd like to check the array size in order to prevent the program from
crashing.


Fri, 06 Sep 2002 03:00:00 GMT  
 Checking size of array argument

Quote:
> Is there a way for a function to check the size of an array which is
> passed as an argument, before manipulating it?

No.

Quote:
> I havew a DLL function:
> __declspec(__dllexport) int MyFnc(double *a).
> A is supposed to be manipulated by the function, but, being a DLL I
> cannot be sure at program time that the call will be done correctly, so
> I'd like to check the array size in order to prevent the program from
> crashing.

Pass the size of the array to MyFnc.

TiTi



Fri, 06 Sep 2002 03:00:00 GMT  
 Checking size of array argument

Quote:

>Is there a way for a function to check the size of an array which is
>passed as an argument, before manipulating it?
>I havew a DLL function:
>__declspec(__dllexport) int MyFnc(double *a).
>A is supposed to be manipulated by the function, but, being a DLL I
>cannot be sure at program time that the call will be done correctly, so
>I'd like to check the array size in order to prevent the program from
>crashing.

Sorry, given a pointer to T, there's no way to determine if it points
to a single T or an element of an array of T. In general, you can't
even tell if it points to a valid T.

If MyFnc handles arrays of different sizes, you could add a size
parameter. If it handles an array of only one size, you could use a
reference parameter instead of a pointer:

 void fun(int (&x)[10])
 {
 }

This function will only accept an argument of type int[10], which
really limits its use. But hey, if that's what you want to do...

--
Doug Harrison

Visual C++ MVP



Fri, 06 Sep 2002 03:00:00 GMT  
 Checking size of array argument

Quote:
>Is there a way for a function to check the size of an array which is
>passed as an argument, before manipulating it?

Only if you somehow pass a parameter to the function that specifies
how big the passed array is.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Fri, 06 Sep 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. template argument array size interpreted as 0

2. Passing sizes of arrays without a separate argument for the length

3. How to check the size of an array when passed to a function

4. Array size checks

5. Checking for NULL arguments in Managed C++

6. Differences between char array[SIZE] and char *array

7. How much function argument checking to do?

8. argument checking (for beginners)

9. Argument validity checking (addresses)

10. Checking for arguments (number)

11. Variable Argument Type Checking

12. Arrays [256][256] --- error Array size too large!

 

 
Powered by phpBB® Forum Software