malloc() usually returns memory with read and write permissions. Some architectures (for example, older than x86) may not allow direct removal of the execution permission, but this is just platform efficiency.
If you want to execute code from the memory you allocated, you will need to explicitly grant execute permissions, and you may need to remove write permissions, as having write and execute permissions in the same memory is considered potentially dangerous on some systems ( commonly called W ^ X). A.
There were several other threads when executing code from memory allocated by the programmer:
Select executable drum in c on linux
Is it possible to execute code from a stack in standard C?
source share