Menu
Coddy logo textTech

Using sizeof() for Allocation

Part of the Logic & Flow section of Coddy's C journey — lesson 33 of 63.

While malloc() allows you to allocate memory dynamically, there's an important best practice that makes your code more portable and maintainable: using the sizeof() operator to determine how many bytes to allocate.

The sizeof() operator returns the number of bytes required to store a particular data type on your system. Instead of hardcoding byte sizes, you can let sizeof() calculate the correct amount automatically:

// Instead of guessing byte sizes:
int *ptr = (int*)malloc(4);  // Assumes int is 4 bytes

// Use sizeof() for portability:
int *ptr = (int*)malloc(sizeof(int));  // Always correct

This approach becomes especially valuable when allocating arrays. To allocate space for multiple elements, multiply the number of elements by the size of each element:

// Allocate space for 10 integers
int *arr = (int*)malloc(10 * sizeof(int));

// Allocate space for 5 doubles
double *values = (double*)malloc(5 * sizeof(double));

Using sizeof() makes your code work correctly across different systems where data types might have different sizes, and it clearly communicates your intent to allocate space for a specific number of elements of a particular type.

challenge icon

Challenge

Easy

Write a C program that demonstrates the proper use of sizeof() with malloc() for dynamic memory allocation. Your program should:

  1. Read an integer n from the user representing the number of elements to allocate
  2. Use malloc() with sizeof() to dynamically allocate memory for an array of n double values
  3. Cast the returned void pointer to a double pointer
  4. Read n double values from the user and store them in the allocated array
  5. Calculate the total memory allocated in bytes and print it in the format: Memory allocated: [bytes] bytes
  6. Calculate and print the average of all values with exactly 2 decimal places in the format: Average: [value]
  7. Find and print the largest value with exactly 2 decimal places in the format: Largest: [value]

Remember to include the <stdlib.h> header to use malloc(). Your program should demonstrate how sizeof() ensures correct memory allocation regardless of the system's data type sizes.

For example, if the input is:

3
2.5 7.8 4.1

Your output should be:

Memory allocated: 24 bytes
Average: 4.80
Largest: 7.80

This challenge reinforces the importance of using sizeof() to make your dynamic memory allocation portable and maintainable, automatically calculating the correct number of bytes needed for any data type on any system.

Cheat sheet

Use the sizeof() operator with malloc() to determine the correct number of bytes to allocate for any data type:

// Portable memory allocation
int *ptr = (int*)malloc(sizeof(int));

// Allocating arrays
int *arr = (int*)malloc(10 * sizeof(int));
double *values = (double*)malloc(5 * sizeof(double));

The sizeof() operator returns the number of bytes required to store a data type on your system, making your code portable across different platforms where data types might have different sizes.

Try it yourself

#include <stdio.h>
#include <stdlib.h>

int main() {
    // Read the number of elements
    int n;
    scanf("%d", &n);
    
    // TODO: Write your code below
    // 1. Allocate memory for n double values using malloc() and sizeof()
    // 2. Cast the returned pointer to double*
    // 3. Read n double values and store them in the allocated array
    // 4. Calculate and print memory allocated in bytes
    // 5. Calculate and print the average with 2 decimal places
    // 6. Find and print the largest value with 2 decimal places
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow