Using Microsoft Visual Studio 2010:
Can I write this type of macro in C? I cannot get it to work myself.
#define MEM_ALLOC_C(type, nElements) (type = (type*)_aligned_malloc(nElements * sizeof(type), CACHE_ALIGNMENT))
If I write it like this, it works:
#define MEM_ALLOC(type, nElements) (testFloat = (float*)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT))
This is how I am using it:
#define CACHE_ALIGNMENT 16
#define INDEX 7
#define MEM_ALLOC(type, nElements) (type = (float*)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT))
#define MEM_ALLOC_C(type, nElements) (type = (type*)_aligned_malloc(nElements * sizeof(type), CACHE_ALIGNMENT))
#define MEM_DEALLOC_PTR(type) (_aligned_free(type))
int _tmain(int argc, _TCHAR* argv[])
{
float* testFloat;
//MEM_ALLOC_C(testFloat, INDEX); // Problem here.
MEM_ALLOC(testFloat, INDEX); // works
//testFloat = (float*)_aligned_malloc(INDEX * sizeof(float), CACHE_ALIGNMENT); // works
testFloat[0] = (float)12;
//MEM_DEALLOC_PTR(testFloat); // If we call de-alloc before printing, the value is not 12.
// De-alloc seems to work?
printf("Value at [%d] = %f \n", 0, testFloat[0]);
getchar();
MEM_DEALLOC_PTR(testFloat);
return 0;
}
Thanks for any help.
think about the replacment:
becomes
.
There is no such thing as
testFloat*.In pure C there is no need to cast the result of malloc. Therefore you can just do: