[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Re: Reference counting

jameskuyper

6/15/2011 9:43:00 PM

On Tuesday, June 14, 2011 6:00:21 PM UTC-4, Tim Rentsch wrote:
> James Kuyper <james...@verizon.net> writes:
>
> > On 06/10/2011 02:30 PM, ImpalerCore wrote:
....
> >> #define PTR_HEADER_SIZE (sizeof (int))
> >>
> >> void* rc_malloc( size_t size )
> >> {
> >> void* mem = NULL;
> >> void* p = NULL;
> >>
> >> p = malloc( size + PTR_HEADER_SIZE );
> >>
> >> if ( p )
> >> {
> >> /* Set the reference count to 1. */
> >> *((int*)p) = 1;
> >>
> >> mem = (unsigned char*)p + PTR_HEADER_SIZE;
> >> }
> >>
> >> return mem;
> >> }
> >
> > The value stored in 'p' is guaranteed to be correctly aligned for all
> > types. However, the only thing portably guaranteed about alignment off
> > the value stored in 'mem' is that it is suitable for 'int', it need not
> > be suitably aligned for any type with a size greater than 1 that is
> > different from that of 'int'. [snip]
>
> Unless one considers the type 'unsigned int' to be a type
> different from that of 'int'.

I was talking about types "with a size ... that is different from that of 'int'". 'unsigned int' is a type with a size that is guaranteed to be the same as that of 'int', so that type is just as safe as 'int' itself.
3 Answers

Ian Collins

6/15/2011 10:01:00 PM

0

On 06/16/11 09:42 AM, jameskuyper wrote:

Please don't use the sociopathic "new" google interface, it ruins threading!

--
Ian Collins

James Kuyper

6/16/2011 2:56:00 AM

0

On 06/15/2011 06:01 PM, Ian Collins wrote:
> On 06/16/11 09:42 AM, jameskuyper wrote:
>
> Please don't use the sociopathic "new" google interface, it ruins threading!

Sorry about that. I normally post from my home machine, and use Google
only for tracking the newsgroups when not at home. I should have been
more careful about responding.
--
James Kuyper

Tim Rentsch

6/19/2011 9:59:00 PM

0

jameskuyper <jameskuyper@gmail.com> writes:

> On Tuesday, June 14, 2011 6:00:21 PM UTC-4, Tim Rentsch wrote:
>> James Kuyper <james...@verizon.net> writes:
>>
>> > On 06/10/2011 02:30 PM, ImpalerCore wrote:
> ...
>> >> #define PTR_HEADER_SIZE (sizeof (int))
>> >>
>> >> void* rc_malloc( size_t size )
>> >> {
>> >> void* mem = NULL;
>> >> void* p = NULL;
>> >>
>> >> p = malloc( size + PTR_HEADER_SIZE );
>> >>
>> >> if ( p )
>> >> {
>> >> /* Set the reference count to 1. */
>> >> *((int*)p) = 1;
>> >>
>> >> mem = (unsigned char*)p + PTR_HEADER_SIZE;
>> >> }
>> >>
>> >> return mem;
>> >> }
>> >
>> > The value stored in 'p' is guaranteed to be correctly aligned for all
>> > types. However, the only thing portably guaranteed about alignment off
>> > the value stored in 'mem' is that it is suitable for 'int', it need not
>> > be suitably aligned for any type with a size greater than 1 that is
>> > different from that of 'int'. [snip]
>>
>> Unless one considers the type 'unsigned int' to be a type
>> different from that of 'int'.
>
> I was talking about types "with a size ... that is different from that
> of 'int'". 'unsigned int' is a type with a size that is guaranteed to
> be the same as that of 'int', so that type is just as safe as 'int'
> itself.

I see - the unspecified referent of the final "that" allowed
the earlier statement to be read ambiguously.

But the assertion about size isn't right either. The pointer
value in 'mem' is suitably aligned for any type whose size
evenly divides 'sizeof (int)', or for that matter any type
whose alignment evenly divides 'sizeof (int)'. The second
condition is conservatively testable (no false positives,
only potentially false negatives), and the first is exactly
testable, using completely portable code.