Last active
November 2, 2021 15:07
-
-
Save TheRainstorm/86dd810e8d8c785811a315d4e195d922 to your computer and use it in GitHub Desktop.
[malloc maxium] get the maxium memory allocated by malloc #CA #malloc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<stdlib.h> | |
| int main(){ | |
| unsigned int maximum = 0; | |
| unsigned int blocksize[] = {1024*1024, 1024, 1}; | |
| for(int i=0; i<3; i++){ | |
| for(int count=1; ; count++){ | |
| void *block = malloc(maximum + blocksize[i]*count); | |
| if(block){ | |
| maximum += blocksize[i]*count; | |
| free(block); | |
| }else{ | |
| break; | |
| } | |
| } | |
| } | |
| printf("maximum malloc size = %uu B, %fM, %fG\n", maximum, (float)maximum/1024/1024, (float)maximum/1024/1024/1024); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment