From the course: Complete Guide to C Programming Foundations

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Allocating storage

Allocating storage

- [Instructor] When a program needs a chunk of memory on the fly, you allocate storage and create a buffer. The new buffer's address is saved in a pointer such as buffer declared as a character pointer here. The malloc function at line eight allocates memory. It sets aside 128 characters of storage, saving the address in pointer buffer. Immediately an if statement tests for an allocation error. If so, an error message is output. The exit function leaves the program at once. It works similarly to the return statement, but it can be used anywhere in the code. This function is prototyped in the STDlib.H header file included here. Upon success, text is output and the buffer has been created. The buffer's storage is released at line 15. Another text update is output and the program quits. You can't see the buffer, nor does the code use it, but it was successfully created and its memory released. In C the malloc function allocates memory. It's defined in the STD.lib.H header file, which…

Contents