malloc ()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* malloc() takes one argument: # of bytes to allocate
* # of bytes will equal, (# of elements) * (sizeof(data type))
* If creating an integer array with 10 elements, use:
* malloc( 10 * sizeof(int) )
* NOTE: malloc() will return a pointer to the head
* of this newly allocated memory
*
* ADDITIONAL NOTE: malloc() will NOT initialize any element of the array
* All elements of array are garbage values until values are manually assigned
*/
int main(void) {
// int * arr will store a pointer to an integer array of size arrLen
// NOTE: adding (int*) explicitly casts malloc to integer pointer
// HOWEVER - without (int*) in front, cast is implicit / still works
int arrLen = 20;
int *arr = (int *)malloc(arrLen * sizeof(int));
// Initializes each element of array to zero
for (int i = 0; i < arrLen; i++)
arr[i] = 0;
/////////////////////////////////////////////////////////////////////////////
// malloc() for strings
// Strings end with a NULL terminator ('\0')
// String memory must be allocated as string length + 1 to create additional
// space for NULL terminator
char str[] = "puppy";
int len = strlen(str);
// To copy statically allocated string str to a dynamically allocated variable
// First, the appropriate space must be allocated
// NOTE: (len + 1) for NULL terminator
char *newStr = malloc((len + 1) * sizeof(char));
// Once memory is allocated, string can be copied using strcpy()
strcpy(newStr, str);
/////////////////////////////////////////////////////////////////////////////
// pointer to pointer [In this example being used as a 2D array]
// A pointer to pointer can create an array of pointers
// Each element in the array can point to its own array
// NOTE: Cast to (int**) but each element is sizeof(int*)
int numElements = 10;
int **arr2D = (int **)malloc(numElements * sizeof(int *));
// Now that array of pointers is allocated, each element points to garbage
// These pointers can be manually set
int sizeEachElement = 5;
// Initializes each element of array to zero
for (int i = 0; i < numElements; i++) {
// First allocate space
arr2D[i] = (int *)malloc(sizeEachElement * sizeof(int));
// Then initialize each element
for (int j = 0; j < sizeEachElement; j++) {
arr2D[i][j] = 0;
}
}
/////////////////////////////////////////////////////////////////////////////
// Always free dynamically allocated memory when you're done with it
for (int i = 0; i < numElements; i++) {
free(arr2D[i]);
}
free(arr2D);
free(newStr);
free(arr);
printf("I hope this has helped to teach you how malloc() works :)\n");
return 0;
}
Academic Notice: The code shown here is provided solely as a learning reference. Copying and pasting is intentionally disabled to encourage independent practice. Students should implement solutions on their own to demonstrate understanding. This material is not intended for direct submission in assignments.
Additionally: This code was written by a former CS1 student and may not reflect your professor’s intended solution or instructional approach. For coursework, students are expected to follow examples, conventions, and requirements presented in class and in professor-assigned materials.