1. You call free() on every pointer you got from malloc() in reverse order.
2. You can't. You need to explicitly store the dimensions somewhere.
Well, obviously you can't use the array after you've freed it, so you have to free it in main() after you're done with it.
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.
To grim,u can use a delete function in the clib,to delete all the memory dat is unsed on the stack only since allocated memory in the heap