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


Groups > comp.lang.c++ > #6967

Re: Strict aliasing and buffer handling

From "Paul" <pchristor@yahoo.co.uk>
Newsgroups comp.lang.c++
References <f70612e6-c4e9-4166-842a-afc158fba36e@k6g2000yqc.googlegroups.com>
Subject Re: Strict aliasing and buffer handling
Message-ID <UtMLp.5255$Rw7.298@newsfe28.ams2> (permalink)
Organization virginmedia.com
Date 2011-06-20 19:39 +0100

Show all headers | View raw


"Francois Duranleau" <xiao.bai.xiong@gmail.com> wrote in message 
news:f70612e6-c4e9-4166-842a-afc158fba36e@k6g2000yqc.googlegroups.com...
> Hi,
>
> I am facing a situation where I would like to follow strict aliasing,
> but, unless I misunderstand the standard, I can't find a solution
> without sacrificing performance, memory size, etc.
>
> The problem is as follows. Essentially, I have a runtime description
> of a buffer's layout in memory. The information is essentially a set
> of offset (in bytes) with type of data located at that offset. There
> is an API I use that needs the data to be sent in that particular
> layout. A common method of dealing with such a buffer is something
> like this:
>
> - Buffer is allocated like this:
>
> char* buffer = new char[buffer_size];
>
> - Assigning/querying data from that buffer is done with something like
> this:
>
> template < typename T >
> void set_buffer_data(char* buffer, unsigned int offset, const T& data)
> {
>    T* d = reinterpret_cast<T*>(buffer + offset);
>    *d = data;
> }
>
> template < typename T >
> const T& get_buffer_data(const char* buffer, unsigned int offset)
> {
>    const T* d = reinterpret_cast<const T*>(buffer + offset);
>    return *d;
> }
>
> I am volontarily simplifying things to better illustrate the problem,
> so never mind alignment and type safety problems, and proper
> initialization of the buffer's content with non-POD types.
>
> Now, if I read the standard correctly (3.10-15), this is a violation
> of strict aliasing because casting from char* to T* (T != char or
> unsigned char) and then accessing the data via that pointer is not
> allowed. But then, how can we handle this without sacrificing
> performance and memory?
>
> We could do memcpy, e.g.
>
> template < typename T >
> void set_buffer_data(char* buffer, unsigned int offset, const T& data)
> {
>    memcpy(buffer + offset, &data, sizeof(T));
> }
>
> template < typename T >
> void get_buffer_data(const char* buffer, unsigned int offset, T& data)
> {
>    memcpy(&data, buffer + offset, sizeof(T));
> }
>
> and hope the compiler optimizes this well. But then, what about non-
> PODs? And get_buffer_data now needs to return a copy all the times (I
> could live with that, though, as most of the times, the return value
> would be assigned to a non-reference variable anyways).
>
> For the record, a concrete example for this problem is when dealing
> with OpenGL's uniform buffers (I guess it would be similar with
> D3D10+'s constant buffers). At runtime, you get a layout description
> of a buffer, and you have to respect that layout to send the data.
>
> --
Have you considered placement new ?
Maybe that is another option for you. 

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


Thread

Strict aliasing and buffer handling Francois Duranleau <xiao.bai.xiong@gmail.com> - 2011-06-20 09:59 -0700
  Re: Strict aliasing and buffer handling "Paul" <pchristor@yahoo.co.uk> - 2011-06-20 19:39 +0100
    Re: Strict aliasing and buffer handling Francois Duranleau <xiao.bai.xiong@gmail.com> - 2011-06-20 11:56 -0700
      Re: Strict aliasing and buffer handling Leigh Johnston <leigh@i42.co.uk> - 2011-06-20 20:12 +0100
        Re: Strict aliasing and buffer handling Francois Duranleau <xiao.bai.xiong@gmail.com> - 2011-06-20 12:39 -0700
          Re: Strict aliasing and buffer handling Leigh Johnston <leigh@i42.co.uk> - 2011-06-20 20:49 +0100
            Re: Strict aliasing and buffer handling "Paul" <pchristor@yahoo.co.uk> - 2011-06-20 21:12 +0100
              Re: Strict aliasing and buffer handling Leigh Johnston <leigh@i42.co.uk> - 2011-06-20 21:20 +0100
                Re: Strict aliasing and buffer handling "Paul" <pchristor@yahoo.co.uk> - 2011-06-21 10:35 +0100
                Re: Strict aliasing and buffer handling Leigh Johnston <leigh@i42.co.uk> - 2011-06-21 12:35 +0100
                Re: Strict aliasing and buffer handling "Paul" <pchristor@yahoo.co.uk> - 2011-06-21 12:37 +0100
          Re: Strict aliasing and buffer handling "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-20 22:16 +0200
            Re: Strict aliasing and buffer handling Francois Duranleau <xiao.bai.xiong@gmail.com> - 2011-06-20 13:29 -0700
              Re: Strict aliasing and buffer handling "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-20 23:27 +0200
                Re: Strict aliasing and buffer handling Joshua Maurice <joshuamaurice@gmail.com> - 2011-06-20 14:37 -0700
                Re: Strict aliasing and buffer handling "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-21 00:31 +0200
                Re: Strict aliasing and buffer handling Joshua Maurice <joshuamaurice@gmail.com> - 2011-06-20 19:37 -0700
                Re: Strict aliasing and buffer handling Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-20 23:38 +0200
                Re: Strict aliasing and buffer handling "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-21 00:46 +0200
                Re: Strict aliasing and buffer handling Francois Duranleau <xiao.bai.xiong@gmail.com> - 2011-06-21 05:03 -0700
                Re: Strict aliasing and buffer handling "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-21 16:19 +0200
                Re: Strict aliasing and buffer handling Francois Duranleau <xiao.bai.xiong@gmail.com> - 2011-06-21 09:08 -0700
                Re: Strict aliasing and buffer handling "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-22 01:43 +0200
  Re: Strict aliasing and buffer handling Joshua Maurice <joshuamaurice@gmail.com> - 2011-06-20 14:49 -0700
  Re: Strict aliasing and buffer handling "io_x" <a@b.c.invalid> - 2011-06-21 09:12 +0200
    Re: Strict aliasing and buffer handling Ian Collins <ian-news@hotmail.com> - 2011-06-21 20:14 +1200

csiph-web