Dynamic Memory Manipulation basics in c programming

Reading Time: 4 minutes

Today in this article, we are going to discuss the dynamic memory manipulation basics in C programming language. If you ever wonder what memset, memcpy, and memmove stands for, keep reading and you’ll get a sound knowledge about them and their usages. If you are new to C programming, I suggest you first look at http://20.187.108.42/dynamic-memory-management-basics-in-c-programming/

What are we going to learn?

  • What is dynamic memory manipulation in C?
  • memset and its usage
  • memcpy and its usage
  • memmove and its usage
  • memcpy and memmove comparison
  • calloc, malloc, and memset facts

What is dynamic memory manipulation in C?

In our programs, we might sometimes need to manipulate the memory blocks that we initially set using various functions like malloc and calloc.

For example, we might need,

  • setting all bytes in a particular memory block to a specific value
  • coping information from one location to another
  • moving information from one location to another

What is memset?

memset sets all bytes in a memory block to a specific value.

A practical usage of the memset function is to erase a block of memory by setting all values stored in it to (say for example) zero. This clears any information previously stored in that block of memory.

memset’s protoype is,

void *memset(void *location, int value, size_t bytes);

The location variable is a pointer to the block of memory that we want to manipulate (set the values to).

The value is the value to set to the block of memory. The value of variable value can only be in the range 0 through 255. This int variable is treated as a char variable. It means the unsigned char conversion of the variable value is taken by the program.

The size_t is an unsigned integer datatype.

bytes variable stores the number of bytes to be set, counting from the address pointed by location pointer.

What happens in this code is, the values in the memory block starting from location address and moving the number of bytes denoted by bytes variable, are replaced by the value of the variable value.

memset function returns a pointer to the memory block pointed to by the location variable.

What is memcpy?

memcpy copies data bytes from one memory block to another.

The type of data being copied is not important when use memcpy.

memcpy’s function prototype is,

void *memcpy(void *destination, int *source, size_t bytes);

Here, destination variable points to destination memory block. This is the block where the data will be copied to.

source variable points to source memory block. This is the block where the data are originally present.

The number of bytes to be copied is stored in bytes variable.

The memcpy function may not work as intended if,

  • memory block at source and memory block at destination overlap.

Due to this problem, memcpy is rarely used.

The solution to this memory overlapping problem is,

  • use memmove function.

memcpy returns a pointer to the destination memory block.

What is memmove?

memmove is very similar to memcpy.

memmove also copies data bytes from one memory block to another.

The major difference between memcpy and memmove is that, memmove can handle overlapping memory blocks.

memmove’s function prototype is,

void *memmove(void *destination, int *source, size_t bytes);

Here, destination variable points to destination memory block. This is the block where the data will be copied to.

source variable points to source memory block. This is the block where the data is originally present.

The number of bytes to be copied is stored in bytes variable.

If the destination and source memory blocks overlap,

  • overlapping source data are copied first, before overwritten.

memmove returns a pointer to the destination memory block.

memcpy and memmove comparison

calloc, malloc and memset facts

Example program using the memset, memcpy and memmove

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

char str1[60] = "I ate three apples last week";
char str2[60] = "abcdefghijklmnopqrstuvwxyz";
char tmp[60];

int main() {
    //memset
    printf("\nstr1 before applying memset :\t%s", str1);
    memset(str1 + 6, '*', 12);
    printf("\nstr1 after applying memset :\t%s", str1);

    printf("\n");

    //memcpy
    strcpy(tmp, str2);
    printf("\nOriginal str2 :\t\t\t\t\t%s", tmp);
    memcpy(tmp + 4, tmp + 16, 10);
    printf("\nstr2 after memcpy (without overlapping) :\t%s", tmp);
    strcpy(tmp, str2);
    memcpy(tmp + 6, tmp + 4, 10);
    printf("\nstr2 after memcpy (with overlapping) :\t\t%s", tmp);

    printf("\n");

    //memmove
    strcpy(tmp, str2);
    printf("\nOriginal str2 :\t%s", tmp);
    memmove(tmp + 4, tmp + 16, 10);
    printf("\nstr2 after memmove (without overlapping) :\t%s", tmp);
    strcpy(tmp, str2);
    memmove(tmp + 6, tmp + 4, 10);
    printf("\nstr2 after memmove (with overlapping) :\t\t%s", tmp);

    return 0;
}

In the line 11, memset function is used. str + 6 is a pointer arithmetic (addition). It is used to pass the address of the destination memory block.

The second argument ‘*’ is the character which replaces the specified range in the memory block.

The third argument 12 is the number of bytes to set starting from the first byte of the memory block.

In line 22, the memcpy function is used. Since there is an overlapping of the memory blocks, the outcome is not predictable.

Conclusion

That’s it! and those are the basics that you should know about dynamic memory manipulation in C.

Like rukbook.com so far? Chances are, you’ll like rukbook on YouTube (coming soon)! Click the button below to stay subscribed ahead of time!

Did you enjoy this article? Why not share it with your friends and connections? It helps spread the word about rukbook and it means a lot! Thanks for reading. Subscribe to rukbook.com mailing list by just entering your email below!

Subscribe

* indicates required

Leave a comment. We love your feedback.