|
-
April 10th, 2004, 10:30 PM
#1
Knowing the size of a Variable argument list
Is there a way to know the size of a variable argument list, without using a sentinel parameter.
For example, I have a function as below: -
void AssignArray(double* array, double first, ...);
and I used it as follows : -
AssignArray(array, 5.0, 6.0, 7.0);
Is there a way to determine that 3 parameters were passed to the function?
-
April 10th, 2004, 11:00 PM
#2
I do not know the answer, but wouldn't the compiler give errors for not sending the exact numbers of parameters.
and what use would it be for knowing how many parameters are being passed?, since u already know how many u r suppose to send...
Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more
The best in Technology and Gaming News back2games.com ::: Coming this Summer
-
April 10th, 2004, 11:38 PM
#3
No there is no other way. Using a sentinel parameter is the only option.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
April 11th, 2004, 01:57 AM
#4
Re: Knowing the size of a Variable argument list
Originally posted by Hamid Mushtaq
Is there a way to determine that 3 parameters were passed to the function?
Without any information, no. The number of arguments that are passed in a variable argument list is determined by either a sentinel value, or some other parameter that indicates the number of values, or some other value in your code that you've generated.
For example, you can do this:
Code:
void AssignArray(double* array, int numitems, double first, ...);
where "numitems" is the number of doubles in the variable argument list. Note that functions such as printf() use a format string to determine the number and type of the arguments. So you have a few options, but one option you don't have is a way to know the number of arguments without explicit information in your code somewhere.
Regards,
Paul McKenzie
-
April 12th, 2004, 05:31 PM
#5
I think there's at least one way to do this.
With __cdecl convention, the caller take the responsibility to setup and clear the callee's params. Looking at this in the caller:
Code:
//...
call AssignArray
add esp,NNNN
//...
'NNNN' is the number of bytes the callee's actual params take. This divided by 4 (on 32-bit platforms) is the number of the actual params. Here's the code that gets 'NNNN'
Code:
BYTE b[3];//size of opcode 'add esp,NNNN'
unsigned int dst= (unsigned int)b;
_asm{
;copy 3 bytes from address [ebp+4] to b
mov esi,[ebp+4]
mov edi,dst
mov ecx,3
rep movsb
}
int nVar= (int)b[2]/4;//b[2]= 'NNNN'
[ebp+4] points to the address of 'add esp,NNNN', and nVar is the number of your actual params. I tested this with VC 7, but must be compiled with /Od (disable optimization) option, otherwise, [ebp+4] will be other value.
Last edited by sephiroth2m; April 12th, 2004 at 05:33 PM.
Trust urself!
-
April 12th, 2004, 06:05 PM
#6
Yeah, well, that's a real nice hack 
I.e. don't use this
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
April 12th, 2004, 06:09 PM
#7
let me throw my distracted comment in, why not jsut count the args via va_args etc. I'll think of a hack later on...
edit: I'll even think later on...
-
April 12th, 2004, 06:21 PM
#8
Originally posted by Mick
let me throw my distracted comment in, why not jsut count the args via va_args etc.
Because there is no terminator, and no means to know that you have arrived at the last argument. It's a good way to crash though
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
April 12th, 2004, 06:29 PM
#9
Originally posted by Yves M
Because there is no terminator, and no means to know that you have arrived at the last argument. It's a good way to crash though
brain fart
-
April 13th, 2004, 09:21 PM
#10
Just came up on an article about variable-length argument lists: http://www.cprogramming.com/tutorial/lesson17.html
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html
-
April 14th, 2004, 12:41 AM
#11
That article doesn't put any new light on the problem
described at this post. still, you have, by the article example,
to use the first argument as the sentinel.
**** **** **** **** **/**
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|