S
santosh
arnuld said:On Wed, 06 Aug 2008 06:53:05 +0000, Richard Heathfield wrote:
I mean it depends on the circumstances. For example, if you want to
"sprint" three possibly-negative ints and a ten-byte string,
separated by spaces, you know at compile time that you need
3 * /* three */
((sizeof(int) * CHAR_BIT + 2) / 3) + /* ints, */
1) + /* possibly negative, */
10 + /* and a ten-byte string, */
3 + /* and three separating
spaces, */
1 /* and a null terminator */... SNIP..
I really did not understand the most of itbut I got the idea that
you must know your input size in advance which I already know. e.g if
I am sure that my input is no more than 10 bytes then:
char arrc[10];
sprintf(arrc, "This input is definitely more than 10 bytes");
what will it do. In curiosity I have played with this code:
<snip>
C doesn't say what happens after undefined behaviour is invoked. The
expected result could be derived, the program could crash, the machine
could crash, the fuses for your building's power supply could blow...
In short, there is not much point in experimenting with undefined
behaviour. It is of course nice to see what really happens on a buffer
overrun on your implementation, but undefined behaviour being what it
is, the same "thing" might not happen on the very next run of the same
program, or after the same program is recompiled with differring
options, or if it's run after a system upgrade, or with another
implementation on another machine etc.
Even if an instance of UB is defined and documented by your compiler,
there is no requirement that the next compiler define it or document
what it has defined or define it in a way that is compatible with the
previous compiler's behaviour and so on.