Re: pointer one past malloc.ed memory

From: pete (pfiland_at_mindspring.com)
Date: 07/12/04


Date: Mon, 12 Jul 2004 10:39:04 GMT

Sushil wrote:
>
> > We generally don't know MIPS assembler and as a C programmer, you
> > shouldn't care. If you care to post some C code which exhibits the
> > problem, several people here might be more able to help you.
> >
> > Note that 0x80000000 is -2147483648 or INT_MIN on 32-bit systems.
>
> Thanks for your reply Joe and Pete.
>
> The simplified code is like this
> magic_struct *magic_ptr = ((magic_struct *) ((uchar *) ptr +
> ptr->size))- 1;
> where magic_struct contains two magic ulongs.
>
> The issue is ptr + ptr->size becomes 0x80000000 and -1 after pointer
> arithmetic is 8 bytes since sizeof(magic_struct) is 8. However, the
> compiler does not do the -8 part since it is compiled with -O3 (okay I
> know this is offtopic). Instead, when magic_ptr->magic1 is referenced
> (later) compiler offsets -8 from the above address at that point. This
> is overflowing and causing an address exception.
>
> The way I fixed it is I rearranged code
> magic_struct *magicptr = ((uchar *) ptr - sizeof(magic_struct)) +
> ptr->size;

If ptr is the lowest byte of the object,
then it is not safe to subtract from it.
It is safe to calculate the integer part first
and then add it the pointer.

    magic_struct *magicptr
        = ptr->size - sizeof(magic_struct) + (uchar *)ptr;

>
> Question 1:
> Do you think this trick(the parentheses) will force it to calculate
> entire thing (it is working right now - but I am not confident it
> always will).

No.

> Question 2:
> What does the language say about the address calculation.

There is a rule which explicitly allows the calculation
of an address one byte beyond an object, as long as it
is not dereferenced. There is no such rule for addresses
lower than an object lowest addressable byte.

> What is the signedness of address when it comes to address arithemtic.
> For example pointer to a struct. Is it signed or unsigned.

No. Pointers are scalar but not integer types.
The difference between two pointers,
is a signed integer type called ptrdiff_t, defined in stddef.h

-- 
pete


Relevant Pages

  • Types
    ... Operations are allowed between compatible types and not between any data whatsoever. ... Each type can have an associated pointer type: for int we have int pointer, for double we have double pointer, etc. ... Specific integer types ... If both specify repetition counts, ...
    (comp.lang.c)
  • Re: Overview Of New Intel Core i7(Nehalem) Processor
    ... the variable name is a pointer. ... An array or structure is handled as a pointer. ... There are five standard signed integer types, ... types are the standard unsigned integer types. ...
    (sci.electronics.design)
  • Re: Conceptual, Logical, and Physical views of data
    ... As an aside, let me just relate that about 2 years ago, I had a lengthy discussion at work in which it was me vs. 2 guys with CS Ph.Ds, with them taking the position that there's no difference between a pointer and a foreign key. ... I don't care what some other developer's 'meaning' for it is as long as that meaning works in his application and doesn't screw up mine. ... I'm not interested in trying to prove it, but I suspect that if one took some of their pronouncements to heart, all logical database development would come to a permanent halt. ... However, if somebody else doesn't see it my way, good for them, and let them explain all the costs, short-term and long-term of their better approach (and better it may be in their particular case, especially if the boss and owners don't care about the long-term in which case I'd hope it was a commercial and not a government 'enterprise') to their boss or customer. ...
    (comp.databases.theory)
  • Re: Are these behaviors defined?
    ... > any of this is covered in the FAQ, please feel free to point me to the ... Overflow of signed integer types produces undefined behavior. ... have mentioned produce undefined behavior if passed a null pointer. ... For left shifts and right shifts of unsigned integer types and signed ...
    (comp.lang.c)