While conceptually a List
is just another expression with head List
and arbitrary elements, the implementation uses more efficient representations (packed array) when all elements are either machine reals or machine integers. This representation is maintained when sending the array through a MathLink connection. In principle, you can (1) detect if there is a packed array on the link (2) determine its type (3) use the correct Get
function to retrieve it.
Unfortunately, how to do this is not documented. Lots of things about MathLink are not very well documented. There is a question about this on M.SE: https://mathematica.stackexchange.com/q/180706/12
I would not recommend trying to do this, unless you have a very good reason to do so (e.g. you do not know at all what expression you are receiving on the link). Instead, you can try to determine the type of the array in Mathematica code, and either pass in the type in some way (some encoding of your choice) to the C functions, so that function knows what to get, or just use two different functions (what you were trying to avoid).
If you do this anyway, LibraryLink will give you much better performance: https://mathematica.stackexchange.com/q/91887/12 With LibraryLink, having two functions will not be avoidable.
Note that by using LibraryLink you will not lost almost anything because optionally you can still use MathLink-based passing.
To determine the array type in Mathematica, use Developer`PackedArrayQ
(see 2nd argument). To determine if a non-packed array is suitable for passing to your function (i.e. if types are homogeneous, if they fit in machine numbers, etc.) you can first try to pack it using Developer`ToPackedArray
.
All that said, if you all wanted was to save a bit of memory, then just don't specialize the functions to integer/real. It will not save memory at all. If you care about performance a lot, just use LibraryLink.
Let me also recommend my LTemplate package with the caveat that you should get familiar with plain LibraryLink before starting to use it. LTemplate is most useful if you want to use managed library expressions, i.e. if you want to store some sort of data in C and manipulate it from Mathematica. I use it for my IGraph/M package, where I need to store graphs in C and manipulate them repeatedly.
I hope this information will help you make a good decision.