Languages & Coding > C++ / C#

Using a void pointer?

(1/3) > >>

Midimaster:
I tried to reduce three of my C-glue-functions into one. If I could use void-pointers I could use the same same function for the audio formats u8, s16, s32 ans f32.

So I replaced the fix Float-Pointers to universal Void-Pointer and depending on the given format I try o cast them into the necessary type:

before:

--- Code: ---//one pass decoding 32bit float:
int MM_Decode32f(float *TSampleRAM){
....
float* pAudioData;
int result = ma_decode_file(...., &pAudioData);
....
for (i=0; i<100; i++){
TSampleRAM[i] =  pAudioData[i];
}
}
--- End code ---

after:

--- Code: ---//one pass decoding all formats:
int MM_Decode( void* TSampleRAM, int format){
...
void* pAudioData;
switch (format) {
....
case ma_format_f32:
TSampleRAM = (float*) TSampleRAM;
pAudioData = (float*) pAudioData;
break;
}
int result = ma_decode_file(...., &pAudioData);
...
for (i=0; i<100; i++){
TSampleRAM[i] =  pAudioData[i];
}
}

--- End code ---

But now I get the Error Message:

--- Code: ---C:/BlitzMaxNG/mod/mima.mod/miniaudio.mod/miniaudiowrapper.c: In function 'MM_Decode':
C:/BlitzMaxNG/mod/mima.mod/miniaudio.mod/miniaudiowrapper.c:270:17:
error: invalid use of void expression
   TSampleRAM[i] =  pAudioData[i];
                 ^
Build Error: failed to compile (1) C:/BlitzMaxNG/mod/mima.mod/miniaudio.mod/miniaudiowrapper.c
Process complete

--- End code ---

What do I forget?

Steve Elliott:

--- Code: ---Perhaps you need to de-reference the pointer and typecast too - but use the address of (&TSampleRAM) ?

TSampleRAM = *((float*) &TSampleRAM));

--- End code ---

Midimaster:
wow! that looks complicate...

C is still not "my world"

Thank you steve. I will try it out immediately.

Midimaster:
It looks like your's is no solution. I corrected the brackets. (was one to much):

--- Code: ---int MM_Decode( void* TSampleRAM, int format){
...
TSampleRAM = *((float*) &TSampleRAM);


--- End code ---

 but already this gives a new error message:

--- Code: ---C:/BlitzMaxNG/mod/mima.mod/miniaudio.mod/miniaudiowrapper.c: In function 'MM_Decode':
C:/BlitzMaxNG/mod/mima.mod/miniaudio.mod/miniaudiowrapper.c:246:13:
error: incompatible types when assigning to type 'void *' from type 'float'

  TSampleRAM = *((float*) &TSampleRAM);
             ^

--- End code ---


I tried a new approach:

This would work:

--- Code: ---int MM_Decode( void* TSampleRAM, int format){
...
float* TSampleRAM_II;
TSampleRAM_II = (float *) TSampleRAM;
...
for (i=0; i<(frameCount*audioConfig.channels); i++){
TSampleRAM_II[i] =  1;
}

--- End code ---

but when I try this inside the switch, it again does not compile:

--- Code: ---int MM_Decode( void* TSampleRAM, int format){
...
switch (format) {
case ma_format_f32:
float* TSampleRAM_II;
TSampleRAM_II = (float *) TSampleRAM;
break;
}
...
for (i=0; i<(frameCount*audioConfig.channels); i++){
TSampleRAM_II[i] =  1;
}

--- End code ---

now the error message is:

--- Code: ---C:/BlitzMaxNG/mod/mima.mod/miniaudio.mod/miniaudiowrapper.c: In function 'MM_Decode':
C:/BlitzMaxNG/mod/mima.mod/miniaudio.mod/miniaudiowrapper.c:252:4: error: a label can only be part of a statement and a declaration is not a statement
    float* TSampleRAM_II;
    ^~~~~
C:/BlitzMaxNG/mod/mima.mod/miniaudio.mod/miniaudiowrapper.c:259:3: error: 'TSampleRAM_II' undeclared (first use in this function)
   TSampleRAM_II[i] =  1;
   ^~~~~~~~~~~~~

--- End code ---

Steve Elliott:
Ah, it was off the top of my head.  But I wouldn't use void pointers anyway, I would use C++ function overloading for this, so you have the same functions but the data type variance would mean C++ would select the correct version:


--- Code: ---eg)

calc( int data ) {
...
}

calc( float data ) {
...
}

So now if you call calc with calc( 4.5 ); then C++ would know to use the floating point version.

--- End code ---

But if you have large functions then I would suggest breaking your functions into smaller functions.  Getting crazy with pointers can lead to confusing bug ridden code.

Navigation

[0] Message Index

[#] Next page

Go to full version