Skip to content

Instantly share code, notes, and snippets.

@TheRainstorm
Last active November 2, 2021 15:07
Show Gist options
  • Select an option

  • Save TheRainstorm/86dd810e8d8c785811a315d4e195d922 to your computer and use it in GitHub Desktop.

Select an option

Save TheRainstorm/86dd810e8d8c785811a315d4e195d922 to your computer and use it in GitHub Desktop.
[malloc maxium] get the maxium memory allocated by malloc #CA #malloc
#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