setrlimit + RLIMIT_AS + malloc
#include <sys/time.h>
#include <sys/resource.h> #include <iostream.h> #include <unistd.h> #include <stdlib.h> int main() { struct rlimit rlim; int result = getrlimit(RLIMIT_AS, &rlim); int *a1 =(int *)malloc(20*sizeof(int)); if(a1 != NULL) cout<<"Memory allocated\n"; else cout<<"Memory allocation failed for the first time\n"; rlim.rlim_cur = 8; rlim.rlim_max = 8; result = setrlimit(RLIMIT_AS, &rlim); cout<<"\nRLIMIT_AS: "; cout<<"\n\tResult: "<<result; cout<<"\n\tSoft Limit: "<<rlim.rlim_cur; cout<<"\n\tHard Limit: "<<rlim.rlim_max<<endl; int *b1 =(int *)malloc(8*sizeof(int)); if(b1 != NULL) cout<<"Memory allocated\n"; else cout<<"Memory allocation failed for the first time\n"; free(a1); free(b1); return 0; } Expected Output: Memory allocated RLIMIT_AS: 0 8 8 Memory allocation failed for the first time Observed o/p: Memory allocated RLIMIT_AS: 0 8 8 Memory allocated Why this discrepancy??? Why the second time malloc didnt fail? Regards Shivali |
The above program gives the expected o/p if i remove the first malloc from the program. why so???
i.e if the program is the one given below it works. #include <sys/time.h> #include <sys/resource.h> #include <iostream.h> #include <unistd.h> #include <stdlib.h> int main() { struct rlimit rlim; int result = getrlimit(RLIMIT_AS, &rlim); rlim.rlim_cur = 8; rlim.rlim_max = 8; result = setrlimit(RLIMIT_AS, &rlim); cout<<"\nRLIMIT_AS: "; cout<<"\n\tResult: "<<result; cout<<"\n\tSoft Limit: "<<rlim.rlim_cur; cout<<"\n\tHard Limit: "<<rlim.rlim_max<<endl; int *b1 =(int *)malloc(8*sizeof(int)); if(b1 != NULL) cout<<"Memory allocated\n"; else cout<<"Memory allocation failed for the first time\n"; free(a1); free(b1); return 0; } |
All times are GMT -5. The time now is 08:09 PM. |