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 - C Tutorial
From the course: Complete Guide to C Programming Foundations
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
-
-
-
-
-
-
-
-
-
-
Understanding pointers3m 23s
-
(Locked)
Using pointers to manipulate data2m 53s
-
(Locked)
Doing pointer math3m 10s
-
(Locked)
Comparing arrays and pointers5m 5s
-
(Locked)
Challenge: Output a string57s
-
(Locked)
Solution: Output a string2m 44s
-
(Locked)
Allocating storage3m 42s
-
(Locked)
Working with a pointer array4m
-
(Locked)
Using pointers in structures3m 8s
-
(Locked)
Challenge: Allocating structures1m 3s
-
(Locked)
Solution: Allocating structures3m 34s
-
(Locked)
Passing pointers to functions2m 23s
-
(Locked)
Returning pointers from a function2m 31s
-
Chapter challenge: Processing input1m 26s
-
(Locked)
Chapter solution: Processing input3m 14s
-
-
-
-