From the course: Master C Language Pointers

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Allocating storage

Allocating storage - C Tutorial

From the course: Master C Language Pointers

Start my 1-month free trial

Allocating storage

- [Instructor] Arrays are fine for many types of storage. But to dynamically allocate storage, you must use a pointer. The pointer holds the buffer's address. The buffers created are allocated by using the malloc function. It's prototyped in the stdlib header file. The malloc functions argument is the buffer's size in bytes, though it's often set by using the size of a data type and a quantity more on that later. Upon success, the buffer's location is returned its address. Upon failure, the symbolic constant NULL is returned and this value must always be tested. In this exercise, 16 bytes of storage are allocated at line eight and assigned to the void pointer storage. Immediately the storage pointer is tested for null at line nine. If true, the allocation failed messages output and the program exits otherwise success message is output and the program exits anyway, doing nothing with the buffer. This exercise updates the…

Contents