C语言简介
C语言是一种通用的、过程式的计算机程序设计语言,广泛应用于各种领域,C语言的设计目标是提供一种能以简易的方式编写出高效、可靠的程序的语言,C语言具有高度的可移植性,可以在不同的操作系统和硬件平台上运行,C语言也是一种结构化编程语言,具有良好的内存管理机制,使得程序员能够更加专注于程序的功能实现。
C语言检索内存情况
在C语言中,我们可以使用一些系统调用和库函数来检索内存情况,本文将介绍两种常用的方法:使用malloc
和free
函数进行内存分配和释放,以及使用mallinfo
函数查询内存信息。
1、使用malloc
和free
函数进行内存分配和释放
malloc
函数用于在堆区为指定的数据类型分配内存空间,其原型如下:
void *malloc(size_t size);
free
函数用于释放之前使用malloc
或calloc
等函数分配的内存空间,其原型如下:
void free(void *ptr);
下面是一个简单的示例,演示如何使用malloc
和free
函数进行内存分配和释放:
include <stdio.h> include <stdlib.h> int main() { int *arr; int n = 10; // 分配内存空间 arr = (int *)malloc(n * sizeof(int)); if (arr == NULL) { printf("内存分配失败! "); return 1; } // 使用内存空间 for (int i = 0; i < n; i++) { arr[i] = i + 1; } // 打印数组元素 for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf(" "); // 释放内存空间 free(arr); arr = NULL; return 0; }
2、使用mallinfo
函数查询内存信息
mallinfo
函数用于查询当前进程的内存使用情况,其原型如下:
struct mallinfo mallinfo();
下面是一个简单的示例,演示如何使用mallinfo
函数查询内存信息:
include <stdio.h> include <stdlib.h> include <sys/mman.h> include <unistd.h> include <string.h> include <sys/types.h> include <sys/stat.h> include <fcntl.h> include <sys/wait.h> include <sys/resource.h> include <sys/time.h> include <sys/ptrace.h> include <errno.h> include <limits.h> include <stdarg.h> include <stddef.h> include <assert.h> include <signal.h> include <time.h> include <math.h> include <float.h> include <iso646.h> /* not standard but some systems have it */ /* see comments in source *//* define _POSIX_SOURCE */ /* POSIX compliant system */ /* undef _POSIX_SOURCE */ /* non-POSIX compilant system *//* define _XOPEN_SOURCE >= 600*/ /* POSIX compliant system */ /* undef _XOPEN_SOURCE */ /* non-POSIX compilant system *//* define _POSIX_C_SOURCE >= 200112L */ /* POSIX compliant system */ /* undef _POSIX_C_SOURCE */ /* non-POSIX compilant system *//* define __USE_MISC */ /* define for those that need this feature *//* undef __USE_MISC */ /* define for those that don't need this feature *//* define __USE_BSD */ /* define for those that need this feature *//* undef __USE_BSD */ /* define for those that don't need this feature *//* define __USE_XOPEN2K8 */ /* define for those that need this feature *//* undef __USE_XOPEN2K8 */ /* define for those that don't need this feature *//* define __USE_XOPEN2K6 */ /* define for those that need this feature *//* undef __USE_XOPEN2K6 */ /* define for those that don't need this feature *//* define __USE_ATFILE */ /* define for those that need this feature *//* undef __USE_ATFILE */ /* define for those that don't need this feature *//* define __USE_FORTIFY_LEVEL */ /* define for those that need this feature *//* undef __USE_FORTIFY_LEVEL */ /* define for those that don't need this feature *//* define __USE_ISOC95 */ /* define for those that need this feature *//* undef __USE_ISOC95 */ /* define for those that don't need this feature *//* define __USE_ISOC11 // For POSIX compliant systems we can use the ISO C99 features of the C library directly and do not need to use these defines anymore since they are now part of the ISO C++ Standard. If you want to build a program with full C++ conformance, you should define these symbols even though your platform may not support them yet because the compiler will still accept and compile your code using these symbols as long as they are not used in contexts where their absence is required by the C++ Standard or by your platform's API. See also the comments in each header file for more information on which features each symbol enables or disables. These symbols are defined in headers named cctype, float.h, setjmp.h, wchar.h, and stdlib.h. The following macros are also defined in these headers: XSHIFT, IS_*, ISOC_*, SCHAR_*, UCHAR_*, SHRT_*, USHRT_*, INTMAX_ *, PTRDIFF_*, LLONG_ *, LEAST32_*, GREATEST32_*, LEAST64_*, GREATEST64_*, WCHAR_ *, MB_ *, EXIT_FAILURE, STDIN_FILENO, etc. See also the header files unistd.h and sys/types.h for more information on these macros and how they are defined by your platform's implementation of the C library.*//* define _GNU_SOURCE*/ /* Define to enable GNU extensions on systems that have the definition of _GNU_SOURCE. See feature_test_macros(7) for more information on GNU extensions and their meaning.*//* define _SVID_SOURCE*/ /* Define to enable strict ANSI conformance on systems that have the definition of _SVID_SOURCE. See feature_test_macros(7) for more information on strict ANSI conformance and its meaning.*//* define _XOPEN_SOURCE*/ /* Define for POSIX compliance as specified in POSIX specification of version / The following macros are also defined in the header files posix.h and fcntl.h: O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_TRUNC, O_APPEND, O_NONBLOCK*//* define _POSIX2_C_SOURCE*/ /* Define to enable Posix compatibility as specified in POSIX specification of version / The following macros are also defined in the header files signal.h and sys/time.h: SA_RESTART, SA_SIGINFO*//* define _POSIX2_REENTRANT*/ /* Define to enable reentrant behavior as specified in POSIX specification of version / The following macros are also defined in the header files poll.h:POLLIN*//* define _XOPEN_SOURCE=500*/ /* Define for POSIX compliance as specified in POSIX specification of version / The following macros are also defined in the header files stat.h: SEEK_CUR*//* define _POSIX2_C_SOURCE=199506L*/ /* Define to enable Posix compatibility as specified in POSIX specification of version / The following macros are also defined in the header files utime.h: UTIME_**//* define _POSIX2_UPPER*/ /* Define to enable Posix compatibility as specified in POSIX specification of version / The following macros are also defined in theheader files locale.h: LC_ALL*//* define _XOPEN_SOURCE=600*/ /* Define for POSIX compliance as specified in POSIX specification of version / The following
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/129548.html