Your program doesn't free the memory it allocates. The free() call is after the return statement so will never be executed. Obviously it can't be in GEN() otherwise you can't return it to the calling function. So it needs to be in the calling function, which in this case is main().

Also it doesn't free all the memory it allocates. You free X itself, but you don't free X[n]. To avoid leaks, ALL malloc statements must be exactly matched with equivalent free statements. You have two mallocs and one free - a clear indication that there is a leak.