Memory

So I have been learning C for the past while as I have mentioned in my previous posts and I have been learning about memory management in C.

What is Memory Management?

Memory management is the process of controlling and coordinating computer memory, assigning portions called blocks to various running programs to optimize overall system performance.

So I have been learning about how to allocate memory in C and how to free it.

What is Memory Allocation?

Memory allocation is the process by which computer programs and services are assigned physical or virtual memory space where they can use it to store data and code.

What is Memory freeing?

Memory freeing is the process of freeing up memory that is no longer needed by a program so it can be used by other programs.

You can use the functions malloc() and free() to allocate and free memory.

malloc() is used to allocate memory and free() is used to free memory.

An example of how you would use these functions is if you wanted to allocate memory for a string you would do something like this:

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

int main() {
    char *str;
    str = (char *) malloc(15);
    strcpy(str, "Hello World!");
    printf("String = %s,  Address = %u\n", str, str);
    free(str);
}

And this would output:

String = Hello World!,  Address = 12345678

Let’s go step by step through this code.

First, we include the stdio.h and stdlib.h, and string.h header files.

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

Then we create a pointer to a char called str.

char *str;

What is a pointer?

A pointer is a variable whose value is the memory address of another variable.

Then we allocate memory for the string using malloc() where we say for it to allocate 15 bytes of memory.

str = (char *) malloc(15);

Then we copy the string "Hello World!" to the memory location of str using strcpy().

strcpy(str, "Hello World!");

Then we print the string and the memory address of the string with printf using the %s for the string and %u for the memory address.

printf("String = %s,  Address = %u\n", str, str);

Then because we are done we can free the memory that we allocated for the string using free() by providing the variable name of the string so it can free the memory that was allocated for it.

free(str);

And that is how you allocate and free memory in C. This is very useful in cases where you want to allocate memory for a string or an array or when trying to do type conversion in C.

Recently I have also been working with C on the Amiga and you can use the same functions but there are also functions such as AllocVec() and FreeVec() that you can use to allocate and free memory on the Amiga.

The examples would look something like this:

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

int main() {
    char *str;
    str = (char *) AllocVec(15, MEMF_ANY);
    strcpy(str, "Hello World!");
    printf("String = %s,  Address = %u\n", str, str);
    FreeVec(str);
}

And this would again output:

String = Hello World!,  Address = 12345678

But you would see a difference where we use AllocVec() where we pass the size of the memory we want to allocate but also a value called MEMF_ANY which is a flag that tells the function to allocate memory from any memory pool.

There are many different flags that you can use with AllocVec() such as:

  • MEMF_ANY - Allocate memory from any memory pool.
  • MEMF_PUBLIC - Allocate memory from the public memory pool.
  • MEMF_CHIP - Allocate memory from the chip memory pool.
  • MEMF_FAST - Allocate memory from the fast memory pool.
  • MEMF_CLEAR - Clear the memory after allocation.

This a very interesting topic and I have been enjoying learning about it! It is particularly important when trying to make your programs more efficient and faster.

Hope you enjoyed this short post and Thanks so much for reading :D