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 - C Tutorial
From the course: Master C Language Pointers
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Comparing arrays and pointers3m 36s
-
Allocating storage3m 29s
-
Exploring allocation functions4m 58s
-
Accessing the buffer3m 41s
-
Working with pointers in structures2m 18s
-
Creating structure pointers3m 29s
-
Saving pointers and buffers4m 31s
-
Challenge: Saving and reading a record1m 6s
-
Solution: Saving and reading a record2m 15s
-
-
-