473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

call of variadic function

Consider the variadic function with the following prototype:
int foo(int num,...);
Here 'num' specifies the number of arguments, and assume that all the
arguments that should be passed to this function are of type int.
(My question has nothing to do with the definition of the function
foo, so don't bother about it.)

If I call the function as:
foo(2,3,4,5,6,7,8);/*More arguments than expected*/

Here I call foo with too many arguments than expected by its
definition.

Assuming that the prototype is visible in the scope of the function
call and the definition of function foo itself does NOT produce any
UB, please tell me if this function call produces UB or not.

I am not able to find any explicit clause from C99 which deals with
this behavior.

Though I find that:

7.19.6.1 The fprintf function
The fprintf function writes output to the stream pointed to by stream,
under control of the string pointed to by format that specifies how
subsequent arguments are converted for output. 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.

This is highly specific to fprintf. I wonder if it applies to all
variadic functions.

Can I assume that the behavior of this function call is UB by
ommission?
Or am I missing something, here?
Please clarify

Thanks in advance for reply.

Jun 17 '07 #1
9 3246
CryptiqueGuy wrote:
Consider the variadic function with the following prototype:
int foo(int num,...);
Here 'num' specifies the number of arguments, and assume that all the
arguments that should be passed to this function are of type int.
(My question has nothing to do with the definition of the function
foo, so don't bother about it.)

If I call the function as:
foo(2,3,4,5,6,7,8);/*More arguments than expected*/

Here I call foo with too many arguments than expected by its
definition.
Not really: You call foo with more arguments than are
expected by its documentation, not by its definition. The
definition can accommodate any strictly positive number of
arguments.
Assuming that the prototype is visible in the scope of the function
call and the definition of function foo itself does NOT produce any
UB, please tell me if this function call produces UB or not.
No UB. The function is not required to "run the va_list
all the way to the end." It is not even required to initialize
the va_list at all:

void foo(int count, ...) {
if (phaseOfMoon() == FULL) {
puts ("Hoooowwwwwllll!");
}
else {
va_list ap;
va_start(ap, count);
...
va_end(ap);
}
}
I am not able to find any explicit clause from C99 which deals with
this behavior.
Neither can I, but there are a few passages that seem
suggestive:

Footnote 123 to 6.7.5.3p9: "The macros [...] may be
used to access arguments that correspond to the
ellipsis." Note the use of the word "may," not
"must" or "shall." (The latter would be out of
place in a non-normative footnote anyhow.)

7.15p3: "[...] If access to the varying arguments
is desired [...]" Use of <stdarg.hfacilities is
needed only "if" access is desired, implying that
access might not be desired, in which case va_xxx
need not be used at all, in which case the ...
arguments (if any) would not be retrieved.
Though I find that:

7.19.6.1 The fprintf function
The fprintf function writes output to the stream pointed to by stream,
under control of the string pointed to by format that specifies how
subsequent arguments are converted for output. 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.

This is highly specific to fprintf. I wonder if it applies to all
variadic functions.
fprintf() gets a special guarantee because even though it
is a variadic function it might not be implemented with the
<stdarg.hmechanisms. Like all the rest of the implementation,
the library functions need not be written in C, and even if
written in C they are at liberty to use "magical" constructs
not available to ordinary code.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 17 '07 #2
On Jun 17, 11:43 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:
CryptiqueGuy wrote:
Consider the variadic function with the following prototype:
int foo(int num,...);
Here 'num' specifies the number of arguments, and assume that all the
arguments that should be passed to this function are of type int.
(My question has nothing to do with the definition of the function
foo, so don't bother about it.)
If I call the function as:
foo(2,3,4,5,6,7,8);/*More arguments than expected*/
Here I call foo with too many arguments than expected by its
definition.

Not really: You call foo with more arguments than are
expected by its documentation, not by its definition.
>The definition can accommodate any strictly positive number of
arguments.
Can you elaborate, on the last sentence of yours?
>
Assuming that the prototype is visible in the scope of the function
call and the definition of function foo itself does NOT produce any
UB, please tell me if this function call produces UB or not.

No UB.
This function call might be intended not to produce UB by the
standards but where is the required specification which makes this
call well defined?
>The function is not required to "run the va_list
all the way to the end." It is not even required to initialize
the va_list at all:
Yes, I agree that the implementation of the function is very much left
to its implementer's choice. So the implementer might implement the
function the way you implemented foo.

But what is the behavior of the function call:
foo(1,2,3,); for the following function, assuming that phaseOfMoon()
returns FULL, so that all the va_start and allies are not executed?
>
void foo(int count, ...) {
if (phaseOfMoon() == FULL) {
puts ("Hoooowwwwwllll!");
}
else {
va_list ap;
va_start(ap, count);
...
va_end(ap);
}
}
I am not able to find any explicit clause from C99 which deals with
this behavior.

Neither can I, but there are a few passages that seem
suggestive:
Can this be considered a glitch with the standards?
>
Footnote 123 to 6.7.5.3p9: "The macros [...] may be
used to access arguments that correspond to the
ellipsis." Note the use of the word "may," not
"must" or "shall." (The latter would be out of
place in a non-normative footnote anyhow.)
As you had already specified we can't base our discussions on the
footnotes.
>
7.15p3: "[...] If access to the varying arguments
is desired [...]" Use of <stdarg.hfacilities is
needed only "if" access is desired, implying that
access might not be desired, in which case va_xxx
need not be used at all, in which case the ...
arguments (if any) would not be retrieved.
Ok, this could be interpreted as, if you want you could use stdarg.h
which is the standard way to access arguments of a variadic function
or else go for some compiler specific funtion calls.
Here the else clause of my sentence specifies one of the numerous non-
portable way of accessing the arguments.

So my conclusion is that explicit statement of behavior is definitely
expected out of the standards when we call a variadic function with
more number of arguments than expected.
Absence of explicit statement of the behavior means that it is a UB.
So the function call I gave in my original post produces UB.

Now, I want to know where do I go wrong in my interpretation, if the
function call I gave in my original question is not a UB?
>
Though I find that:
7.19.6.1 The fprintf function
The fprintf function writes output to the stream pointed to by stream,
under control of the string pointed to by format that specifies how
subsequent arguments are converted for output. 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.
This is highly specific to fprintf. I wonder if it applies to all
variadic functions.

fprintf() gets a special guarantee because even though it
is a variadic function it might not be implemented with the
<stdarg.hmechanisms. Like all the rest of the implementation,
the library functions need not be written in C, and even if
written in C they are at liberty to use "magical" constructs
not available to ordinary code.
This is the thing that keeps me wondering. When an explicit behavior
is stated for special functions like fprintf and friends, why no
behavior is said about a generalised variadic function?

Is an issue like this had already come up in comp.lang.c or
comp.std.c?

A final question, not related to this topic.
Do you think I need to cross post this question at comp.std.c? I think
that I have a poor response to my question, probably because I chose a
wrong group for this topic.

I thank you for replying to my question.
Jun 18 '07 #3
CryptiqueGuy wrote On 06/18/07 12:32,:
On Jun 17, 11:43 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>>CryptiqueGuy wrote:
>>>Consider the variadic function with the following prototype:
int foo(int num,...);
Here 'num' specifies the number of arguments, and assume that all the
arguments that should be passed to this function are of type int.
(My question has nothing to do with the definition of the function
foo, so don't bother about it.)
>>>If I call the function as:
foo(2,3,4,5,6,7,8);/*More arguments than expected*/
>>>Here I call foo with too many arguments than expected by its
definition.

Not really: You call foo with more arguments than are
expected by its documentation, not by its definition.

>>The definition can accommodate any strictly positive number of
arguments.


Can you elaborate, on the last sentence of yours?
One fixed argument (corresponding to num) plus zero
or more variable arguments (for `...'). One plus zero or
more equals one or more, hence "strictly positive."
>
>>>Assuming that the prototype is visible in the scope of the function
call and the definition of function foo itself does NOT produce any
UB, please tell me if this function call produces UB or not.

No UB.


This function call might be intended not to produce UB by the
standards but where is the required specification which makes this
call well defined?
What could be wrong with the call? foo() is declared
to take one int plus any number of other arguments of any
(promoted) types at all, so any call you can write is valid
so long as it has at least one argument and the first is
convertible to int.

The call is unimpeachable, so any uncertainty you have
must be related to what foo() does after it is called. We
know that it may (if it wants) use <stdarg.hfacilities to
retrieve the `...' arguments; we know it can retrieve them
several times if it wants. There is no reason to think that
it is obliged to retrieve them any particular number of times,
nor that any particular "pass" over the arguments must run
all the way through the whole list. We know the behavior
is undefined if foo() tries to retrieve more arguments than
are provided, or if it tries to retrieve arguments whose
type does not agree with the (promoted) expressions in the
call, and we know the behavior is undefined if a va_start()
is not matched with a va_end(). But nowhere is it said
that va_arg() must be called any specific number of times.
>>The function is not required to "run the va_list
all the way to the end." It is not even required to initialize
the va_list at all:


Yes, I agree that the implementation of the function is very much left
to its implementer's choice. So the implementer might implement the
function the way you implemented foo.

But what is the behavior of the function call:
foo(1,2,3,); for the following function, assuming that phaseOfMoon()
returns FULL, so that all the va_start and allies are not executed?
As shown, the behavior is "diagnostic required" followed
by whatever the compiler chooses to do next. Without the
extraneous comma, foo writes "Hoooowwwwwllll!\n" to stdout
and returns, doing nothing else.
>
> void foo(int count, ...) {
if (phaseOfMoon() == FULL) {
puts ("Hoooowwwwwllll!");
}
else {
va_list ap;
va_start(ap, count);
...
va_end(ap);
}
}

>>>I am not able to find any explicit clause from C99 which deals with
this behavior.

Neither can I, but there are a few passages that seem
suggestive:

Can this be considered a glitch with the standards?
> Footnote 123 to 6.7.5.3p9: "The macros [...] may be
used to access arguments that correspond to the
ellipsis." Note the use of the word "may," not
"must" or "shall." (The latter would be out of
place in a non-normative footnote anyhow.)


As you had already specified we can't base our discussions on the
footnotes.

> 7.15p3: "[...] If access to the varying arguments
is desired [...]" Use of <stdarg.hfacilities is
needed only "if" access is desired, implying that
access might not be desired, in which case va_xxx
need not be used at all, in which case the ...
arguments (if any) would not be retrieved.


Ok, this could be interpreted as, if you want you could use stdarg.h
which is the standard way to access arguments of a variadic function
or else go for some compiler specific funtion calls.
Here the else clause of my sentence specifies one of the numerous non-
portable way of accessing the arguments.

So my conclusion is that explicit statement of behavior is definitely
expected out of the standards when we call a variadic function with
more number of arguments than expected.
The function "expects" any strictly positive number of
arguments; that's what its definition says. What it then
decides to do with those arguments is its own business; it
is not required to "pay attention" to every one of them.

Let's try to simplify things by considering a (perhaps)
more transparent function:

int choose(int x, int y) {
return x;
}

Do you think this function invokes undefined behavior? It
uses only one of its two arguments, ignoring the other. Does
the Standard explicitly allow choose() to ignore `y'? Do
you think it needs to?
Absence of explicit statement of the behavior means that it is a UB.
So the function call I gave in my original post produces UB.
Does the Standard state explictly that it is permissible
to arrange the cases of a switch in descending order? Does
the Standard state explicitly that it is permissible to write
a statement label and never use it in a goto?
Now, I want to know where do I go wrong in my interpretation, if the
function call I gave in my original question is not a UB?
>>>Though I find that:
>>>7.19.6.1 The fprintf function
The fprintf function writes output to the stream pointed to by stream,
under control of the string pointed to by format that specifies how
subsequent arguments are converted for output. 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.
>>>This is highly specific to fprintf. I wonder if it applies to all
variadic functions.

fprintf() gets a special guarantee because even though it
is a variadic function it might not be implemented with the
<stdarg.hmechanisms. Like all the rest of the implementation,
the library functions need not be written in C, and even if
written in C they are at liberty to use "magical" constructs
not available to ordinary code.


This is the thing that keeps me wondering. When an explicit behavior
is stated for special functions like fprintf and friends, why no
behavior is said about a generalised variadic function?

Is an issue like this had already come up in comp.lang.c or
comp.std.c?

A final question, not related to this topic.
Do you think I need to cross post this question at comp.std.c? I think
that I have a poor response to my question, probably because I chose a
wrong group for this topic.
Since your question is about the interpretation of the
meaning of the Standard, I think it would be on-topic for
c.s.c. (I also think you'll get the same answer I've given
you -- but you already know that's what I think ;-)

--
Er*********@sun.com
Jun 18 '07 #4
On Mon, 18 Jun 2007 16:32:04 -0000, CryptiqueGuy
<SR**********@gmail.comwrote:
>On Jun 17, 11:43 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>CryptiqueGuy wrote:
Consider the variadic function with the following prototype:
int foo(int num,...);
Here 'num' specifies the number of arguments, and assume that all the
arguments that should be passed to this function are of type int.
(My question has nothing to do with the definition of the function
foo, so don't bother about it.)
If I call the function as:
foo(2,3,4,5,6,7,8);/*More arguments than expected*/
Here I call foo with too many arguments than expected by its
definition.

Not really: You call foo with more arguments than are
expected by its documentation, not by its definition.
>>The definition can accommodate any strictly positive number of
arguments.

Can you elaborate, on the last sentence of yours?
foo can be called with 1 argument, with 2 arguments, ..., with any
positive number of arguments up to the implementation limit for
quantity of arguments.
Remove del for email
Jun 18 '07 #5

"Eric Sosman" <Er*********@sun.comha scritto nel messaggio
news:1182188575.394182@news1nwk...
CryptiqueGuy wrote On 06/18/07 12:32,:
[asking wheter calling a variadic function with more arguments that
it uses is UB]
>Absence of explicit statement of the behavior means that it is a UB.
So the function call I gave in my original post produces UB.

Does the Standard state explictly that it is permissible
to arrange the cases of a switch in descending order? Does
the Standard state explicitly that it is permissible to write
a statement label and never use it in a goto?
It doesn't even say that the function call
#include <stdio.h>
#include <string.h>
memset(stdout, 11, sizeof(FILE));

can cause UB. But I wouldn't expect a program to continue working
after it.
Jun 19 '07 #6
On Jun 18, 10:42 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
CryptiqueGuy wrote On 06/18/07 12:32,:


On Jun 17, 11:43 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>CryptiqueGuy wrote:
>>Consider the variadic function with the following prototype:
int foo(int num,...);
Here 'num' specifies the number of arguments, and assume that all the
arguments that should be passed to this function are of type int.
(My question has nothing to do with the definition of the function
foo, so don't bother about it.)
>>If I call the function as:
foo(2,3,4,5,6,7,8);/*More arguments than expected*/
>>Here I call foo with too many arguments than expected by its
definition.
Not really: You call foo with more arguments than are
expected by its documentation, not by its definition.
>The definition can accommodate any strictly positive number of
arguments.
Can you elaborate, on the last sentence of yours?

One fixed argument (corresponding to num) plus zero
or more variable arguments (for `...'). One plus zero or
more equals one or more, hence "strictly positive."
Well, I can't think like that because I thought this specific function
call I gave is UB.
Now the problem is resolved. I agree with your statement.
>

>>Assuming that the prototype is visible in the scope of the function
call and the definition of function foo itself does NOT produce any
UB, please tell me if this function call produces UB or not.
No UB.
This function call might be intended not to produce UB by the
standards but where is the required specification which makes this
call well defined?

What could be wrong with the call? foo() is declared
to take one int plus any number of other arguments of any
(promoted) types at all, so any call you can write is valid
so long as it has at least one argument and the first is
convertible to int.

The call is unimpeachable, so any uncertainty you have
must be related to what foo() does after it is called. We
know that it may (if it wants) use <stdarg.hfacilities to
retrieve the `...' arguments; we know it can retrieve them
several times if it wants. There is no reason to think that
it is obliged to retrieve them any particular number of times,
nor that any particular "pass" over the arguments must run
all the way through the whole list. We know the behavior
is undefined if foo() tries to retrieve more arguments than
are provided, or if it tries to retrieve arguments whose
type does not agree with the (promoted) expressions in the
call, and we know the behavior is undefined if a va_start()
is not matched with a va_end(). But nowhere is it said
that va_arg() must be called any specific number of times.
>The function is not required to "run the va_list
all the way to the end." It is not even required to initialize
the va_list at all:
Yes, I agree that the implementation of the function is very much left
to its implementer's choice. So the implementer might implement the
function the way you implemented foo.
But what is the behavior of the function call:
foo(1,2,3,); for the following function, assuming that phaseOfMoon()
returns FULL, so that all the va_start and allies are not executed?

As shown, the behavior is "diagnostic required" followed
by whatever the compiler chooses to do next. Without the
extraneous comma, foo writes "Hoooowwwwwllll!\n" to stdout
and returns, doing nothing else.
oops, I have not noticed that comma! It was a typographic error.
Anyway, you are right in stating that compiler will have to generate
diagnostic.
>

void foo(int count, ...) {
if (phaseOfMoon() == FULL) {
puts ("Hoooowwwwwllll!");
}
else {
va_list ap;
va_start(ap, count);
...
va_end(ap);
}
}
>>I am not able to find any explicit clause from C99 which deals with
this behavior.
Neither can I, but there are a few passages that seem
suggestive:
Can this be considered a glitch with the standards?
Footnote 123 to 6.7.5.3p9: "The macros [...] may be
used to access arguments that correspond to the
ellipsis." Note the use of the word "may," not
"must" or "shall." (The latter would be out of
place in a non-normative footnote anyhow.)
As you had already specified we can't base our discussions on the
footnotes.
7.15p3: "[...] If access to the varying arguments
is desired [...]" Use of <stdarg.hfacilities is
needed only "if" access is desired, implying that
access might not be desired, in which case va_xxx
need not be used at all, in which case the ...
arguments (if any) would not be retrieved.
Ok, this could be interpreted as, if you want you could use stdarg.h
which is the standard way to access arguments of a variadic function
or else go for some compiler specific funtion calls.
Here the else clause of my sentence specifies one of the numerous non-
portable way of accessing the arguments.
So my conclusion is that explicit statement of behavior is definitely
expected out of the standards when we call a variadic function with
more number of arguments than expected.

The function "expects" any strictly positive number of
arguments; that's what its definition says. What it then
decides to do with those arguments is its own business; it
is not required to "pay attention" to every one of them.

Let's try to simplify things by considering a (perhaps)
more transparent function:

int choose(int x, int y) {
return x;
}

Do you think this function invokes undefined behavior? It
uses only one of its two arguments, ignoring the other. Does
the Standard explicitly allow choose() to ignore `y'? Do
you think it needs to?
Absence of explicit statement of the behavior means that it is a UB.
So the function call I gave in my original post produces UB.

Does the Standard state explictly that it is permissible
to arrange the cases of a switch in descending order? Does
the Standard state explicitly that it is permissible to write
a statement label and never use it in a goto?
Your argument makes sense to me. It is quite convincing. Now the issue
has become psychological!
I *thought* that, passing more arguments, which is not read by the
variadic function using va_start et al or using some compiler specific
way, is as bad as dereferencing a null pointer. So the entire problem
was with my biased perception of this issue.

Now, I am convinced that it isn't UB.
Thank you very much for your cogent explanations.
>

Now, I want to know where do I go wrong in my interpretation, if the
function call I gave in my original question is not a UB?
>>Though I find that:
>>7.19.6.1 The fprintf function
The fprintf function writes output to the stream pointed to by stream,
under control of the string pointed to by format that specifies how
subsequent arguments are converted for output. 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.
>>This is highly specific to fprintf. I wonder if it applies to all
variadic functions.
fprintf() gets a special guarantee because even though it
is a variadic function it might not be implemented with the
<stdarg.hmechanisms. Like all the rest of the implementation,
the library functions need not be written in C, and even if
written in C they are at liberty to use "magical" constructs
not available to ordinary code.
This is the thing that keeps me wondering. When an explicit behavior
is stated for special functions like fprintf and friends, why no
behavior is said about a generalised variadic function?
Is an issue like this had already come up in comp.lang.c or
comp.std.c?
A final question, not related to this topic.
Do you think I need to cross post this question at comp.std.c? I think
that I have a poor response to my question, probably because I chose a
wrong group for this topic.

Since your question is about the interpretation of the
meaning of the Standard, I think it would be on-topic for
c.s.c. (I also think you'll get the same answer I've given
you -- but you already know that's what I think ;-)

Jun 19 '07 #7
On Jun 19, 6:30 pm, "Army1987" <please....@for.itwrote:
"Eric Sosman" <Eric.Sos...@sun.comha scritto nel messaggionews:1182188575.394182@news1nwk...
CryptiqueGuy wrote On 06/18/07 12:32,:

[asking wheter calling a variadic function with more arguments that
it uses is UB]
Absence of explicit statement of the behavior means that it is a UB.
So the function call I gave in my original post produces UB.
Does the Standard state explictly that it is permissible
to arrange the cases of a switch in descending order? Does
the Standard state explicitly that it is permissible to write
a statement label and never use it in a goto?

It doesn't even say that the function call
#include <stdio.h>
#include <string.h>
memset(stdout, 11, sizeof(FILE));

can cause UB. But I wouldn't expect a program to continue working
after it.
I think you are stating that subsequent usage of stdout either
explicitly (using fprintf,fputc etc.) or implicitly (using printf,
putc etc.) produces UB.
Agreed, that it is a UB, if there is any usage of stdout.
But there won't be any if you don't use it.

The standards do state that subsequent usage of stdout produces UB!
Well, if you ask me how, here I give my explanation.
Functions like fprintf uses FILE pointer to do their job. So there
will be dereferencing of the FILE pointer in their function call. In
such a case if dereferencing is done on the pointer that does not
point to a *valid* object, this produces UB. This applies to your case
also.

If your library function does not dereference FILE pointer or doesn't
use it at all for any pointer arithmetics (in that case it should be
using some complex mechanism!), then there will be no UB. But I think
such implementations are highly unlikely though not disallowed.
Jun 19 '07 #8
On Jun 19, 9:29 pm, CryptiqueGuy <SRRajesh1...@gmail.comwrote:
On Jun 19, 6:30 pm, "Army1987" <please....@for.itwrote:


"Eric Sosman" <Eric.Sos...@sun.comha scritto nel messaggionews:1182188575.394182@news1nwk...
CryptiqueGuy wrote On 06/18/07 12:32,:
[asking wheter calling a variadic function with more arguments that
it uses is UB]
>Absence of explicit statement of the behavior means that it is a UB.
>So the function call I gave in my original post produces UB.
Does the Standard state explictly that it is permissible
to arrange the cases of a switch in descending order? Does
the Standard state explicitly that it is permissible to write
a statement label and never use it in a goto?
It doesn't even say that the function call
#include <stdio.h>
#include <string.h>
memset(stdout, 11, sizeof(FILE));
can cause UB. But I wouldn't expect a program to continue working
after it.
I think you are stating that subsequent usage of stdout either
explicitly (using fprintf,fputc etc.) or implicitly (using printf,
putc etc.) produces UB.

I meant putchar. Sorry.

Jun 19 '07 #9

"CryptiqueGuy" <SR**********@gmail.comha scritto nel messaggio news:11**********************@d30g2000prg.googlegr oups.com...
On Jun 19, 6:30 pm, "Army1987" <please....@for.itwrote:
>"Eric Sosman" <Eric.Sos...@sun.comha scritto nel messaggionews:1182188575.394182@news1nwk...
CryptiqueGuy wrote On 06/18/07 12:32,:

[asking wheter calling a variadic function with more arguments that
it uses is UB]
>Absence of explicit statement of the behavior means that it is a UB.
So the function call I gave in my original post produces UB.
Does the Standard state explictly that it is permissible
to arrange the cases of a switch in descending order? Does
the Standard state explicitly that it is permissible to write
a statement label and never use it in a goto?

It doesn't even say that the function call
#include <stdio.h>
#include <string.h>
memset(stdout, 11, sizeof(FILE));

can cause UB. But I wouldn't expect a program to continue working
after it.

I think you are stating that subsequent usage of stdout either
explicitly (using fprintf,fputc etc.) or implicitly (using printf,
putc etc.) produces UB.
Agreed, that it is a UB, if there is any usage of stdout.
But there won't be any if you don't use it.

The standards do state that subsequent usage of stdout produces UB!
Well, if you ask me how, here I give my explanation.
Functions like fprintf uses FILE pointer to do their job. So there
will be dereferencing of the FILE pointer in their function call. In
such a case if dereferencing is done on the pointer that does not
point to a *valid* object, this produces UB. This applies to your case
also.

If your library function does not dereference FILE pointer or doesn't
use it at all for any pointer arithmetics (in that case it should be
using some complex mechanism!), then there will be no UB. But I think
such implementations are highly unlikely though not disallowed.
#include <stdio.h>
#include <string.h>

static FILE foo;

int main(void)
{
memcpy(stdout, &foo, sizeof foo);
puts("hello, world");
return 0;
}

(Now I'm no longer completely serious, the FILE object may contain
pointers which become NULL after the call to memcpy. But I would
expect the standard to explicitly state that if a FILE object is
modified other than by a library function, then passing a pointer
to it to a library function causes UB.)
Jun 19 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
5
by: Christopher Benson-Manica | last post by:
Do variadic functions (such as printf()) always scan the format string for format specifiers, even when there are no additional arguments? If it is instead a QoI issue, what would be true for a...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
34
by: Srinu | last post by:
Hi all, Can we assign return value of a function to a global variable? As we know, main() will be the first function to be executed. but if the above is true, then we have a function call before...
9
by: Yannick | last post by:
Hi everyone - I am not quite sure to understand what is really going on when a function defined in one translation unit calls a function defined in a different translation unit without knowing...
8
by: Christof Warlich | last post by:
Hi, is there any way to access individual elements in the body of a variadic macro? I only found __VA_ARGS__, which always expands to the complete list. Thanks for any help, Christof
5
by: viza | last post by:
Hi all Can one reliably call a function via a function pointer of different type as below? struct some_struct { int some_int; }; int some_function( struct some_struct *struct_ptr ){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.