Re: Can I do ANYTHING on allocated memory?




"Harald van D?k" <truedfx@xxxxxxxxx> wrote in message
Malcolm
It doesn't even rely on that. You can safely convert any result from
malloc to and from any non-function pointer type. The only time pointer
casts can lose information (or worse) is if the pointer is not properly
aligned, but the pointer is the result from a call to malloc, so it
must be properly aligned.

You are confusing what will happen on any current implementation with what
is guaranteed by the standard.

*The standard is a bit underspecified regarding pointer conversions, but
*does clearly address this part in 6.3.2.3p7: "A pointer to an object or
*incomplete type may be converted to a pointer to a different object or
*incomplete type. If the resulting pointer is not correctly aligned57)
*for the pointed-to type, the behavior is unde?ned. Otherwise, when
*converted back again, the result shall compare equal to the original
*pointer."

You are actaully correct, because of that that phrase "correctly aligned".
As you say, it is a bit under-specified. At the moment all pointers consist
of hardware (1)address plus, occasionally, alignment bits to implement
sub-hardware byte chars. That might change because of technical developments
in the future, though I couldn't think of an obvious scenario.
(1) for the pedants, sometimes a translated hardware address.


.



Relevant Pages

  • Re: How does malloc align pointer
    ... > The pointer returned by malloc(), ... > pointer to any type of object. ... > Also, how does mallocdetermine, which pointer type has the most ... > has most strict alignment storage restriction. ...
    (comp.lang.c)
  • RE: mysql installation problems
    ... Warning: duplicate function definition 'do' detected in mysql.xs, ... from integer without a cast ... dbdimp.c:2583: error: cannot convert to a pointer type ...
    (perl.dbi.users)
  • AW: mysql installation problems
    ... Warning: duplicate function definition 'do' detected in mysql.xs, ... from integer without a cast ... dbdimp.c:2583: error: cannot convert to a pointer type ...
    (perl.dbi.users)
  • AW: mysql installation problems
    ... Warning: duplicate function definition 'rows' detected in mysql.xs, ... from integer without a cast ... dbdimp.c:2583: error: cannot convert to a pointer type ...
    (perl.dbi.users)
  • Re: How does malloc align pointer
    ... > The pointer returned by malloc(), ... > pointer to any type of object. ... > Also, how does mallocdetermine, which pointer type has the most ... > has most strict alignment storage restriction. ...
    (comp.lang.c)