beginner with programming, how to learn to debug and few C generalquestions

K

Keith Thompson

Tim Rentsch said:
(e-mail address removed) (Richard Bos) writes: [...]
In other words, it may in theory well be that atoi() is less likely to
have damaging undefined behaviour than gets(); but in practice, anyone
who even spends time pondering that likelihood instead of immediately
going for strtol() is a fool.

Perhaps so. But someone who doesn't distinguish between actual
behavior and Standard-specified behavior is a bigger fool.

I certainly do distinguish between actual behavior and
Standard-specified behavior. This does not lead me to conclude
that atoi() is appropriate in anything other than toy programs or
situations where the argument is tightly controlled (and in the
latter case, it's generally not worth the effort).

What is the actual behavior of atoi("99999999999999999999999999")?
How do you know? Are you sure it will remain the same on any
implementation your code is ever used with?
 
T

Tim Rentsch

Keith Thompson said:
Tim Rentsch said:
(e-mail address removed) (Richard Bos) writes: [...]
In other words, it may in theory well be that atoi() is less likely to
have damaging undefined behaviour than gets(); but in practice, anyone
who even spends time pondering that likelihood instead of immediately
going for strtol() is a fool.

Perhaps so. But someone who doesn't distinguish between actual
behavior and Standard-specified behavior is a bigger fool.

I certainly do distinguish between actual behavior and
Standard-specified behavior. This does not lead me to conclude
that atoi() is appropriate in anything other than toy programs or
situations where the argument is tightly controlled (and in the
latter case, it's generally not worth the effort).

My intention was to make a generic comment without offering any
judgment on the specific case (ie, atoi()).

What is the actual behavior of atoi("99999999999999999999999999")?
How do you know? Are you sure it will remain the same on any
implementation your code is ever used with?

Of course, the answers here are that I don't know for sure. My
point is that there are some situations where questions like
these are relevant, and other situations where they aren't so
relevant. I understand the black/white perspective of defined
versus undefined behavior, but that perspective isn't always the
most appropriate one; it's important to remember that and try to
take a perspective that's appropriate to the particular situation
under consideration. This may not be a deep thought, but it does
seem to bear repeating from time to time.
 
K

Keith Thompson

Tim Rentsch said:
Keith Thompson said:
Tim Rentsch said:
(e-mail address removed) (Richard Bos) writes: [...]
In other words, it may in theory well be that atoi() is less likely to
have damaging undefined behaviour than gets(); but in practice, anyone
who even spends time pondering that likelihood instead of immediately
going for strtol() is a fool.

Perhaps so. But someone who doesn't distinguish between actual
behavior and Standard-specified behavior is a bigger fool.

I certainly do distinguish between actual behavior and
Standard-specified behavior. This does not lead me to conclude
that atoi() is appropriate in anything other than toy programs or
situations where the argument is tightly controlled (and in the
latter case, it's generally not worth the effort).

My intention was to make a generic comment without offering any
judgment on the specific case (ie, atoi()).
What is the actual behavior of atoi("99999999999999999999999999")?
How do you know? Are you sure it will remain the same on any
implementation your code is ever used with?

Of course, the answers here are that I don't know for sure. My
point is that there are some situations where questions like
these are relevant, and other situations where they aren't so
relevant. I understand the black/white perspective of defined
versus undefined behavior, but that perspective isn't always the
most appropriate one; it's important to remember that and try to
take a perspective that's appropriate to the particular situation
under consideration. This may not be a deep thought, but it does
seem to bear repeating from time to time.

Under what circumstances, in your opinion, is it acceptable to use
atoi(), with its risk of undefined behavior, rather than strtol(),
which with a little care can be used safely?
 
B

Ben Pfaff

Keith Thompson said:
Under what circumstances, in your opinion, is it acceptable to use
atoi(), with its risk of undefined behavior, rather than strtol(),
which with a little care can be used safely?

Quite seriously: when you just don't care very much. That is, in
trivial experimental programs, for one.
 
B

Beej Jorgensen

Keith Thompson said:
Under what circumstances, in your opinion, is it acceptable to use
atoi(), with its risk of undefined behavior, rather than strtol(),
which with a little care can be used safely?

Or when success is highly assured (e.g. your inputs will be the numbers
"0" through "63".)

I kinda feel the same way about it as I do "goto". Generally it's not
the best solution, but there are certain cases where it's fine (any
maybe even preferable) to use.

-Beej
 
R

Richard Bos

Keith Thompson said:
Tim Rentsch said:
(e-mail address removed) (Richard Bos) writes: [...]
In other words, it may in theory well be that atoi() is less likely to
have damaging undefined behaviour than gets(); but in practice, anyone
who even spends time pondering that likelihood instead of immediately
going for strtol() is a fool.

Perhaps so. But someone who doesn't distinguish between actual
behavior and Standard-specified behavior is a bigger fool.

Not so. Knowing is one thing; caring is another. In this case,
_especially_ those who know the difference should also know that they
should not care about it.
I certainly do distinguish between actual behavior and
Standard-specified behavior. This does not lead me to conclude
that atoi() is appropriate in anything other than toy programs or
situations where the argument is tightly controlled (and in the
latter case, it's generally not worth the effort).

What is the actual behavior of atoi("99999999999999999999999999")?
How do you know? Are you sure it will remain the same on any
implementation your code is ever used with?

I insist, and will continue to insist, that the onnly important question
is: why the tanj do you _care_ what it is, when you should have been
using strtol() instead?

Richard
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top