Re: Questions regarding specialized malloc()/free() replacements



Stephan Beal wrote:
blargg wrote:
[...]
void MyType_init()
[...]
void MyType_free( MyType* p )
{
/* This might be technically non-portable, though portable in
practice */
if ( p != NULL && (void*) p >= blocks &&
(void*) p < blocks += max_blocks )

Why shouldn't this be portable? An array must, by definition, be a
contiguous range of memory, which means that any memory from outside
that range *must* compare not-equal.
[...]

Equal, not equal, sure, but the others require that the pointers be
inside the same object:

| 6.5.8 Relational operators
[...]
| 5 When two pointers are compared, the result depends on the relative
| locations in the address space of the objects pointed to. If two
| pointers to object or incomplete types both point to the same object,
| or both point one past the last element of the same array object, they
| compare equal. If the objects pointed to are members of the same
| aggregate object, pointers to structure members declared later compare
| greater than pointers to members declared earlier in the structure,
| and pointers to array elements with larger subscript values compare
| greater than pointers to elements of the same array with lower
| subscript values. All pointers to members of the same union object
| compare equal. If the expression P points to an element of an array
| object and the expression Q points to the last element of the same
| array object, the pointer expression Q+1 compares greater than P. In
| all other cases, the behavior is undefined.
.



Relevant Pages

  • Re: Test if pointer points to allocated memory
    ... >> or one past the same object or array. ... When two pointers are compared, the result depends on the relative ... pointers to structure members declared later compare ... > The typical implementation of automatic storage is on the program stack. ...
    (comp.lang.c)
  • Re: Array comparison
    ... pointers are only going to compare their object pointers ... pointers and not the contents of the strings. ... things like array of Char, bytes, words, dword, int64's etc.. ...
    (alt.comp.lang.borland-delphi)
  • Re: Differance between Array and Pointers
    ... Well, except that arrays tend to be much larger than pointers, and the ... An array is a contiguous region of memory containing N elements of M ... indexing vs. a loop). ...
    (comp.arch.embedded)
  • [RFCv2][PATCH] flexible array implementation
    ... I call it a flexible array. ... storage for pointers to the second level. ... all locking must be provided by the caller. ... make sure to pass in &ptr instead of ptr. ...
    (Linux-Kernel)
  • Re: Test if pointer points to allocated memory
    ... >> Undefined behavior. ... When two pointers are compared, ... last element of the same array object, they compare equal. ...
    (comp.lang.c)