我试图使用void指针缓冲区创建一个通用的循环缓冲区,我遇到了一些我不明白的问题。
如果我对缓冲区使用双指针,我的循环缓冲区的行为与预期的一样(参见下面的test1),但是如果我使用void指针,我会得到一个错误的行为(参见test2),尽管所有的void指针都是双指针。我的演员出了什么问题?
还有一个问题:在circular_buffer_write_chunk
中,我使用双指针传递数据。我需要像使用void指针一样传递它以使其成为通用的。然后我必须在函数中动态地将它强制转换为缓冲区的类型(可以是double
或int
),以使指针算法工作。我怎样才能做到这一点?如何获取指向缓冲区的指针类型,然后将我的数据指针转换为该类型?
任何评论或建议欢迎。
test1 使用缓冲区作为双指针运行circular_buffer_test()
*** circular_buffer test***
capacity: 12
Write 5 values
cb[0]=0.000000
cb[1]=1.000000
cb[2]=2.000000
cb[3]=3.000000
cb[4]=4.000000
cb[5]=0.000000
cb[6]=0.000000
cb[7]=0.000000
cb[8]=0.000000
cb[9]=0.000000
cb[10]=0.000000
cb[11]=0.000000
Write 10 values
cb[0]=12.000000
cb[1]=13.000000
cb[2]=14.000000
cb[3]=3.000000
cb[4]=4.000000
cb[5]=5.000000
cb[6]=6.000000
cb[7]=7.000000
cb[8]=8.000000
cb[9]=9.000000
cb[10]=10.000000
cb[11]=11.000000
Test done
test2 使用缓冲区作为void指针运行circular_buffer_test()
。为什么会有所不同?
*** circular_buffer test***
capacity: 12
Write 5 values
cb[0]=0.000000
cb[1]=1.000000
cb[2]=2.000000
cb[3]=3.000000
cb[4]=4.000000
cb[5]=0.000000
cb[6]=0.000000
cb[7]=0.000000
cb[8]=0.000000
cb[9]=0.000000
cb[10]=0.000000
cb[11]=0.000000
Write 10 values
cb[0]=12.000000
cb[1]=13.000000
cb[2]=14.000000
cb[3]=0.000000 // ?
cb[4]=0.000000 // ?
cb[5]=0.000000 // ?
cb[6]=0.000000 // ?
cb[7]=0.000000 // ?
cb[8]=0.000000 // ?
cb[9]=0.000000 // ?
cb[10]=0.000000 // ?
cb[11]=0.000000 // ?
Test done
circular_buffer.c
#include "circular_buffer.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct
{
double *buffer;
double *buffer_end;
unsigned int capacity;
double *read_ptr;
double *write_ptr;
unsigned int count;
} circular_buffer;
/*
// Why doesn't this behave like with double pointer, since it's casted in circular_buffer_init_double?
typedef struct
{
void *buffer;
void *buffer_end;
unsigned int capacity;
void *read_ptr;
void *write_ptr;
unsigned int count;
} circular_buffer;
*/
void circular_buffer_init_double(circular_buffer *cb, unsigned int capacity)
{
cb->buffer = (double*) malloc(sizeof(double) * capacity);
cb->buffer_end = (double*) (cb->buffer + capacity);
cb->capacity = capacity;
cb->read_ptr = (double*) (cb->buffer);
cb->write_ptr = (double*) (cb->buffer);
cb->count = 0;
}
void circular_buffer_destroy(circular_buffer *cb){
free(cb->buffer);
cb->buffer = NULL;
}
int circular_buffer_is_full(circular_buffer *cb) {
return cb->count == cb->capacity;
}
int circular_buffer_is_empty(circular_buffer *cb) {
return cb->count == 0;
}
unsigned int circular_buffer_write_chunk(circular_buffer* cb, double *data, unsigned int type_size,
unsigned int num_elements){
if( num_elements > cb->capacity ) return 0;
if( cb->write_ptr + num_elements > cb->buffer_end ){
unsigned int write_ptr_to_buffer_end = cb->buffer_end - cb->write_ptr;
unsigned int write_ptr_position = cb->write_ptr - cb->buffer;
memcpy( cb->write_ptr, data, write_ptr_to_buffer_end * type_size );
memcpy( cb->buffer, data + write_ptr_to_buffer_end,
( num_elements - write_ptr_to_buffer_end) * type_size );
cb->write_ptr = cb->buffer + num_elements - write_ptr_to_buffer_end;
}
else{
memcpy( cb->write_ptr, data, type_size * num_elements );
cb->write_ptr += num_elements;
if( cb->write_ptr == cb->buffer_end ){
cb->write_ptr = cb->buffer;
}
}
if( circular_buffer_is_full(cb) ) cb->read_ptr = cb->write_ptr;
cb->count = cb->count + num_elements > cb->capacity ? cb->capacity : cb->count + num_elements;
return num_elements;
}
void circular_buffer_test(){
fprintf(stdout, "*** circular_buffer test***\n");
circular_buffer *cb = malloc(sizeof(circular_buffer));
circular_buffer_init_double(cb, 12);
fprintf(stdout, "capacity: %d\n", cb->capacity);
int i;
double *w1 = malloc(sizeof(double) * 5);
for(i=0; i<5; i++) w1[i] = (double) i;
fprintf(stdout, "\nWrite 5 values\n");
circular_buffer_write_chunk(cb, w1, sizeof(double), 5);
for(i=0; i<12; i++) fprintf(stdout, "cb[%d]=%f\n", i, ((double *)cb->buffer)[i]);
fprintf(stdout, "\nWrite 10 values\n");
double *w2 = malloc(sizeof(double) * 10);
for(i=5; i<15; i++) w2[i-5] = (double) i;
circular_buffer_write_chunk(cb, w2, sizeof(double), 10);
for(i=0; i<12; i++) fprintf(stdout, "cb[%d]=%f\n", i, ((double *)cb->buffer)[i]);
free(w1);
free(w2);
circular_buffer_destroy(cb);
fprintf(stdout, "Test done\n");
}
答案 0 :(得分:2)
C:
中不允许使用void *
的指针算术
(double*) (cb->buffer + capacity);
gcc
通过将void *
的大小视为void
,将1
的指针算法添加为扩展名。
buffer
void *
gcc
与double *
或cb->buffer + capacity
((double*) cb->buffer + capacity);
的结果是否不同。使用:
buffer
如果void *
是{{1}}。