Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What are memory operations in C language?
Memory operations in C are functions that manipulate raw bytes in memory, regardless of data type. These functions are declared in #include <string.h> and work with void* pointers to handle any data type.
Syntax
The five primary memory functions have the following prototypes −
void *memchr(const void *s, int c, size_t n); int memcmp(const void *s1, const void *s2, size_t n); void *memcpy(void *dest, const void *src, size_t n); void *memmove(void *dest, const void *src, size_t n); void *memset(void *s, int c, size_t n);
Memory Functions Overview
| Function | Purpose |
|---|---|
| void *memchr (const void *s, int c, size_t n); | Search for a character in a buffer. |
| int memcmp (const void *s1, const void *s2, size_t n); | Compare two buffers byte by byte. |
| void *memcpy (void *dest, const void *src, size_t n); | Copy bytes from source to destination. |
| void *memmove (void *dest, const void *src, size_t n); | Copy bytes safely, handles overlapping regions. |
| void *memset (void *s, int c, size_t n); | Fill memory with a specific byte value. |
Example: Basic Memory Operations
This example demonstrates the common memory functions in action −
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello World";
char dest[20];
int numbers[] = {1, 2, 3, 4, 5};
int dest_nums[5];
// memcpy: Copy string bytes
memcpy(dest, src, strlen(src) + 1);
printf("After memcpy: %s<br>", dest);
// memset: Fill with specific value
memset(dest, 'A', 5);
dest[5] = '\0';
printf("After memset: %s<br>", dest);
// memcpy: Copy integer array
memcpy(dest_nums, numbers, sizeof(numbers));
printf("Copied array: ");
for(int i = 0; i < 5; i++) {
printf("%d ", dest_nums[i]);
}
printf("<br>");
// memcmp: Compare arrays
int result = memcmp(numbers, dest_nums, sizeof(numbers));
printf("Arrays are %s<br>", result == 0 ? "identical" : "different");
return 0;
}
After memcpy: Hello World After memset: AAAAA Copied array: 1 2 3 4 5 Arrays are identical
Key Differences
| Operation | For Characters | For Other Data Types |
|---|---|---|
| Copy | memcpy(dest, src, SIZE) | memcpy(dest, src, SIZE * sizeof(type)) |
| Compare | memcmp() compares bytes | strcmp() compares until null terminator |
| Overlap Safety | memmove() handles overlap | memcpy() undefined for overlap |
Important Notes
- memcpy() is faster but undefined for overlapping regions
- memmove() handles overlapping memory safely
- memcmp() compares unsigned bytes, returns negative, zero, or positive
- Always use
sizeof()for non-character data types
Conclusion
Memory operations provide low-level control over data manipulation in C. They work with raw bytes and are essential for efficient memory management, especially when working with arrays, structures, and binary data.
Advertisements
