Re: C++ sucks for games

From: Adam Warner (usenet_at_consulting.net.nz)
Date: 11/17/04


Date: Thu, 18 Nov 2004 01:57:30 +1300

Hi rif,

>> Would it help making arrays of (unsigned-byte 8) or (unsigned-byte 32)
>> and writing inlined accessor functions then?
>>
>> Kind regards,
>>
>> Hannah.
>
> It will help for some but not all applications. If you want to have a
> struct whose fields are all the same type, and that type can be inlined
> in arrays, then you can just allocate a big array of that type and build
> syntax to do the right thing. If your structs contain fields of
> different types (say, (unsigned-byte 32)'s *and* double-float's), then
> you have to either pay a memory penalty or move to a "multiple arrays"
> setup (what would naturally be an array of structs, where each struct is
> a two int's and a double-float, becomes an array of ints and an array of
> double-floats).

Not necessarily. Here's single array access for (unsigned-byte 32)'s and
double-float's for CMUCL and SBCL:

(let ((a (make-array 99 :element-type '(unsigned-byte 32)
                        :initial-element 0)))
  (defun get-int (i)
    (aref a (* i 3)))
  (defun set-int (i j)
    (setf (aref a (* i 3)) j))
  (defun get-df (i)
    (#+cmu kernel:make-double-float #+sbcl sb-kernel:make-double-float
     (aref a (+ (* i 3) 1)) (aref a (+ (* i 3) 2))))
  (defun set-df (i j)
    (setf (aref a (+ (* i 3) 1)) (#+cmu kernel:double-float-high-bits
                                  #+sbcl sb-kernel:double-float-high-bits j))
    (setf (aref a (+ (* i 3) 2)) (#+cmu kernel:double-float-low-bits
                                  #+sbcl sb-kernel:double-float-low-bits j))
    j))

* (set-df 10 3.14d0)

3.14d0
* (get-df 10)

3.14d0
* (set-int 10 34)

34
* (get-int 10)

34

Your structure could only have one slot for an array. You could build
function or macro syntax to access the individual elements.

Not as efficient as you'd like but it's still only an extra array
lookup for each integer.

Regards,
Adam



Relevant Pages

  • Re: Passing an array of structuresfrom a pointer?
    ... to only declare structs in headers and then define the ... the struct should be declared ... what if you have a simple array like this: ... In the header we would declare? ...
    (microsoft.public.vc.language)
  • Re: Passing an array of structuresfrom a pointer?
    ... I've tried an attempt to do that, and I tried to "clean" the header and main ..c file. ... int LIST_NUMBER; ... typedef struct ddListBox{ ... DDLB_COLL1array! ...
    (microsoft.public.vc.language)
  • Re: Marshalling Question: Array of Complex Structs?
    ... I've used no managed types within the struct. ... the following is the code used to call the DLL function. ... C style arrays don't have any information about the length of the array, ... byte buffers to fill with character data while the marshalling code ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: LONG: Variable internals and copy-on-write
    ... mxArray structure than ... struct mxArray_tag { ... int vartype; ... The header of a cell array looks identical to the ...
    (comp.soft-sys.matlab)
  • Re: Passing an array of structuresfrom a pointer?
    ... I just tried to access data from the DDLB_COLL1 array and got an error! ... int LIST_NUMBER; ... typedef struct ddListBox{ ... to only declare structs in headers and then define the ...
    (microsoft.public.vc.language)