Skip to content

Instantly share code, notes, and snippets.

@haseeb-heaven
Last active December 11, 2023 23:04
Show Gist options
  • Save haseeb-heaven/e33d87537290d3e1002ae9837724f5ef to your computer and use it in GitHub Desktop.
Save haseeb-heaven/e33d87537290d3e1002ae9837724f5ef to your computer and use it in GitHub Desktop.
Cross-Platform Memory Size Detector in C portable way.

🌐 Cross-Platform Memory Size Detector in C πŸš€

Hey there, fellow coders! πŸ‘‹ Let's dive into this cool piece of C code that helps us figure out the size of arrays and pointers, no matter what operating system you're on! πŸ–₯οΈπŸ’»

🧩 Code Breakdown

#include Statements

  • #include <stdio.h> and #include <stdlib.h>: These are our bread and butter, the standard I/O and standard library headers. 🍞🧈
  • Platform-specific headers: We're playing the field with Linux, macOS, and Windows. We've got you covered! 🐧🍏πŸͺŸ

Macros and Functions

  • getPointerSize: A chameleon macro! It changes its colors depending on the OS. It's our secret agent for memory size! πŸ•΅οΈ
  • getArraySize: This little gem calculates the size of static arrays. Quick and easy! πŸ’Ž
  • printDetectedOSName: Shouting out the OS you're using because why not? It's always good to know where you stand! πŸ“’

main Function

  • We start with a warm welcome, printing the OS name. πŸ‘‹
  • Playing with an array (my_arr) and a dynamically allocated array pointer (my_arr_ptr), we're showing off how to use our cool functions to get their sizes. 🎩✨
  • Error handling? Check! We're not letting any memory allocation issues slip by. πŸš«πŸ›

πŸ€” But Wait, There's More!

  • Cross-platform Compatibility: This code is like a world traveler, comfortable in many lands (aka operating systems)! 🌍✈️
  • Dynamic vs Static: It's like comparing apples and oranges. Both are fruit, but oh so different! This code understands that! 🍎🍊
  • Safety First: We're not taking any risks. If something goes wrong, we'll let you know! 🚨

So, there you have it! A fun, efficient, and safe way to deal with memory sizes in C across different platforms. Happy coding! πŸŽ‰πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’»


Output Example (Put this in a code block for the real feels!):

Linux
Pointer size: [Size of the allocated memory]
Array size: 14
/*
Description: Cross-Platform Memory Size Detector in C
Author: HeavenHM
Language: C
Date: 12-12-2023
Compilers Tested: GCC, Clang, MSVC.
Platforms Tested: Unix,Linux, MacOS, Windows.
*/
// Includes for cross-platform compatibility.
#include <stdio.h>
#include <stdlib.h>
// Macros to get the size of a pointer (Platform dependent).
#if defined(__linux__) || defined(__unix__) || defined(__unix) // Linux/Unix
#include <malloc.h>
#define getPointerSize(ptr) malloc_usable_size(ptr)
#elif __APPLE__ // MacOS
#include <malloc/malloc.h>
#define getPointerSize(ptr) malloc_size(ptr)
#elif defined(_WIN32) || defined(_MSC_VER) // Windows
#include <malloc.h>
#define getPointerSize(ptr) _msize(ptr)
#else
#error "OS not supported!"
#endif
// Macro to get the size of an array.
#define getArraySize(arr) (sizeof(arr) / sizeof((arr)[0]))
// Function to print the detected operating system.
char* getDetectedOSName() {
#if defined(__linux__) || defined(__unix__) || defined(__unix)
return "Linux";
#elif __APPLE__
return "MacOS";
#elif defined(_WIN32) || defined(_MSC_VER)
return "Windows";
#else
return "Unknown";
#endif
}
int main() {
// Print the detected operating system.
char* detected_os = getDetectedOSName();
printf("Detected OS: %s\n", detected_os);
// Example of demo array and pointer.
int my_arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 55, 44, 66};
int* my_ptr = (int*)malloc(sizeof(int) * 14);
if (my_ptr != NULL) {
// Print the size of the pointer.
size_t my_arr_ptr_size = getPointerSize(my_ptr);
printf("Pointer size: %zu\n", my_arr_ptr_size);
free(my_ptr);
} else {
printf("Memory allocation failed.\n");
}
// Print the size of the array.
size_t my_arr_size = getArraySize(my_arr);
printf("Array size: %zu\n", my_arr_size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment