Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

calling function with inadequate args (via function pointer)

2 views
Skip to first unread message

Felix Kater

unread,
Jan 11, 2007, 7:40:16 AM1/11/07
to
Hi,

what does gcc do in case I call a function with not enough or too many
arguments like this:

int f(int i, int k){ /* ... */ return 0; }

int main(int argc, char** argv){

int(*g)(int,...);

g=((*)(int,...))f;

g(0); /* not enough arguments */
g(0,1,2); /* too many arguments */

return 0;
}

Besides the fact that the arguments inside f may not contain reliable
values: Is this code without any memory leaks or other danger?

Thank You

Felix

Paul Pluzhnikov

unread,
Jan 11, 2007, 11:17:15 AM1/11/07
to
Felix Kater <fka...@googlemail.com> writes:

> what does gcc do in case I call a function with not enough or too many
> arguments like this:

It does exactly what you told it -- calls the functions with not
enough or too many args.

> int f(int i, int k){ /* ... */ return 0; }

> Besides the fact that the arguments inside f may not contain reliable


> values: Is this code without any memory leaks or other danger?

We can't tell: it depends on what "k" is used for ...

// no danger whatsoever
int f(int i, int k) { return i+k; }

// danger of starting 3rd world war due to uninitialized "k"
int f(int i, int k) { if (k) nuke_Korea(); return 0; }

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.

Felix Kater

unread,
Jan 12, 2007, 2:55:41 AM1/12/07
to
On Thu, 11 Jan 2007 08:17:15 -0800
Paul Pluzhnikov <ppluzhn...@charter.net> wrote:

> // danger of starting 3rd world war due to uninitialized "k"
> int f(int i, int k) { if (k) nuke_Korea(); return 0; }

Ok, that's what I expected for not enough arguments.

However, what's with the second call of g where the function is called
with too many arguments? Will this cause errors on return of the
function (since 3 pushed to the stack and just 2 pop'ed or whatever) ?

Felix

Paul Pluzhnikov

unread,
Jan 12, 2007, 9:43:59 AM1/12/07
to
Felix Kater <fka...@googlemail.com> writes:

> However, what's with the second call of g where the function is called
> with too many arguments? Will this cause errors on return of the
> function (since 3 pushed to the stack and just 2 pop'ed or whatever) ?

There are several popular calling conventions.

Many calling conventions (e.g. SPARC and x86_64) pass first N args
in registers. Obviously there is nothing to push and pop, so there
is no danger.

On x86 under Linux, the calling convention is that caller pushes
*and* pops, so there is no danger either.

On Win32/x86, there is "stdcall" calling convention where caller
pushes but callee pops. That convention can not work for functions
with variable number of args (callee has no idea how many to pop),
and you could get into trouble when you cast stdcall function
pointer to a "regular" function pointer.

0 new messages