Skip to content

computerman2027/Memory-Functions-in-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Memory Functions in C

Author: Imon Mallik
GitHub: https://github.com/computerman2027/Memory-Functions-in-C.git


πŸ“Œ Overview

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.


πŸ“‚ Project Structure

memchrdemo/
memcmpdemo/
memcopydemo/
memsetdemo/

Each folder contains demonstration programs related to one memory function.


πŸ” memchr() Demonstration

πŸ“ memchrdemo/memchrdemo_1.c

Function Used:

void *memchr(const void *ptr, int value, size_t num);

What It Does:

Searches for the first occurrence of a byte in a block of memory.

Demonstration:

  • Searches integer array memory byte-by-byte.
  • Shows that memchr() works on raw bytes, NOT integers.
  • Demonstrates how integer values are stored in memory.

πŸ”Ž memcmp() Demonstrations

πŸ“ memcmpdemo/memcmpdemo_1.c

  • Compares two identical integer arrays.
  • Prints decimal, hexadecimal, and actual byte-level memory.
  • Shows that memcmp() returns 0 for identical memory blocks.

πŸ“ memcmpdemo/memcmpdemo_2.c

  • Compares slightly different integer arrays.
  • Demonstrates non-zero return value.

πŸ“ memcmpdemo/memcmpdemo_3.c

  • Compares int[] with short int[].
  • Demonstrates byte-level comparison across different data types.

πŸ“ memcmpdemo/memcmpdemo_4.c

  • Demonstrates how integer values may contain embedded short values.
  • Shows memory alignment behavior.

πŸ“ memcmpdemo/memcmpdemo_5.c

  • Demonstrates endianness impact on comparison.

πŸ“ memcmpdemo/memcmpdemo_6.c

  • Compares strcmp() vs memcmp().
  • Cases demonstrated:
    • Equal strings
    • Lexicographical difference
    • Different lengths
    • Embedded NULL characters
    • Signed vs unsigned char
    • Binary data comparison

Function Used:

int memcmp(const void *ptr1, const void *ptr2, size_t num);

Key Learning:

memcmp() compares raw memory byte-by-byte, not logical values.


πŸ“¦ memcpy() Demonstrations

πŸ“ memcopydemo/memcopydemo_1.c

  • Copies entire integer array.
  • Shows before and after memory representation.

πŸ“ memcopydemo/memcopydemo_2.c

  • Demonstrates partial memory copying.
  • Copies only part of an array.

πŸ“ memcopydemo/memcopydemo_3.c

  • Copies an entire struct.
  • Shows raw memory layout of structure.
  • Demonstrates structure padding effects.

πŸ“ memcopydemo/memcopydemo_4.c

  • Demonstrates overlapping memory issue.
  • Shows undefined behavior with overlapping regions.

πŸ“ memcopydemo/memcopydemo_5.c

  • Another overlapping example.
  • Highlights why memcpy should not be used when memory overlaps.

Function Used:

void *memcpy(void *dest, const void *src, size_t num);

Key Learning:

  • Fast memory copying.
  • Undefined behavior when memory regions overlap.

πŸ” memmove() Demonstration

πŸ“ memcopydemo/memmovedemo_4.c

Function Used:

void *memmove(void *dest, const void *src, size_t num);

What It Demonstrates:

  • Safe copying when memory regions overlap.
  • Correct behavior compared to memcpy.

🧹 memset() Demonstrations

πŸ“ memsetdemo/memsetdemo_1.c

  • Sets integer memory using byte values.
  • Shows how memset sets byte-by-byte, NOT integer-by-integer.

πŸ“ memsetdemo/memsetdemo_2.c

  • Demonstrates pointer arithmetic effects.
  • Shows impact of unaligned memory modification.
  • Demonstrates how altering memory at byte level changes integer values.

Function Used:

void *memset(void *ptr, int value, size_t num);

Key Learning:

memset() fills memory byte-by-byte.


🧠 Core Concepts Covered

  • 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

πŸ›  How to Compile

Using GCC:

gcc filename.c -o output
./output

Example:

gcc memcmpdemo_1.c -o memcmpdemo_1
./memcmpdemo_1

🎯 Purpose of This Repository

This project is created for:

  • Deep understanding of memory in C
  • Interview preparation
  • Systems programming fundamentals
  • Strengthening pointer and memory concepts

⭐ Final Note

This repository is meant for educational purposes to help understand how memory manipulation functions operate internally at the byte level.

Happy Coding πŸš€

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages