printing a string

H

Harry

Hi All,

I had made a mistake in my code when printing a string.

printf(" Hello world %s\n");;

I executed this statement in a while loop. Everytime the application
was invoked,some junk character was printed.

Is there a chance that this statement will cause a segmentation fault
at a particular instance. I have this doubt my application had quited.
I have a doubt that this statement could have caused a segmentation
fault and code would have exited though i have corrected it now.

Any comments on this?
 
N

Nick Keighley

Hi All,

I had made a mistake in my code when printing a string.

printf(" Hello world %s\n");;

I executed this statement in a while loop. Everytime the application
was invoked,some junk character was printed.

Is there a chance that this statement will cause a segmentation fault
at a particular instance. I have this doubt my application had quited.
I have a doubt that this statement could have caused a segmentation
fault and code would have exited though i have corrected it now.

Any comments on this?

it's undefined behaviour (the standard doesn't specify how an
implementation
will behave) so anything at all can occur.

You can pretty well guarantee the program will access some storage
location
that isn't well defined or may not even exist. I don't see any point
on
going furthur with this. Just don't write code like this.
 
R

raashid bhatt

Hi All,

I had made a mistake in my code when printing a string.

printf(" Hello world %s\n");;

I executed this statement in a while loop. Everytime the application
was invoked,some junk character was printed.

Is there a chance that this statement will cause a segmentation fault
at a particular instance. I have this doubt my application had quited.
I have a doubt that this statement could have caused a segmentation
fault and code would have exited though i have corrected it now.

Any comments on this?

Its actually a UB. And in may cases it will cause segmentation fault
%s here takes the address of the adjacent value in the stack and if it
point to a invalid location then it will surely cause a segmentation
fault
 
K

Keith Thompson

raashid bhatt said:
Its actually a UB. And in may cases it will cause segmentation fault
%s here takes the address of the adjacent value in the stack and if it
point to a invalid location then it will surely cause a segmentation
fault

Perhaps. C doesn't mention a stack, nor does it specify how arguments
are passed.

In practice, the "%s" is likely to cause printf to attempt to
access a string at an address that it will assume was passed as the
second argument. Which means either that it will attempt to grab
a string from some arbitrary address, or that it will choke while
attempting to determine the address before it even has a chance to
grab the string.

My description is somewhat more general than yours, but it still makes
assumptions that are not supported by the language. All you can
really say is that the behavior is undefined. Understanding how the
UB is likely to manifest can be helpful in tracking down and
correcting this kind of problem.
 
H

Harry

Perhaps.  C doesn't mention a stack, nor does it specify how arguments
are passed.

In practice, the "%s" is likely to cause printf to attempt to
access a string at an address that it will assume was passed as the
second argument.  Which means either that it will attempt to grab
a string from some arbitrary address, or that it will choke while
attempting to determine the address before it even has a chance to
grab the string.

My description is somewhat more general than yours, but it still makes
assumptions that are not supported by the language.  All you can
really say is that the behavior is undefined.  Understanding how the
UB is likely to manifest can be helpful in tracking down and
correcting this kind of problem.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Thanks for the explanation.
I have a similar doubt. What will happen in the following case

printf("Child PID is d\n", (long) getpid());

Actually it has to be %ld there,but because of a mistake, it was just
d. So the output was 'child pid is d',but again i suppose the
behaviour is undefined. Because i have seen a case where the
application had terminated without this print.
 
N

Nick Keighley

I'm not sure what you mean. Did your application crash?

did it exit or not?


don't quote sigs (the bit after " --")
Thanks for the explanation.
I have a similar doubt. What will happen in the following case

printf("Child PID is d\n", (long) getpid());

Actually it has to be %ld there,but because of a mistake, it was just
d.

I think unused additional arguments are harmless. It is certainly
that way on a "typical" application.
So the output was 'child pid is d',but again i suppose the
behaviour is undefined.

probably not
Because i have seen a case where the
application had terminated without this print.

you've seen what? An application that didn't have an erroneous
printf "terminated". By "terminated" do you mean "crashed"?
 
H

Harry

I'm not sure what you mean. Did your application crash?


did it exit or not?



don't quote sigs (the bit after " --")




I think unused additional arguments are harmless. It is certainly
that way on a "typical" application.


probably not


you've seen what? An application that didn't have an erroneous
printf "terminated". By "terminated" do you mean "crashed"?

I had put the wrong print in a thread. The thread had exited at the
line where the wrong print was there. Because the print before the
line was seen in the output.
 
N

Nobody

I have a similar doubt. What will happen in the following case

printf("Child PID is d\n", (long) getpid());

Actually it has to be %ld there,but because of a mistake, it was just
d. So the output was 'child pid is d',but again i suppose the
behaviour is undefined. Because i have seen a case where the
application had terminated without this print.

Excess arguments to a variadic function are harmless on all common
platforms. I dont't think that this is guaranteed by the standard, but
I couldn't name a platform where it won't work.

Excess arguments to a non-variadic function may be problematic in some
cases, primarily for the "stdcall" calling convention normally used by
functions exported from Win32 DLLs.
 
K

Keith Thompson

Nobody said:
Excess arguments to a variadic function are harmless on all common
platforms. I dont't think that this is guaranteed by the standard, but
I couldn't name a platform where it won't work.

It's guaranteed by the standard for fprintf, and therefore for all the
standard *printf functions. C99 7.19.6.1p2:

If the format is exhausted while arguments remain, the excess
arguments are evaluated (as always) but are otherwise ignored.

I *think* the behavior is well defined for variadic functions
in general, particularly for variadic functions that use the
<stdarg.h> mechanism. I don't see an explicit statement of this,
but there's nothing in the description of <stdarg.h> (C99 7.15)
that says va_end can't be invoked before exhausting the arguments.
(There's an explicit statement for fprintf because the standard
doesn't require fprintf to use said:
Excess arguments to a non-variadic function may be problematic in some
cases, primarily for the "stdcall" calling convention normally used by
functions exported from Win32 DLLs.

Passing excess arguments to a non-variadic function invokes undefined
behavior.
 
B

Beej Jorgensen

Keith Thompson said:
It's guaranteed by the standard for fprintf, and therefore for all the
standard *printf functions. C99 7.19.6.1p2:

Oh, man... would that mean this is allowable:

printf("world!\n", printf("Hello, "));

(GCC does issue a diagnostic message with -Wall: "too many arguments for
format".)

-Beej
 
K

Keith Thompson

Beej Jorgensen said:
Oh, man... would that mean this is allowable:

printf("world!\n", printf("Hello, "));

Yes. Lots of very silly things are allowable.
(GCC does issue a diagnostic message with -Wall: "too many arguments for
format".)

Not surprising. Passing too many arguments to printf is usually
a logical error, but its behavior is well defined by the language,
so a warning is appropriate. If gcc treated this as a fatal error,
it would be non-conforming.
 
F

Flash Gordon

Beej said:
Oh, man... would that mean this is allowable:

printf("world!\n", printf("Hello, "));
Yes.

(GCC does issue a diagnostic message with -Wall: "too many arguments for
format".)

It is allowed to, and it is helpful, but it is not required and there is
no undefined behaviour.
 
P

Phil Carmody

Beej Jorgensen said:
Oh, man... would that mean this is allowable:

printf("world!\n", printf("Hello, "));

Well, you've seen ',' as a sequencing operator, here it's a
reverse sequencing operator!

(And yes, I know, it's not, it's the function calls that sequence.)

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top