Does Ubuntu automatically release memory allocated by a C program as the program terminates?

Yes.

The kernel will release all the resources allocated by a program upon the program's termination.

This is done after the do_exit() function defined in kernel/exit.c has terminated its execution; the do_exit() function execution itself might be triggered by a number of events, most commonly by an explicit exit() syscall called by the program during its regular computation or by an implicit exit() syscall called by the program upon its termination (as the C compiler places an exit() syscall after main()'s return). Other reasons include the reception of an unhandable / not ignorable signal or exception.

The do_exit() function itself performs a number of tasks. After it has terminated its execution, the program's allocated resources are released by the kernel and returned to the system for further uses.

Source