Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮   
     ❯   

C Tutorial

C HOMEC IntroC Get StartedC SyntaxC OutputC CommentsC VariablesC Data TypesC Type ConversionC ConstantsC OperatorsC BooleansC If...ElseC SwitchC While LoopC For LoopC Break/ContinueC ArraysC StringsC User InputC Memory AddressC Pointers

C Functions

C FunctionsC Function ParametersC ScopeC Function DeclarationC Math FunctionsC Inline FunctionsC RecursionC Function Pointers

C Files

C Create FilesC Write To FilesC Read Files

C Structures

C StructuresC Nested StructuresC Structs & PointersC UnionsC typedefC Struct Padding

C Enums

C Enums

C Memory

C Memory Management

C Errors

C ErrorsC DebuggingC NULLC Error HandlingC Input Validation

C More

C DateC Random NumbersC MacrosC Organize CodeC Storage ClassesC Bitwise OperatorsC Fixed-width Integers

C Projects

C Projects

C Reference

C ReferenceC KeywordsC <stdio.h>C <stdlib.h>C <string.h>C <math.h>C <ctype.h>C <time.h>

C Examples

C ExamplesC Real-Life ExamplesC ExercisesC QuizC CompilerC SyllabusC Study PlanC Interview Q&AC Certificate

CMemory Management Example


Real-Life Memory Management Example

To demonstrate a practical example ofdynamic memory, we created a program that can make a list of any length.

Regular arrays in C have a fixed length and cannot be changed, but with dynamic memory we can create a list as long as we like:

Example

struct list {
  int *data; // Points to the memory where the list items are stored
  int numItems; // Indicates how many items are currently in the list
  int size; // Indicates how many items fit in the allocated memory
};

void addToList(struct list *myList, int item);

int main() {
  struct list myList;
  int amount;
  int i, j;

  // Create a list and start with enough space for 10 items
  myList.numItems = 0;
  myList.size = 10;
  myList.data = malloc(myList.size * sizeof(int));

  // Find out if memory allocation was successful
  if (myList.data == NULL) {
    printf("Memory allocation failed");
    return 1; // Exit the program with an error code
  }

  // Add any number of items to the list specified by the amount variable
  amount = 44;
  for (i = 0; i < amount; i++) {
    addToList(&myList, i + 1);
  }

  // Display the contents of the list
  for (j = 0; j < myList.numItems; j++) {
    printf("%d ", myList.data[j]);
  }

  // Free the memory when it is no longer needed
  free(myList.data);
  myList.data = NULL;

  return 0;
}

// This function adds an item to a list
void addToList(struct list *myList, int item) {

  // If the list is full then resize the memory to fit 10 more items
  if (myList->numItems == myList->size) {
    int newSize = myList->size + 10;

    // Use a temporary pointer so we don't lose the original on failure
    int *tmp = realloc(myList->data, newSize * sizeof(int));
    if (tmp == NULL) {
      printf("Memory resize failed\n");
      return; // Leave the list unchanged
    }

    // Only update fields after a successful reallocation
    myList->data = tmp;
    myList->size = newSize;
  }

  // Add the item to the end of the list
  myList->data[myList->numItems] = item;
  myList->numItems++;
}
Try it Yourself »

Pointers to structures: This example has a pointer to the structuremyList. Because we are using apointer to the structure instead of the structure itself, we use the arrow syntax (->) to access the structure's members.

Example explained

This example has three parts:

  • A structuremyList that contains a list's data
  • Themain() function with the program in it.
  • A functionaddToList() which adds an item to the list

ThemyList structure

ThemyList structure contains all of the information about the list, including its contents. It has three members:

  • data - A pointer to the dynamic memory which contains the contents of the list
  • numItems - Indicates the number of items that list has
  • size - Indicates how many items can fit in the allocated memory

We use a structure so that we can easily pass all of this information into a function.

Themain() function

Themain() function starts by initializing the list with space for 10 items:

// Create a list and start with enough space for 10 items
myList.numItems = 0;
myList.size = 10;
myList.data = malloc(myList.size * sizeof(int));

myList.numItems is set to 0 because the list starts off empty.

myList.size keeps track of how much memory is reserved. We set it to 10 because we will reserve enough memory for 10 items.

We then allocate the memory and store a pointer to it inmyList.data.

Then we include error checking to find out if memory allocation was successful:

// Find out if memory allocation was successful
if (myList.data == NULL) {
  printf("Memory allocation failed");
  return 1; // Exit the program with an error code
}

If everything is fine, a loop adds 44 items to the list using theaddToList() function:

// Add any number of items to the list specified by the amount variable
amount = 44;
for (i = 0; i < amount; i++) {
  addToList(&myList, i + 1);
}

In the code above,&myList is a pointer to the list andi + 1 is a number that we want to add to the list. We chosei + 1 so that the list would start at 1 instead of 0. You can choose any number to add to the list.

After all of the items have been added to the list, the next loop prints the contents of the list.

// Display the contents of the list
for (j = 0; j < myList.numItems; j++) {
  printf("%d ", myList.data[j]);
}

When we finish printing the list we free the memory to prevent memory leaks.

// Free the memory when it is no longer needed
free(myList.data);
myList.data = NULL;

TheaddToList() function

OuraddToList() function adds an item to the list. It takes two parameters:

void addToList(struct list *myList, int item)
  1. A pointer to the list.
  2. The value to be added to the list.

The function first checks if the list is full by comparing the number of items in the list to the size (capacity). If the list is full, it tries to grow the memory to fit 10 more items. We use atemporary pointer withrealloc so we don't lose the original block if the resize fails. We only updatedata andsize after a successful resize:

// If the list is full then resize the memory to fit 10 more itemsif (myList->numItems == myList->size) {  int newSize = myList->size + 10;  // Use a temp pointer so we don't lose the original on failure  int *tmp = realloc(myList->data, newSize * sizeof(int));  if (tmp == NULL) {    printf("Memory resize failed\n");    return; // Leave the list unchanged  }  // Only update fields after a successful reallocation  myList->data = tmp;  myList->size = newSize;}

Finally, the function adds the item to the end of the list. The index atmyList->numItems is always at the end of the list because it increases by 1 each time a new item is added.:

// Add the item to the end of the listmyList->data[myList->numItems] = item;myList->numItems++;

Why do we reserve 10 items at a time?

Optimizing is a balancing act between memory and performance. Even though we may be allocating some memory that we are not using, reallocating memory too frequently can be inefficient. There is a balance between allocating too much memory and allocating memory too frequently.

We chose the number 10 for this example, but it depends on how much data you expect and how often it changes. For example, if we know beforehand that we are going to have exactly 44 items then we can allocate memory for exactly 44 items and only allocate it once.


Complete stdlib Reference

For a complete reference of memory management functions and other functions found in the standard library, go to ourC <stdlib.h> Library Reference.




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

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 ourterms of use,cookies andprivacy policy.

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


[8]ページ先頭

©2009-2025 Movatter.jp