Author: Imon Mallik
GitHub: https://github.com/computerman2027/Memory-Functions-in-C.git
This repository demonstrates the working of important memory
manipulation functions in C from <string.h>:
memchr()memcmp()memcpy()memmove()memset()
Each function is explained using multiple practical examples, including: - Byte-level memory visualization - Endianness observation (Little Endian systems) - Struct copying - Partial memory copying - Overlapping memory issues - Binary data comparison - Embedded NULL character behavior
The goal of this repository is to deeply understand how memory works at the byte level in C.
memchrdemo/
memcmpdemo/
memcopydemo/
memsetdemo/
Each folder contains demonstration programs related to one memory function.
void *memchr(const void *ptr, int value, size_t num);Searches for the first occurrence of a byte in a block of memory.
- Searches integer array memory byte-by-byte.
- Shows that
memchr()works on raw bytes, NOT integers. - Demonstrates how integer values are stored in memory.
- Compares two identical integer arrays.
- Prints decimal, hexadecimal, and actual byte-level memory.
- Shows that
memcmp()returns 0 for identical memory blocks.
- Compares slightly different integer arrays.
- Demonstrates non-zero return value.
- Compares
int[]withshort int[]. - Demonstrates byte-level comparison across different data types.
- Demonstrates how integer values may contain embedded short values.
- Shows memory alignment behavior.
- Demonstrates endianness impact on comparison.
- Compares
strcmp()vsmemcmp(). - Cases demonstrated:
- Equal strings
- Lexicographical difference
- Different lengths
- Embedded NULL characters
- Signed vs unsigned char
- Binary data comparison
int memcmp(const void *ptr1, const void *ptr2, size_t num);memcmp() compares raw memory byte-by-byte, not logical values.
- Copies entire integer array.
- Shows before and after memory representation.
- Demonstrates partial memory copying.
- Copies only part of an array.
- Copies an entire struct.
- Shows raw memory layout of structure.
- Demonstrates structure padding effects.
- Demonstrates overlapping memory issue.
- Shows undefined behavior with overlapping regions.
- Another overlapping example.
- Highlights why memcpy should not be used when memory overlaps.
void *memcpy(void *dest, const void *src, size_t num);- Fast memory copying.
- Undefined behavior when memory regions overlap.
void *memmove(void *dest, const void *src, size_t num);- Safe copying when memory regions overlap.
- Correct behavior compared to memcpy.
- Sets integer memory using byte values.
- Shows how memset sets byte-by-byte, NOT integer-by-integer.
- Demonstrates pointer arithmetic effects.
- Shows impact of unaligned memory modification.
- Demonstrates how altering memory at byte level changes integer values.
void *memset(void *ptr, int value, size_t num);memset() fills memory byte-by-byte.
- Little Endian memory representation
- Byte-level memory inspection
- Pointer arithmetic
- Structure padding
- Undefined behavior
- Overlapping memory issues
- Signed vs unsigned comparison
- Difference between logical comparison and raw memory comparison
Using GCC:
gcc filename.c -o output
./output
Example:
gcc memcmpdemo_1.c -o memcmpdemo_1
./memcmpdemo_1
This project is created for:
- Deep understanding of memory in C
- Interview preparation
- Systems programming fundamentals
- Strengthening pointer and memory concepts
This repository is meant for educational purposes to help understand how memory manipulation functions operate internally at the byte level.
Happy Coding π