简体   繁体   English

关于空隙长度的困惑*

[英]Confusion about length of void*

I have a question about void*.我有一个关于 void* 的问题。 I have a function which captures blocks of 0.2 sec of audio by microphone.我有一个功能,可以通过麦克风捕获 0.2 秒的音频块。 I have to process these blocks, in concrete a convolution.我必须处理这些块,具体来说是卷积。

This function returns these blocks of audio as a void * .此函数将这些音频块作为 void * 返回。 To process this information I can't use void * because I can't access to them, so I have to convert in other kind of data, for example double but I don't know which length is assigned to this new pointer or how can I do it.为了处理这些信息,我不能使用 void * 因为我无法访问它们,所以我必须转换其他类型的数据,例如 double 但我不知道哪个长度分配给这个新指针或如何我可以吗。

My code:我的代码:

void Pre_proc_mono::PreProcess(void *data, int lenbytes, float t_max){
    double * aux = (double*) data;
}

Now, aux's length is lenbytes too?现在,aux 的长度也是 lenbytes 吗? Or I have to do something like:或者我必须做类似的事情:

int size = lenbytes/sizeof(double);

How can I make this work?我怎样才能使这项工作?

A pointer is an address in memory.指针是内存中的地址。 This is the address of the first byte of data.这是数据的第一个字节的地址。 The type of the pointer tells you how long is the data.指针的类型告诉您数据的长度。 So if we have所以如果我们有

int *p

the value of p tells you where the data starts, and the type of the pointer, in this case int * tells you that from that address you need to take 4 bytes (on most architectures). p 的值告诉你数据从哪里开始,指针的类型,在这种情况下int *告诉你从那个地址你需要 4 个字节(在大多数架构上)。

A void * pointer has only the starting address, but not the length of the data, so that's why you can't dereference a void * pointer. void *指针只有起始地址,而没有数据的长度,所以这就是为什么不能取消引用void *指针的原因。

sizeof(p) where p is a pointer (of any type) is the size of the pointer, and has nothing to do with the kind of data you find where the pointer points to sizeof(p)其中 p 是指针(任何类型)是指针的大小,与您在指针指向的位置找到的数据类型无关

for instance:例如:

sizeof(char) == 1
sizeof(char *) == 4
sizeof(void *) == 4

In your function:在您的功能中:

void *data, int lenbytes, float t_max

data is a pointer to where the data starts, lenbytes is how many bytes the data has. data是指向数据开始位置的指针, lenbytes是数据有多少字节。

So you can have something like:所以你可以有这样的东西:

uint8_t *aux =  (uint8_t*) data;

and you have a vector of lenbytes elements of type uint8_t (uint8_t is guaranteed to have 1 byte).并且您有一个uint8_t类型的lenbytes元素向量(uint8_t 保证有 1 个字节)。

Or something like this:或者像这样:

double * aux = (double*) data;

and you have a vector of lenbutes/sizeof(double) elements of type double .你必须的矢量lenbutes/sizeof(double)类型的元素double But you need to be careful so that lenbytes is a multiple of sizeof(double) .但是您需要小心,使lenbytessizeof(double)的倍数。

Edit编辑

And as regarding to what you should convert to, the answer depends on only the format of your blocks of data.至于你应该转换成什么,答案只取决于你的数据块的格式。 Read the documentation, or search for an example.阅读文档,或搜索示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM