I have a little problem... many solutions but maybe someone will give me a better hint.
It's about the possibility of assigning the return value of a malloc assignment to a variable of array type.
Namely I have a working function and a generic helper, where function has a very big array on it and callls helper(&bigarray).
Now, the point is that helper relies on the operator sizeof, so the verybigarray cannot be of pointer type (this way the 'size' that the compiler will tell is wrong), on the other size function cannot afford to have the whole array on the stack...
In code is something like this:
#define BIGSIZE 0xFFFFFFFFFFFFFFFFFFFF/*cking long*/
void function()
{
...
long anArray[BIGSIZE];
helper(anArray);
// This compiles, but the stack is far too big...
long aPointer* = (int*) malloc(BIGSIZE * sizeof int);
helper(*aPointer);
// This too compiles, but the sizeof in helper will give the wrong result.
// trashing all down :(
...
}
template(typename T)
void helper(T& a)
{
...
someFunction(sizeof a);
...
}
Possible solutions I thought of are:
*) Passing - from outside - a reference to the array as an argument to function... so having to change all the calls to it.
*) Switching to 'new[]'... having then to worry about the problems of mixing it with malloc.
*) Declaring helper as
template<typename T>
void helper(T& t, size_t s = sizeof t);
... but in this case what if somebody calls helper with an array passed as a pointer and forgets to add it's size?
Moreover I suspect that if I have something like n arrays of the same type but of differrent size the compiler will generate n functions (my application will run on an embedded system, so code size is really an issue).
... In less words I'd like to find a way to have an array declared inside a function but allocated on the heap, or anyway to convert the result of malloc in an array before calling 'helper', so that the latter can rely on the sizeof operator.
Any suggestion welcome.
Sergio