From the course: C: Data Structures, Pointers, and File Systems

Unlock the full course today

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

Allocating storage

Allocating storage - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

Allocating storage

- [Instructor] When you need a chunk of memory on the fly, you allocate storage for a new buffer by using a pointer. In this exercise file, a storage buffer for 128 characters is created as the program runs. Two key statements make this possible. First, a character pointer variable buffer is declared at line six. Second, the malloc function at line eight allocates a chunk of 128 bytes of memory to use as that buffer, build and run. Now you don't see the buffer in the output but it was successfully created because an error message did not appear. The malloc function allocates memory which is where it gets its name, memory allocation. It requires an argument equal to the quantity of memory desired, measured in bytes, what's called a size_t data type which is essentially a character. The value returned is a memory location upon success or the null constant on failure. The malloc function is prototyped in the stdlib header file which must be included in your source code. In your code…

Contents