Re: Question about the clc string lib
Keith Thompson schrieb:
> "aegis" <aegis@xxxxxxxxxxxxxxxxx> writes:
> > Jordan Abel wrote:
> [...]
> >> take SIZE_MAX 65535
> >>
> >> char * foo = calloc(256,256);
> >> memset(foo,"x",SIZE_MAX);
> >
> > I'm pretty sure this is undefined behavior.
>
> How so? The calloc() call can either succeed or fail; I don't see any
> permission for it to do anything else. The memset() call is ok (if
> calloc() succeeded). Note that it doesn't set the entire object; it
> leaves the last byte as '\0'.
>
> > It parallels, I think, with an issue brought up
> > months ago(in comp.std.c) that labeled the
> > following as undefined behavior.
> >
> > int array[10][10];
> >
> > array[0][10] = 10;
> >
> > and that each array object is not guaranteed to
> > be adjacent to the other.
>
> As I understand the argument, the array elements are guaranteed to be
> adjacent; the assignment invokes undefined behavior because an
> implementation could do explicit bounds checking, not because the
> address might be invalid. I don't see the connection between this and
> the calloc() issue.
>
Douglas Gwyn presented a segmented memory model by where
the offsets from a particular base address of one of the arrays
would not exactly lead access to an adjacent array. By this reason
it was regarded as invoking undefined behavior. Because calloc
could be provide an object by the same means, it would imply
that the exploitation of accessing an adjacent array with a suitable
offset from the base address of its neighbor, would also invoke
undefined behavior. That is, the as-if rule can be applied here
for objects whose storage duration is allocated and not automatic
or static.
--
aegis
.
Relevant Pages
- Re: SSCANF
... This gives undefined behavior. ... You're trying to modify ... in this context will evalutate to pointers to their ... array of three char). ... (comp.lang.c) - Re: The value of variables
... I write a programe as follows,and compile successfuly. ... end of an array invokes undefined behavior after which *anything* is ... including the nul character. ... (comp.lang.c) - Re: register
... >> 6.3.2.1 says that the implicit conversion of an array expression to a ... >> pointer to its first element invokes undefined behavior if the ... >> designated array object has register storage class. ... gcc complains "error: address of register variable 'a' requested" on ... (comp.lang.c) - Re: Compiler difference
... However, when I checked other examples of array and its size, they tend to ... Attempts to do so result in 'undefined behavior' ... Evalutating the value of this pointer will ... > String literals, by definition are not allowed to be ... (alt.comp.lang.learn.c-cpp) - Re: array positioning question
... undefined behavior, which means that there is no way to predict anything. ... The inner cycle in your 'sort' function iterates through all values of ... the array and try to process it together with the existing elements. ... at the end and consequently resulted in original elements residing in ... (comp.lang.cpp) |
|