Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C Memory Management


Memory management is the process of handling how much memory a program uses through different operations.


Memory in C

Understanding how memory works in C is important. When you create a basic variable, C will automatically reserve space for that variable. An int variable for example, will typically occupy 4 bytes of memory, while a double variable will occupy 8 bytes of memory.

You can use the sizeof operator to find the size of different types:

Example

int myInt;
float myFloat;
double myDouble;
char myChar;

printf("%lu\n", sizeof(myInt));      // 4 bytes
printf("%lu\n", sizeof(myFloat));    // 4 bytes
printf("%lu\n", sizeof(myDouble));   // 8 bytes
printf("%lu\n", sizeof(myChar));     // 1 byte
Try it Yourself »

Why is it important to know?

If you create a program that occupies too much, or unnecessary memory, it can result in slow and poor performance.

In C, you have to manage memory yourself. It is a complicated task, but is also quite powerful when used correctly: Properly managing the computer memory optimizes the performance of the program, so it is useful that you know how to release memory when it is no longer required and only use as little as necessary for the task.

In previous chapters you learned about memory addresses and pointers.

Both have an importance when it comes to memory management, since it is possible to work directly with memory through pointers.

But be careful; pointers must be handled with care, since it is possible to damage data stored in other memory addresses.


Memory Management

Memory management is the process of handling how much memory a program uses through allocation, reallocation and deallocation (often referred to as "freeing"). We will introduce each of these topics in the following chapters.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.