Skip to content

Instantly share code, notes, and snippets.

@pavly-gerges
Created February 18, 2023 18:15
Show Gist options
  • Save pavly-gerges/a78205ad769f370c822bc7cc9a9b8e7f to your computer and use it in GitHub Desktop.
Save pavly-gerges/a78205ad769f370c822bc7cc9a9b8e7f to your computer and use it in GitHub Desktop.
Tests chunk memory copy in C
/**
* Test chunk memcpy
*
* @author pavl_g
*/
#include <stdio.h>
#include <string.h>
static inline void printAddresses(char* arr, int length) {
for (int i = 0; i < length; i++) {
printf("%p", &((arr)[i]));
printf("\n");
}
}
static inline void printValues(char* arr, int length) {
for (int i = 0; i < length; i++) {
printf("%c", arr[i]);
}
printf("\n");
}
int main() {
/* 20 bytes */
const int length = 20;
char src[length];
char dest[length];
src[0] = 'a';
src[1] = 'b';
src[2] = 'c';
src[3] = 'd';
printAddresses(src, length);
printValues(src, length);
/* copy values [length] from src starting from index [2] to the
dest buffer starting from index [2] */
memcpy(&dest[2], &src[2], length);
printAddresses(dest, length);
printValues(dest, length);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment