Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #76940

Re: Strict aliasing and restrict

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Strict aliasing and restrict
Date 2015-11-23 11:59 +1300
Message-ID <dbevm7FjpqnU14@mid.individual.net> (permalink)
References (1 earlier) <87poz3o789.fsf@bsb.me.uk> <slrnn50q9b.5q5.grahn+nntp@frailea.sa.invalid> <5831fcba-8de0-4e11-8ef9-8f6229d63935@googlegroups.com> <779099d5-1301-4f43-a2e9-a54f429d84d7@googlegroups.com> <f0b3423a-1782-4321-906a-b325bc7a891f@googlegroups.com>

Show all headers | View raw


supercat@casperkitty.com wrote:
> On Sunday, November 22, 2015 at 5:41:19 AM UTC-6, Malcolm McLean wrote:
>> On Saturday, November 21, 2015 at 5:14:18 PM UTC, supercat wrote:
>>> Standard C makes it literally impossible to perform anything resembling
>>> efficient type-agnostic memory management and recycling without using malloc
>>> and free to scrub memory every time it is recycled.
>
>> There's a glitch in the standard that makes it impossible to write a conforming
>> malloc() in standard C. That's that malloc has to return a pointer aligned for
>> any use, and there's no way to specify such an alignment.
>
> That could easily be overcome.  There's a *MUCH* bigger problem, which for
> some reason a lot of programmers are blind to.
>
>>       It needs to return the biggest block, so you lose a bit of memory, but
>> you can reuse the block at will, without even calling the allocator again if
>> three machine instructions of overhead is too much for you.
>
> You don't see the real problem.  The real problem is that given:
>
>      float *fp = _box_alloc(pool); // Allocation function in rtx
>      *fp = 12.34;
>      _box_free(pool, fp);
>      float *ip = _box_alloc(pool); // Allocation function in rtx
>      *ip = 5;
>      printf("%d", *ip);
>      _box_free(pool, ip);
>
> there's no decent way to prevent a compiler from rewriting the code as:
>
>      float *fp = _box_alloc(pool); // Allocation function in rtx
>      _box_free(pool, fp);
>      float *ip = _box_alloc(pool); // Allocation function in rtx
>      *ip = 5;
>      *fp = 12.34;
>      printf("%d", *ip);
>      _box_free(pool, ip);

This would change the observable behaviour of the code.  A more likely 
rearrangement would be:

     float *fp = _box_alloc(pool); // Allocation function in rtx
     _box_free(pool, fp);
     float *ip = _box_alloc(pool); // Allocation function in rtx

     *fp = 12.34;
     *ip = 5;

     printf("%d", *ip);
     _box_free(pool, ip);

which does not change the observable behaviour of the code.

-- 
Ian Collins

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Strict aliasing and restrict Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-11-21 13:12 +0100
  Re: Strict aliasing and restrict James Kuyper <jameskuyper@verizon.net> - 2015-11-21 07:39 -0500
    Re: Strict aliasing and restrict supercat@casperkitty.com - 2015-11-21 09:59 -0800
  Re: Strict aliasing and restrict Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-21 12:41 +0000
    Re: Strict aliasing and restrict Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-21 12:52 +0000
      Re: Strict aliasing and restrict supercat@casperkitty.com - 2015-11-21 09:14 -0800
        Re: Strict aliasing and restrict Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-21 19:41 +0000
          Re: Strict aliasing and restrict Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-21 14:57 -0500
            Re: Strict aliasing and restrict Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-22 11:02 +0000
          Re: Strict aliasing and restrict supercat@casperkitty.com - 2015-11-21 17:50 -0800
            Re: Strict aliasing and restrict Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-22 11:18 +0000
              Re: Strict aliasing and restrict supercat@casperkitty.com - 2015-11-22 07:45 -0800
        Re: Strict aliasing and restrict Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-22 03:41 -0800
          Re: Strict aliasing and restrict supercat@casperkitty.com - 2015-11-22 08:02 -0800
            Re: Strict aliasing and restrict Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-22 14:39 -0800
            Re: Strict aliasing and restrict Ian Collins <ian-news@hotmail.com> - 2015-11-23 11:59 +1300
              Re: Strict aliasing and restrict supercat@casperkitty.com - 2015-11-22 15:22 -0800
            Re: Strict aliasing and restrict Stephen Sprunk <stephen@sprunk.org> - 2015-11-22 19:22 -0600
          Re: Strict aliasing and restrict raltbos@xs4all.nl (Richard Bos) - 2015-11-23 14:07 +0000
  Re: Strict aliasing and restrict Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-21 08:17 -0500
  Re: Strict aliasing and restrict supercat@casperkitty.com - 2015-11-21 09:45 -0800
    Re: Strict aliasing and restrict Stephen Sprunk <stephen@sprunk.org> - 2015-11-22 16:30 -0600
      Re: Strict aliasing and restrict supercat@casperkitty.com - 2015-11-22 15:18 -0800
  Re: Strict aliasing and restrict Stephen Sprunk <stephen@sprunk.org> - 2015-11-21 16:39 -0600

csiph-web