I'm implementing ring buffer with dynamic resizing. When tail is behind head, data from the end of the buffer has to be moved to new end of buffer after resize. In order to do so I made following code:
memmove(self->broadcaster.events+self->broadcaster.events_head+self->broadcaster.events_size,
self->broadcaster.events+self->broadcaster.events_head,
self->broadcaster.events_size-self->broadcaster.events_head);
where self->broadcaster.events_size
is old size (new_size/2). Unfortunately it results in segmentation fault. I thought it's quivalent of this code:
for (i = 0 ; i < self->broadcaster.events_size - self->broadcaster.events_head ; ++i)
self->broadcaster.events[self->broadcaster.events_size+self->broadcaster.events_head+i]=
self->broadcaster.events[self->broadcaster.events_head+i];
but such naive for-loop implementation works properly so it seems I don't know how to use memmove
properly. How those two pieces of code are different?