CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2003
    Location
    Karachi, Pakistan
    Posts
    38

    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?

  2. #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

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    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

  5. #5
    Join Date
    Jan 2004
    Posts
    56
    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!

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.

  7. #7
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    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...

  8. #8
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.

  9. #9
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    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

  10. #10
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729
    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

  11. #11
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    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
  •  





Click Here to Expand Forum to Full Width

Featured