The malloc function allocates storage for use as a program runs. The size of the storage allocated is based on the data type used in the storage, multiplied by a specific quantity. The malloc function returns a memory location, the address of the storage allocated. A pointer variable matching the type of memory allocated is used to store the memory address malloc returns. When storage isn't available, the NULL pointer is returned. Always test the malloc function for the NULL pointer.
- [Instructor] When your program runs, it's given a chunk of memory to use by the operating system. Within the code, memory storage is assigned by declaring variables of various data types. When the code needs more memory, you create an array or buffer, and when you don't know how much storage space is needed, you allocate storage as your program runs by making a special request for it. To request more memory, use the malloc function. Malloc stands for memory allocation.
If you use it in your code, you muse include the stdlib or standard library header file. The malloc function has one argument, the number of bytes of memory to allocate. Upon success, malloc returns the location of the memory allocated. If memory isn't available or something unexpected happens, malloc returns the null pointer. Null, capital N-U-L-L, is a constant you can use as a comparison to check for the null pointer.
Also, the null constant is a pointer, a memory address. It's not the same thing as the null character at the end of a string. To determine how much memory is needed, use the sizeof operator on the type of data being requested, a char, int, float, or other data type. Multiply the result by the number of items requested. For example, to allocate storage for 100 integer variables, use sizeof int times 100.
This calculation determines the proper number of bytes malloc must allocate. The address malloc returns must match the type of data requested. Use a character pointer to store the address of a character buffer. Use an integer pointer to access storage allocated for integers and so on, and always test to ensure that the address returned isn't the null pointer. That means memory wasn't allocated or some other error occurred.
In this code, the malloc function at line 10 asks the operating system for a new chunk of memory, enough to store 1,024 characters, one kilobyte. The size of a single character variable is multiplied by the constant expression size, or 1,024. The memory allocated is saved in the sto pointer variable, and sto is a character pointer which is good.
Line 11 tests the memory address returned. If it's null, meaning memory wasn't available or not assigned, an error message is displayed and the program quits. Otherwise, memory was allocated and is available for use, though it's not really used in this code. Build and run the code. If memory is available, and it should be, you see that 1,024 bytes of memory is allocated at a specific address.
In this code, the malloc function requests enough storage for four integers. The storage is obtained by taking the size of an integer data type and multiplying it by four. The result is saved in the scores pointer. If the value returned is the null pointer, an error message is displayed, and the program stops. This step is important. Always ensure that memory was allocated. If you use a null pointer in your code, the program will crash.
That's why the null constant is available for you to use in your source code. Lines 15 through 18 assign values to the four integers created. I use pointer math here as references to the memory locations allocated. Finally, a for loop displays each of the scores as values. The loop's printf statement, you see the same format used for accessing allocated memory from lines 15 through 18.
The base plus the offset. Build and run the code. Space for the four variables is allocated on the fly as the program runs. A pointer then accesses those memory locations, storing and retrieving the values saved. Using malloc to allocate storage is far better than guessing with a specific buffer size, especially for input when the buffer size isn't known. You can use malloc to create a storage buffer on the fly and use memory as needed when your program runs.
- Recognize how to obtain a compiler and IDE.
- Determine the basics of C code.
- Identify how to add comments to the code.
- Break down how to work with escape sequences.
- Examine when is the best time to introduce variables.
- Recognize when the components of working with strings.
- Evaluate how to make calculations.