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 - C Tutorial
From the course: C: Data Structures, Pointers, and File Systems
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…
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
-
-
-
-
-
Understanding pointers3m 19s
-
(Locked)
Using pointers to manipulate data2m 28s
-
(Locked)
Doing pointer math2m 57s
-
(Locked)
Comparing arrays and pointers3m 55s
-
(Locked)
Allocating storage3m 15s
-
Working with a pointer array3m 38s
-
(Locked)
Using pointers in structures3m 30s
-
(Locked)
Passing pointers to functions2m 57s
-
(Locked)
Returning pointers2m 39s
-
(Locked)
Challenge: Create a pointer array54s
-
(Locked)
Solution: Create a pointer array2m 23s
-
-
-
-