Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vDSP_ctozD malloc error C++ #16

Closed
q-depot opened this issue Mar 14, 2013 · 9 comments
Closed

vDSP_ctozD malloc error C++ #16

q-depot opened this issue Mar 14, 2013 · 9 comments

Comments

@q-depot
Copy link
Contributor

q-depot commented Mar 14, 2013

I'm trying to implement simpletest.c example in c++, the app throws malloc error, while running Guard Malloc the app breaks on vDSP_ctozD called by XTRACT_SPECTRUM, however despite it breaks, it output the data correctly(spectral bins, MFCCs).

@jamiebullock
Copy link
Owner

To run under a C++ compiler, I think the code in simpletest.c will need a few modifications. I'm not a C++ expert, but I'm certain the return value from malloc() will need an explicit cast, i.e. (double *)malloc(...)

If adding the cast doesn't resolve your problem, can you post a full console trace of the compiler errors.

@jamiebullock
Copy link
Owner

OK — I have now pushed a fix, which adds these casts to the malloc calls. Hopefully this will resolve your problem.

@q-depot
Copy link
Contributor Author

q-depot commented Mar 14, 2013

same problem, I tried to use a C header with extern "C" statement and then define the function in a .c file. This function is eventually called in a C++ app. Alternatively I've implemented the code directly in my C++ file and in both cases I get the same error.

to run the code in C++ I had to change this function:
xtract[XTRACT_SPECTRUM]((void *)&input, BLOCKSIZE, &argd[0], (void *)&spectrum[0]);

to this:
xtract[XTRACT_SPECTRUM] ( input, BLOCKSIZE, argd, spectrum );

I didn't quite understand why you take the array address by dereferencing the array, I guess it's a C thing, in C++ input should return the address to the first block of memory.

any thoughts?

thanks for your help.

@jamiebullock
Copy link
Owner

OK, I'll take a look at this and try to put together a C++-compatible simpletest. I am busy the rest of the day, but can probably push a fix tomorrow morning.

On 14 Mar 2013, at 13:18, Andrea notifications@github.com wrote:

same problem, I tried to use a C header with extern "C" statement and then define the function in a .c file. This function is eventually called in a C++ app. Alternatively I've implemented the code directly in my C++ file and in both cases I get the same error.

to run the code in C++ I had to change this function:
xtractXTRACT_SPECTRUM&input, BLOCKSIZE, &argd[0], (void *)&spectrum[0]);

to this:
xtractXTRACT_SPECTRUM;

I didn't quite understand why you take the array address by dereferencing the array, I guess it's a C thing, in C++ input should return the address to the first block of memory.

any thoughts?

thanks for your help.


Reply to this email directly or view it on GitHub.

@q-depot
Copy link
Contributor Author

q-depot commented Mar 14, 2013

I just published the cinder block on github, even though I'm not sure you are familiar with Cinder

https://github.com/q-depot/ciLibXtract

there is a sample samples/BasicSample which I will use to build the block.

At the moment I tried to implement the code in two different way:

  1. testC() in simpletest.c
    simpletest.h is included in the main app, testC() is then called from the app mouseDown() even handler. I did so because I wanted to ensure testC() was declared inside the extern "C" block in simpletest.h.

  2. mouseDown() event handler in BasicSampleApp.cpp
    I implemented and adapted(c++) the code directly in the mouseDown() function

does it make sense?

@q-depot
Copy link
Contributor Author

q-depot commented Mar 14, 2013

I'm checking the data type size and I have different results when I print the size of double and double*
The size of double* is 8 bytes in C and only 4 bytes in C++ regardless I call it from my main app or from the file I include to implements your code(it's a .c file).

I guess this might be the problem, please let me know if you have the same opinion or other thoughts.

printf( "\n%d %d", sizeof(double), sizeof(double*) );

simpletest.c
8 8

C++ app
8 4

@jamiebullock
Copy link
Owner

OK. Regarding your comment:

to run the code in C++ I had to change this function:
xtract[XTRACT_SPECTRUM](%28void *%29&input, BLOCKSIZE, &argd[0], %28void *%29&spectrum[0]);

to this:
xtract[XTRACT_SPECTRUM](input, BLOCKSIZE, argd, spectrum);

Yup, this is the same problem as the malloc() issue. C++ compilers won't implicitly cast from void * to another pointer type so you need an explicit cast. However, in this case, no cast is need at all, because the pointer type being passed to the function, matches the one in the function prototype. I've just pushed a version of simpletest.c that fixes this. I tried compiling it with g++ and it worked just fine.

I didn't quite understand why you take the array address by dereferencing the array, I guess it's a C thing, in C++ > input should return the address to the first block of memory.

& is the referencing operator, so I'm not dereferencing the array, we're getting the address of its first element. However, this is completely unnecessary, and probably in the code for historical reasons (simpletest.c is quite old).

If we declare an array double foo[NUM_ELEMS]; then &foo[0] is exactly equivalent to foo.

I'm checking the data type size and I have different results when I print the size of double and double*
The size of double* is 8 bytes in C and only 4 bytes in C++ regardless

All this means is that your C compiler is using 64-bits to store pointers to double and your C++ compiler is using 32-bits to store pointers to double. The number of bytes used to store a pointer is implementation defined. I'm not sure what the reason for the difference is in this specific case, but I don't think it's what's causing our problems.

@q-depot
Copy link
Contributor Author

q-depot commented Mar 15, 2013

I'm running the block with the new version of Cinder(app rewrite) and it seems to be working now!
Even though I haven't figured it out, I'll keep working on my block, I'm sure the issue will come back eventually.

thanks again for your help.

@jamiebullock
Copy link
Owner

No problem. I'll close this issue now. Please open a new one if you have problems further down the line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants