S
S.Tobias
No. malloc has no way of knowing what such a requirement is.
Of course not. It would have to return maximum object size.
There would have to be maximum object size.
No. malloc has no way of knowing what such a requirement is.
CBFalconer said:As I read the following from N869:
7.20.3 Memory management functions
[#1] The order and contiguity of storage allocated by
successive calls to the calloc, malloc, and realloc
functions is unspecified. The pointer returned if the
allocation succeeds is suitably aligned so that it may be
assigned to a pointer to any type of object and then used to
access such an object or an array of such objects in the
space allocated (until the space is explicitly freed or
reallocated). Each such allocation shall yield a pointer to
an object disjoint from any other object. The pointer
returned points to the start (lowest byte address) of the
allocated space. If the space cannot be allocated, a null
pointer is returned. If the size of the space requested is
zero, the behavior is implementation-defined: either a null
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pointer is returned, or the behavior is as if the size were
some nonzero value, except that the returned pointer shall
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
not be used to access an object. The value of a pointer
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
that refers to freed space is indeterminate.
Is freeing NULL so generated accessing an object? Is freeing
non-NULL so generated accessing an object?
(e-mail address removed)-cnrc.gc.ca (Walter Roberson) wrote:
# In article <[email protected]>,
# >[email protected] (Walter Roberson) wrote:
# ># If realloc() finds it necessary to move the memory block, then
# ># does it free() the previously allocated block?
#
# >You're over-specifying the function. What happens to the block is irrelevant
# >to understanding the function. Use the address returned by realloc; don't
# >worry about what it was before.
#
# The question is not over-specified if one is concerned about
# whether one is leaking memory. When your datasets are ~1 Gb each
# and you are handling them in a loop, you can't afford to allow memory
# to leak.
All you can do to prevent leaks is to match frees to allocs; you don't need to
know how the library is implemented to do that much. It's still leaking
you're stuck unless you can get the library source code.
In other words, if we have a system on which we can detect "failure
to align for any object whatsoever" by just using ordinary pointer
assignments, *then* that system will have to do 16-byte (or whatever)
alignment for a 3-byte malloc; but if the system handles "misaligned"
pointers without trouble, as long as they are not used to access
their misaligned objects, then -- because there will be no way for
the user to tell -- the as-if rule will allow us to implement
malloc(3) with just two-byte alignment.
SM said:# > # ># If realloc() finds it necessary to move the memory block, then
# > # ># does it free() the previously allocated block?
# Nope; that's probably why the OP wanted to know whether realloc() frees
# the block it is passed. If it didn't, and could leak, one could still
# implement a version which doesn't leak using malloc() and free(). From
# the answers to the OP one can conclude that this isn't necessary; but
# the question makes sense.
Some day you'll learn about modular programming. Most malloc libraries promise
not to leak if you match allocates and free properly. So with modular programming
you concentrate on the interface (matching allocates and frees) and not worry
about how the other modules fulfills its promises.
It suffices to know the interface and that you cannot trust the input pointer
to realloc, but rather the output pointer (whether they differ or no).
Trying to figure out if realloc calls free or immediately deallocates or unmaps
pages or anything else is breaking into the module without need. Even worrying
about whether memory moves is irrelevant. Suppose realloc is able to remap a
page so the phyical DRAM appears at a different address. Was memory moved or
not? MU.
I should hope not. If it is, then
int *x;
x=malloc(100);
free(x);
invokes undefined behaviour. When the memory is received from malloc(),
it contains uninitialised bytes; it never is given a real value;
therefore, accessing it is accessing an uninitialised int value, which
invokes UB
Robert Gamble said:The only thing being discussed here is alignmentment requirements. The
above quote states that your object created with malloc will be properly
*aligned* to hold any type, not that any type can actually be stored in it
if the space doesn't exist.
Chris said:Is there anything which says that alignments are always on a power of 2?
If not, then malloc() could have to be very wasteful. Imagine a system
(the DS9011, say) where a byte is 11 bits and the sizes (and alignments)
in bytes are:
Type Alignment
char 1
short 2
int 3
long 5
long long 7
and assigning
Thus malloc() would have to return an area aligned to at least 2*3*5*7
which is 210 bytes, even for malloc(1).
Perhaps it is needed a version of malloc() which takes the size of the
base object as well as the number of them, so
type *p = nmalloc(sizeof(*p), num);
That could look up the size and work out the alignment (if it was not a
basic type then it could just use the sizeof value, since the alignment
for that type must be that value or a submultiple of it).
Not to my knowledge.
ITYM "1*2*3*5*7." Two marks off for bad penmanship.
About a decade ago this question arose with respect to
calloc(), and the eventual conclusion was that calloc() had to
be "oblivious" to the significance of its two operands. That
is, calloc(2,8) and calloc(8,2) had to satisfy exactly the same
alignment constraints: the former could not legitimately decide
that only 2-byte alignment was required.
Mind you, much heated discussion preceded the conclusion,
and it is not certain that all the discussors agreed thereto.
"A man convinced against his will is of the same opinion still."
SM Ryan said:# > # ># If realloc() finds it necessary to move the memory block, then
# > # ># does it free() the previously allocated block?
# Nope; that's probably why the OP wanted to know whether realloc() frees
# the block it is passed. If it didn't, and could leak, one could still
# implement a version which doesn't leak using malloc() and free(). From
# the answers to the OP one can conclude that this isn't necessary; but
# the question makes sense.
Some day you'll learn about modular programming.
Most malloc libraries promise
Walter said:.... snip ...
Let me then go back and rephrase my original question:
If I call realloc() with a non-NULL pointer to a block of
memory that was allocated with a non-zero size, and the new size
is also non-zero, and if realloc() returns a different pointer
than the original pointer, then in C89 may I free() the original
pointer without invoking unspecified or undefined behaviour?
^Walter said::Some day you'll learn about modular programming. Most malloc libraries
romise not to leak if you match allocates and free properly.
CBFalconer said:tigervamp said:Walter said:If realloc() finds it necessary to move the memory block, then
does it free() the previously allocated block?
Yes.
The C89 standard has some reference to undefined behaviour if
one realloc()'s memory that was freed by realloc(), but the only
way explicitly mentioned in the C89 standard to free memory via
realloc() is to realloc() it down to 0 bytes.
Passing a pointer to realloc that points to memory free'd by _any_
function results in undefined behavior. If you successfully
realloc memory which results in the original object being
deallocated, you must provide a pointer to the new object created
by realloc in subsequent realloc calls as the old object has been
freed.
I had always assumed it would automatically free the previous
memory, but is the behaviour instead undefined [or defined as
not happening] ?
No, the behavior is well defined by the Standard, see section
7.20.3 of C99, I don't have a C89 copy handy.
No, I don't consider it well defined. It leaves too much up to the
implementation in the case of zero block size requests. This means
that NULL is not always a sign of failure, and that a non-NULL
return is not necessarily freeable or reallocable.
In my nmalloc implementation for DJGPP I have chosen to treat a
zero block size as a request for one byte, and let the alignment
mechanisms raise it as they will. This ensures that all successful
requests return a non-NULL pointer that can safely be used in
subsequent frees or reallocs. (It also has the convenient to me
side effect of ensuring space for some record-keeping in my code)
I suspect the standard is written to not invalidate the sloppier
malloc implementations already out there. In cases like this I
think it should include a recommended practice.
SM Ryan said:Could you point me to the portion of C89 in which the interface
to realloc() is defined in a way that specifies [one way or
another] what happens to the input block when realloc() finds it
necessary to resort to the storage allocator ?
Of course it doesn't say what happens to the input block. That's the
whole point. The caller is free of concerns about the
implementation of the function, only the interface and its protocol.
My user and I would be happy to live with whatever the interface
specification -is-, but what *is* that specification?? Where is it
written in the C89 standard what exactly will happen?
The specification is the input pointer value is generally no longer valid
and should not be used again in any calls or memory references;
you must instead use the output pointer value. And that in most malloc
libraries freed memory is eventually reused.
The the input pointer remains valid if the output pointer is null and the
length is greater than zero.
Whether the input and output pointers are the same is irrelevant, because
you should only be using the output pointer if the realloc was successful.
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.