Re: what is difference between sizeof and strlen
"Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> writes:[color=blue]
> Anton Petrusevich wrote on 08/08/05 :[color=green][color=darkred]
>>> "Undefined behaviour" is a technical term in the C standard. It's not
>>> really relevant whether *you* would call it that.[/color]
>>
>> I don't get it. I thought, and I still continue to think, that "undefined
>> behaviour" means just that: when the behaviour is undefined. If the
>> behaviour is dependent on some defined thing (platform) then it's
>> "defined". You (or I) just need to know the platform. Of course, there's no
>> such (defined) thing as "platform" in C standard, so they say "undefined
>> behaviour". "int main()" is a tough religion, isn't it?[/color]
>
> No matter what you think. Just stick to the facts. The C standard
> defines a number of behaviours. Some others are clearly under the
> responsability of the implementation (implementation-dependent). The
> rest is simply 'undefined'. It's a fact. Period. There is nothing
> really to argue about it.
>
> What is undefined can do anything. The result is not predictable and
> not reproducable. It's a bug. Period.[/color]
To clarify (and disagree slightly) ...
The phrase "undefined behavior", as used in this newsgroup, refers to
behavior that is not defined by the C standard. Such behavior might
or might not be defined by something else (the hardware, some
secondary standard, or whatever).
As far as the C standard is concerned, code that exhibits undefined
behavior can do anything; we jokingly talk about it making demons fly
out your nose. This doesn't mean that nasal demons are a real
possibility, just that if demons *do* fly out your nose it doesn't
mean the C implementation is non-conforming.
In some cases, it can make sense to do something that's UB as far as
the C standard is concerned, *if* you have some other guarantee that
it will behave as you expect. You just need to be aware that the code
is non-portable -- and you should probably spend some time thinking
about whether there's a portable way to do it (sometimes there isn't).
For example, a union of a double and a struct containing carefully
defined bitfields can be a good way to get at the components of a
floating-point variable (sign, exponent, mantissa) -- but only at the
risk of having the code break when you port it, or when you upgrade to
a new compiler, or when your sysadmin upgrades your compiler behind
your back.
In other cases, undefined behavior is truly dangerous with no
corresponding benefit. For example, gets() cannot be used safely; it
will overflow your input buffer at the whim of the user.
It can also be useful to understand the *likely* consequences of
particular forms of undefined behavior, in general or for a particular
platform. An example I've mentioned here before is void main(); it
invokes undefined behavior, and it should be corrected, but if your
program is blowing up on line 137 you should also look elsewhere for
the cause.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"Emmanuel Delahaye" <emdel@YOURBRAn oos.fr> writes:[color=blue]
> Anton Petrusevich wrote on 08/08/05 :[color=green][color=darkred]
>>> "Undefined behaviour" is a technical term in the C standard. It's not
>>> really relevant whether *you* would call it that.[/color]
>>
>> I don't get it. I thought, and I still continue to think, that "undefined
>> behaviour" means just that: when the behaviour is undefined. If the
>> behaviour is dependent on some defined thing (platform) then it's
>> "defined". You (or I) just need to know the platform. Of course, there's no
>> such (defined) thing as "platform" in C standard, so they say "undefined
>> behaviour". "int main()" is a tough religion, isn't it?[/color]
>
> No matter what you think. Just stick to the facts. The C standard
> defines a number of behaviours. Some others are clearly under the
> responsability of the implementation (implementation-dependent). The
> rest is simply 'undefined'. It's a fact. Period. There is nothing
> really to argue about it.
>
> What is undefined can do anything. The result is not predictable and
> not reproducable. It's a bug. Period.[/color]
To clarify (and disagree slightly) ...
The phrase "undefined behavior", as used in this newsgroup, refers to
behavior that is not defined by the C standard. Such behavior might
or might not be defined by something else (the hardware, some
secondary standard, or whatever).
As far as the C standard is concerned, code that exhibits undefined
behavior can do anything; we jokingly talk about it making demons fly
out your nose. This doesn't mean that nasal demons are a real
possibility, just that if demons *do* fly out your nose it doesn't
mean the C implementation is non-conforming.
In some cases, it can make sense to do something that's UB as far as
the C standard is concerned, *if* you have some other guarantee that
it will behave as you expect. You just need to be aware that the code
is non-portable -- and you should probably spend some time thinking
about whether there's a portable way to do it (sometimes there isn't).
For example, a union of a double and a struct containing carefully
defined bitfields can be a good way to get at the components of a
floating-point variable (sign, exponent, mantissa) -- but only at the
risk of having the code break when you port it, or when you upgrade to
a new compiler, or when your sysadmin upgrades your compiler behind
your back.
In other cases, undefined behavior is truly dangerous with no
corresponding benefit. For example, gets() cannot be used safely; it
will overflow your input buffer at the whim of the user.
It can also be useful to understand the *likely* consequences of
particular forms of undefined behavior, in general or for a particular
platform. An example I've mentioned here before is void main(); it
invokes undefined behavior, and it should be corrected, but if your
program is blowing up on line 137 you should also look elsewhere for
the cause.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Comment