LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2006, 04:18 AM   #1
shivaligupta
Member
 
Registered: Oct 2004
Posts: 45

Rep: Reputation: 15
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
 
Old 04-07-2006, 05:19 AM   #2
shivaligupta
Member
 
Registered: Oct 2004
Posts: 45

Original Poster
Rep: Reputation: 15
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;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
malloc anoosh Programming 1 03-15-2006 04:41 PM
malloc eagle683 Programming 6 05-22-2005 02:40 PM
malloc() vijeesh_ep Programming 4 08-25-2004 03:50 PM
about malloc eshwar_ind Programming 11 02-18-2004 03:41 PM
setrlimit hfawzy Programming 8 08-18-2003 04:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration