1. How or where can I free the allocated memory in the main-program after returned it in array()?
2. How can I find out the dimensions of ab in the main-program?
Actually, the order of free() calls is not important, other than the need to hold onto ab as long as you need its contents. It's invalid to refer to malloced memory after free() has been called on it.
Grima -- you say you "get out the dimensions from the function itself", but that needs explanation because you could be doing things that won't always work. I can't tell from what you said.