You have forgotten to tell us how you are running this code and where it faults.
If you tried to put this into a stock executable then it presumably ended up in the read-only text section, hence the fault.
If you tried this from the stack, then that might not be executable, hence the fault.