views:

157

answers:

3

Hi,

I have 2 classes, one that writes multiple 96bit objects to a buffer (32bits at a time - 3x int32), and one that i want to read from the same buffer.

The first class (Writer) reserves and area of memory and creates a pointer to the first 32bit area.

1) How do I write to the buffer safely (ignoring buffer overflow for now)... I need to write in 32bit chunks, so how do I change the position of the 'write pointer' between each 96bit write? Do I do something like:

for(int count = 0; count < 100; ++count)  // for 100 96bit objects
{
    for(int32 i = 0; i < 3; ++i)
    {
        *buffer = *(myInt32 + i);
    }
    // ** how do I move the buffer ptr to the start of the next 96bit memory space? **
}

2) Is it safe for me to reserve the memory, write a number of 96bit objects, then pass the pointer to the beginning of it to the second (Reader) class, to ensure they are both able to access the same objects? (The Reader will read multiples of 96bit objects in one go (~10,000), so I only need to know the start of the data for reading.)

3) Once the Reader has read the buffer, how do I 'reset' the pointer (empty the buffer) so that I can write to the buffer again?

The buffer: The buffer is actually a pointer to the beginning of an area of memory reserved by posix_memalign.

int32 *tempbufferPtr; 
posix_memalign ((void**)&tempbufferPtr, 8, 10000 ); // space for 10,000 objects 
writePtr = (my96bitObjectclass*)tempbufferPtr;
+2  A: 

Just use pointer arithmetic, it will increase by the proper amount (by the size of the pointed-to object).

int32 *ptr = ...;

*ptr++ = 1;
*ptr++ = 2;
*ptr++ = 3;

// Here, ptr has been advanced by 3 times 32 bits, and is pointing at the next location.
unwind
+2  A: 

Simply increment the buffer pointer. This should work:

for(int count = 0; count < 100; ++count)  // for 100 96bit objects
{
    for(int32 i = 0; i < 3; ++i)
    {
        *buffer++ = *(myInt32 + i);
    }
}
codebolt
Does that increment happen before or after the assignment?After writing the 3 ints, will the pointer be pointing in the right place for the next three, next time round the count loop?
Krakkos
The increment happens after each assignment. The buffer should be pointing to the right address at the next iteration.Btw I recommend you write myInt32[i] instead of *(myInt32 + i) for better readability.
codebolt
are myInt32[i] and *(myInt32 + i) equivalent? Do I have to define myInt32 as an array first?
Krakkos
The [] operator works on both conventional arrays and pointers in the same way (the pointer is then treated like an array where the first element is the object it points to).
codebolt
Ah, yes, I remember now, thanks... thats why pointers and arrays are so closely linked.
Krakkos
A: 

Expanding on the pointer idea of unwind:

typedef struct {
    int a; int b; int c;
} myobj_t;

// allocate some memory
myobj_t *myobj_p, *myobj = malloc( ... );

myobj_p = myobj;  // don't want a memory leak
while ( ... ) {
myobj.a = x;
myobj.b = y;
myobj.c = z;

myobj++;
}
Robert S. Barnes