public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Knowing the number of variable arguments.
@ 2000-05-28 17:39 Gigi Sullivan
  2000-05-28 18:18 ` Alexandre Oliva
  0 siblings, 1 reply; 8+ messages in thread
From: Gigi Sullivan @ 2000-05-28 17:39 UTC (permalink / raw)
  To: gcc-help

Aiee :)

	Hello!

	I'm not sure this is the right mailing list and I search
	a little bit the archive, but didn't find the right answer ...
	so please forgive me :)

	Let consider this simple function prototype:

	int
	func(char, int, ...);

	Well, I know that using va_start/va_arg and va_end I can
	scan all the variable arguments list, but what if I'd like
	to know the *number* of the variable arguments?

	I'm saying so, since the argument type (in my .c file) isn't 
	int, but it's a union (or it could be a struct too - but right now
	a union) and as man said, when there are no other argument in 
	the list, va_arg returns random errors. 

	This is bad, since I cannot know *when* the list is terminated
	(and I cannot do like the example showed to me, i.e. using pointer +
	while and switch etc etc).

	I tried to search about some __buitin_*, but I didn't find anything
	to suite my needs.

	Well, I indeed solved the problem, but it's strictly non-portable
	since I played with the saved frame pointer (to know the caller frame
	address) and with the address of the first argument in `func' (even
	if no asm code were used, tho. Still not-portable, I guess due to
	different calling convention. The CPU is an Intel x86).

	So I was wondering if someone could point me out a solution
	like some magic macro, such as GIMME_ARGS_NUMBER :)

Thx for all.

bye bye

				-- gg sullivan

P.S.

I apologize for my bad english.

-- 
Lorenzo Cavallaro	`Gigi Sullivan' <sullivan@sikurezza.org>

Until I loved, life had no beauty;
I did not know I lived until I had loved. (Theodor Korner)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Knowing the number of variable arguments.
  2000-05-28 17:39 Knowing the number of variable arguments Gigi Sullivan
@ 2000-05-28 18:18 ` Alexandre Oliva
  2000-05-28 18:27   ` Gigi Sullivan
  0 siblings, 1 reply; 8+ messages in thread
From: Alexandre Oliva @ 2000-05-28 18:18 UTC (permalink / raw)
  To: Gigi Sullivan; +Cc: gcc-help

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 824 bytes --]

On May 29, 2000, Gigi Sullivan <sullivan@sikurezza.org> wrote:

> 	Well, I know that using va_start/va_arg and va_end I can
> 	scan all the variable arguments list, but what if I'd like
> 	to know the *number* of the variable arguments?

There's no portable way.  That's why functions with variable number of
arguments require at least one argument: so that you can pass in some
clue about the other arguments.

> 	This is bad, since I cannot know *when* the list is terminated

How about requiring a NULL terminator, as in exec()?

-- 
Alexandre Oliva    Enjoy Guaraná, see http://www.ic.unicamp.br/~oliva/
Cygnus Solutions, a Red Hat company        aoliva@{redhat, cygnus}.com
Free Software Developer and Evangelist    CS PhD student at IC-Unicamp
oliva@{lsd.ic.unicamp.br, gnu.org}   Write to mailing lists, not to me

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Knowing the number of variable arguments.
  2000-05-28 18:18 ` Alexandre Oliva
@ 2000-05-28 18:27   ` Gigi Sullivan
  2000-05-29 11:18     ` Alexandre Oliva
  0 siblings, 1 reply; 8+ messages in thread
From: Gigi Sullivan @ 2000-05-28 18:27 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: gcc-help

Aiee :)

	Hello!

> On May 29, 2000, Gigi Sullivan <sullivan@sikurezza.org> wrote:
> 
> > 	Well, I know that using va_start/va_arg and va_end I can
> > 	scan all the variable arguments list, but what if I'd like
> > 	to know the *number* of the variable arguments?
> 
> There's no portable way.  That's why functions with variable number of
> arguments require at least one argument: so that you can pass in some
> clue about the other arguments.

	Yes, I see.

> 
> > 	This is bad, since I cannot know *when* the list is terminated
> 
> How about requiring a NULL terminator, as in exec()?

	Well, I would avoid using this solution (It's boring for what
	I'm doing to put *mandatory* a NULL at the end).

	Nevertheless to say (well, as you know ;)) that this could be non-portable 
	too (Am I right?) :

	Not all pass parameters on the stack (that could grows towards
	lower/upper address).

	What do you advice to me? (maybe the former solution is better than the
	latter one).

	Anyway THX :))

bye bye

				-- gg sullivan

P.S.

Could be this an addendum to the current gcc? (addendum to stdarg.h I mean)
This is not portable, but it's possible to do as stdarg.h does: #ifdef :)

-- 
Lorenzo Cavallaro	`Gigi Sullivan' <sullivan@sikurezza.org>

Until I loved, life had no beauty;
I did not know I lived until I had loved. (Theodor Korner)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Knowing the number of variable arguments.
  2000-05-28 18:27   ` Gigi Sullivan
@ 2000-05-29 11:18     ` Alexandre Oliva
  2000-05-29 12:27       ` Mike Corbeil
  0 siblings, 1 reply; 8+ messages in thread
From: Alexandre Oliva @ 2000-05-29 11:18 UTC (permalink / raw)
  To: Gigi Sullivan; +Cc: gcc-help

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1279 bytes --]

On May 29, 2000, Gigi Sullivan <sullivan@sikurezza.org> wrote:

>> On May 29, 2000, Gigi Sullivan <sullivan@sikurezza.org> wrote:

>> > 	This is bad, since I cannot know *when* the list is terminated

>> How about requiring a NULL terminator, as in exec()?

> 	Well, I would avoid using this solution (It's boring for what
> 	I'm doing to put *mandatory* a NULL at the end).

If there's no perfect solution, a boring one will have to do, right? :-)

> 	Nevertheless to say (well, as you know ;)) that this could be
> 	non-portable too (Am I right?) :

Nope.  If you pass a list of pointers, the last of which is NULL, then
you can certainly expect va_arg to work correctly on all platforms, as
long as you tell it each argument is the appropriate pointer type.

> 	Not all pass parameters on the stack (that could grows towards
> 	lower/upper address).

va_arg will take care of this for you.

> Could be this an addendum to the current gcc?

What kind of extension are you thinking of?

-- 
Alexandre Oliva    Enjoy Guaraná, see http://www.ic.unicamp.br/~oliva/
Cygnus Solutions, a Red Hat company        aoliva@{redhat, cygnus}.com
Free Software Developer and Evangelist    CS PhD student at IC-Unicamp
oliva@{lsd.ic.unicamp.br, gnu.org}   Write to mailing lists, not to me

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Knowing the number of variable arguments.
  2000-05-29 11:18     ` Alexandre Oliva
@ 2000-05-29 12:27       ` Mike Corbeil
  2000-05-29 14:01         ` Gigi Sullivan
  0 siblings, 1 reply; 8+ messages in thread
From: Mike Corbeil @ 2000-05-29 12:27 UTC (permalink / raw)
  To: gcc-help

Alexandre Oliva wrote:

> On May 29, 2000, Gigi Sullivan <sullivan@sikurezza.org> wrote:
>
> Nope.  If you pass a list of pointers, the last of which is NULL, then
> you can certainly expect va_arg to work correctly on all platforms, as
> long as you tell it each argument is the appropriate pointer type.
>
> >       Not all pass parameters on the stack (that could grows towards
> >       lower/upper address).
>
> va_arg will take care of this for you.
>
> > Could be this an addendum to the current gcc?
>
> What kind of extension are you thinking of?
>

This was initially stated to be for a C program, or .c anyway, and I'm
wondering what stage of development this program is at.  If early and there
wouldn't be much to convert, then couldn't this be done in C++, using
inherent language support?

I haven't worked in or looked at C++ for some time, but remember something
about methods inherently being able to accept a variable number of
arguments, albeit maybe one is always needed.   I have books and could
verify this, but am lazy and will just throw out the idea.  There's
probably someone out there with more experience using this particular C++
capability, because I've never need to use it.

From what I recall, this was only covered for methods.  I don't recall
anything being specifically said about variable command line argument
lists.

Isn't there a getopt or getopts C function?  There is for shell script and
it (I believe) requires -{option}'s, i.e., option switches, to be used (not
sure if the switches are necessary), but seem to remember reading
somewhere, maybe in Perl, that the script getopt function was associated
with the C function, and this would be GNU C.  Perl has been ported to
other OSs and I believe it's getopt function is supported on all;
therefore, GNU C getopt or what ever the correct name is would probably
also be equally supported or portable.

Check for the getopt function (name may be a little different) to see if
this is portable and exists.

With getopt, at least with the option switches, you need to account for all
possibilities and defaults, although default could be the only thing the
block does or does not do, and you code according to what ever the logic is
for the command line.  Perhaps I don't understand the problem in question
well enough, but this is simple to do in script.

If this is a new program and little has been written, then maybe Perl would
be a solution, if you can't determine how to do this in C or C++.

mike



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Knowing the number of variable arguments.
  2000-05-29 12:27       ` Mike Corbeil
@ 2000-05-29 14:01         ` Gigi Sullivan
  2000-05-29 14:23           ` Alexandre Oliva
  0 siblings, 1 reply; 8+ messages in thread
From: Gigi Sullivan @ 2000-05-29 14:01 UTC (permalink / raw)
  To: Mike Corbeil; +Cc: gcc-help

Aiee :)

	Hello!

> Alexandre Oliva wrote:
> 
> > On May 29, 2000, Gigi Sullivan <sullivan@sikurezza.org> wrote:
> >
> > Nope.  If you pass a list of pointers, the last of which is NULL, then
> > you can certainly expect va_arg to work correctly on all platforms, as
> > long as you tell it each argument is the appropriate pointer type.
> >
> > >       Not all pass parameters on the stack (that could grows towards
> > >       lower/upper address).
> >
> > va_arg will take care of this for you.

	Yep, this is true, but I'd like to avoid explicity NULL in parameter
	list. There's no true reason to avoid NULL, only it's more pretty.

> >
> > > Could be this an addendum to the current gcc?
> >
> > What kind of extension are you thinking of?
> >

	I was thinking about some pretty __builtin* feature that could
	returns the number of argument passed to a function ...
	(like __builtin_next_arg(last) is used in some va_arg implementation
	I guess).

> 
> This was initially stated to be for a C program, or .c anyway, and I'm
> wondering what stage of development this program is at.  If early and there
> wouldn't be much to convert, then couldn't this be done in C++, using
> inherent language support?

	Yep, I know... but I cannot (don't want to) write it in C++.

> Isn't there a getopt or getopts C function?  There is for shell script and
> it (I believe) requires -{option}'s, i.e., option switches, to be used (not
> sure if the switches are necessary), but seem to remember reading
> somewhere, maybe in Perl, that the script getopt function was associated
> with the C function, and this would be GNU C.  Perl has been ported to
> other OSs and I believe it's getopt function is supported on all;
> therefore, GNU C getopt or what ever the correct name is would probably
> also be equally supported or portable.
> 
> Check for the getopt function (name may be a little different) to see if
> this is portable and exists.

	Well, I really never looked at getopt sources, but I guess that
	getopt typically use the well-known `char *fmt' stuff then
	with while/switch/va_arg we can get all (ok, man va_arg for example
	and see the EXAMPLE section).

	Uhm ... related question (well it's the same question applied to other
	stuff ;)):

	consider this macro

	#define EXPAND_ARGS(some, args...)  ex_args(some, ##args)

	Is there a way to get the number of parameters EXPAND_ARGS will expand to?
	I means is there some pretty gcc-related-feature to get this?

	e.g.

	#define EXPAND_ARGS(some, args...) \
	do { \
		int argc = $$args /* I'm mad! :) something that can get the #args */ \
		ex_args(some, argc, ##args); \
	} while(0)

	so in ex_args, I can know the parameter number, by getting argc value
	(and can use va_arg (through a for cycle, for example).

bye bye (and thx :))

			-- gg sullivan

-- 
Lorenzo Cavallaro	`Gigi Sullivan' <sullivan@sikurezza.org>

Until I loved, life had no beauty;
I did not know I lived until I had loved. (Theodor Korner)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Knowing the number of variable arguments.
  2000-05-29 14:01         ` Gigi Sullivan
@ 2000-05-29 14:23           ` Alexandre Oliva
  2000-05-29 14:42             ` Gigi Sullivan
  0 siblings, 1 reply; 8+ messages in thread
From: Alexandre Oliva @ 2000-05-29 14:23 UTC (permalink / raw)
  To: Gigi Sullivan; +Cc: Mike Corbeil, gcc-help

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1172 bytes --]

On May 30, 2000, Gigi Sullivan <sullivan@sikurezza.org> wrote:

> 	I was thinking about some pretty __builtin* feature that could
> 	returns the number of argument passed to a function ...

This just can't be computed in general.  There's no such thing as a
magic terminator that GCC could use for that.  And using the FP is not
always possible.

> 	#define EXPAND_ARGS(some, args...)  ex_args(some, ##args)

> 	Is there a way to get the number of parameters EXPAND_ARGS will expand to?
> 	I means is there some pretty gcc-related-feature to get this?

If all args are of the same type, and you know that type, you could
declare an auto array, initialize it with ##args, then divide the size
of the array by the size of a single element.  But this would evaluate
the arguments twice.

But then, why don't you use

#define EXPAND_ARGS(some, args...) ex_args(some, ##args, NULL)

?

-- 
Alexandre Oliva    Enjoy Guaraná, see http://www.ic.unicamp.br/~oliva/
Cygnus Solutions, a Red Hat company        aoliva@{redhat, cygnus}.com
Free Software Developer and Evangelist    CS PhD student at IC-Unicamp
oliva@{lsd.ic.unicamp.br, gnu.org}   Write to mailing lists, not to me

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Knowing the number of variable arguments.
  2000-05-29 14:23           ` Alexandre Oliva
@ 2000-05-29 14:42             ` Gigi Sullivan
  0 siblings, 0 replies; 8+ messages in thread
From: Gigi Sullivan @ 2000-05-29 14:42 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: gcc-help

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1338 bytes --]

Aiee :)

	Hello!

> On May 30, 2000, Gigi Sullivan <sullivan@sikurezza.org> wrote:
> 
> > 	I was thinking about some pretty __builtin* feature that could
> > 	returns the number of argument passed to a function ...
> 
> This just can't be computed in general.  There's no such thing as a
> magic terminator that GCC could use for that.  And using the FP is not
> always possible.

	I would imagine ;)

[snip]
> 
> If all args are of the same type, and you know that type, you could
> declare an auto array, initialize it with ##args, then divide the size
> of the array by the size of a single element.  But this would evaluate
> the arguments twice.

	Yeah, this could be an idea.

> 
> But then, why don't you use
> 
> #define EXPAND_ARGS(some, args...) ex_args(some, ##args, NULL)
> 
> ?

	... and this sounds really good!

	Thx a lot :)
	
> 
> -- 
> Alexandre Oliva    Enjoy Guaraná, see http://www.ic.unicamp.br/~oliva/
> Cygnus Solutions, a Red Hat company        aoliva@{redhat, cygnus}.com
> Free Software Developer and Evangelist    CS PhD student at IC-Unicamp
> oliva@{lsd.ic.unicamp.br, gnu.org}   Write to mailing lists, not to me
> 

bye bye

				-- gg sullivan

-- 
Lorenzo Cavallaro	`Gigi Sullivan' <sullivan@sikurezza.org>

Until I loved, life had no beauty;
I did not know I lived until I had loved. (Theodor Korner)

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2000-05-29 14:42 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-05-28 17:39 Knowing the number of variable arguments Gigi Sullivan
2000-05-28 18:18 ` Alexandre Oliva
2000-05-28 18:27   ` Gigi Sullivan
2000-05-29 11:18     ` Alexandre Oliva
2000-05-29 12:27       ` Mike Corbeil
2000-05-29 14:01         ` Gigi Sullivan
2000-05-29 14:23           ` Alexandre Oliva
2000-05-29 14:42             ` Gigi Sullivan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).