Re: calling convention stdcalll and cdecl call
- From: "Liviu" <lab2k1@xxxxxxxxx>
- Date: Fri, 18 Jul 2008 17:22:40 -0500
"Alf P. Steinbach" <alfps@xxxxxxxx> wrote
* Liviu:
"Alf P. Steinbach" <alfps@xxxxxxxx> wrote
* Igor Tandetnik:
Alf P. Steinbach <alfps@xxxxxxxx> wrote:For what it's worth the above example (1) has undefined behavior
* Igor Tandetnik:
printf("%d", 1, 2);
Nope, behavior is both defined and expected (prints 1, excess
arguments
are evaluated but ignored).
In C99. Thanks, I didn't know. Do you have C89 standard?
Don't have it handy, but if google's
http://rm-f.net/~orange/devel/specifications/c89-draft.html#4.9.6.1
is to be trusted, then it's the same:
| If there are insufficient arguments for the format, the behavior is
| undefined. If the format is exhausted while arguments remain, the
| excess arguments are evaluated (as always) but are otherwise ignored.
You might be thinking at "printf("%d");" but even in that case the
"undefinededness" is particular to printf, and has nothing to do with
calling conventions for variadic functions in general.
It had to do with a minor point that Igor may or may not have been
trying to make, who knows since evidently he doesn't mean what he
writes.
I'll go on a limb here ;-) but I trust Igor meant exactly what he wrote:
| With stdcall, the function is responsible for removing its parameters
| from the stack. To do this, it must know how many parameters there
| are, and thus cannot take variable number of parameters.
But just to address what you say, although unrelated to original
context: I'm sorry, but that's incorrect, i.e. contrary to your claim
the behavior of printf has to do with calling conventions for variadic
functions in general.
C99 printf directly supports a functionally limited and generally
inefficient calling convention (C-style) and for printf, makes the
case that that convention doesn't handle, undefined behavior.
Sorry, but I don't follow. The only undefined behavior mentioned there
is when the caller passes _fewer_ arguments than expected from the
format string, and that's because of printf internals. On the other
hand, the standard explicitly states that excess parameters are
ignored, so it's painfully obvious that printf couldn't possibly clear
the stack after itself based on what the format string says.
Imagine for a moment that you'd be writing the __stdcall printf(const
char *, ...) code. At the point your code would be entered, everything
would look _exactly_ the same when the function is called in either of
these ways:
printf("%d", 1);
printf("%d", 1, 2);
How would you code your printf so that it pops the right number of bytes
off the stack in each case?
I replied originally to Igor by posting complete C++ plus assembly
language code for an example of __stdcall variadic arguments.
No. You just provided several overloads of the 'knurre' function which
happened to internally share some assembly code.
If the point of the exercise was to show that passing an additional
"number of params" into a function can allow it to pop the stack itself,
then yes of course it's both possible and trivial. But it's not
__stdcall, since there is no presumption in __stdcall that such a
"number of params" argument must exist.
And, if you proposed a new calling convention which does that, it would
be highly fragile, and something like
knurre(1, 101, 102, 103);
would not only confuse the callee, but also corrupt the entire stack.
Showing that his claim that it was impossible, was simply incorrect.
I requoted his claim above. What part exactly do you find incorrect?
Cheers,
Liviu
.
- Follow-Ups:
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- References:
- Re: calling convention stdcalll and cdecl call
- From: Igor Tandetnik
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- From: Igor Tandetnik
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- From: Igor Tandetnik
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- From: Igor Tandetnik
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- From: Liviu
- Re: calling convention stdcalll and cdecl call
- From: Alf P. Steinbach
- Re: calling convention stdcalll and cdecl call
- Prev by Date: Re: dialog.domodal causes crash
- Next by Date: Re: calling convention stdcalll and cdecl call
- Previous by thread: Re: calling convention stdcalll and cdecl call
- Next by thread: Re: calling convention stdcalll and cdecl call
- Index(es):
Relevant Pages
|