Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 19 Aug 2005 00:40:03 +0300
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        Dmitry Mityugov <dmitry.mityugov@gmail.com>
Cc:        questions@freebsd.org
Subject:   Re: man malloc
Message-ID:  <20050818214003.GA687@flame.pc>
In-Reply-To: <b7052e1e05081811172b36182e@mail.gmail.com>
References:  <4303A632.1000809@FreeBSD.org> <4303B016.3030201@mac.com> <43044212.20909@FreeBSD.org> <20050818125348.GG1282@flame.pc> <b7052e1e05081811172b36182e@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2005-08-18 22:17, Dmitry Mityugov <dmitry.mityugov@gmail.com> wrote:
>On 8/18/05, Giorgos Keramidas <keramida@ceid.upatras.gr> wrote:
>>On 2005-08-18 12:08, Sergey Matveychuk <sem@freebsd.org> wrote:
>>>Chuck Swiger wrote:
>>>>>What is pointer coercion? I have no pointer before malloc() returns.
>>>>
>>>> Right.  Well, malloc returns a (void *), but most people want to use the
>>>> memory malloc returns to hold their own arrays, structs, whatever, which
>>>> means that you need to be able to coerce the (void *) malloc gave you
>>>> into whatever pointer type you want to actually use.
>>>>
>>>> So the memory malloc gives you needs to be aligned so that it's OK to be
>>>> used for even the most restrictive datatype known to the system,
>>>> commonly 8, 16, or 32 bytes.
>>>
>>> Pointer coercion means a type cast? I see now.
>>> I read it as 'force change of pointer value' before.
>>
>> It may be surprising, but casting back and forth *MAY* change the value
>> of the pointer.
>...
>
> Could you back up this assertion with an example, please?

Do I really have to?

The standard says that casting is only allowed from (type *) to (void *)
and back to (type *).  This is exactly the reason why malloc() knows
(using its own "magic") what to return, so that you _can_ cast its
result.





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050818214003.GA687>