{"id":3688,"date":"2019-07-27T00:01:51","date_gmt":"2019-07-27T07:01:51","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3688"},"modified":"2019-07-20T11:20:24","modified_gmt":"2019-07-20T18:20:24","slug":"take-it-to-the-limit","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3688","title":{"rendered":"Take it to the Limit"},"content":{"rendered":"<p>It&#8217;s been a while since I&#8217;ve been seriously concerned about wasting system resources. Back when I programmed a TRS-80 Model III with only 48K of RAM, keeping an eye on memory usage was vital. Today, not so much. In fact, if you desire to write code that consumes a lot of memory, CPU time, or other system resources, more power to you!<br \/>\n<!--more--><br \/>\nThe only time I&#8217;m concerned about system resources is when processing humongous graphics files. Even then, the size is only a few megabytes, which shouldn&#8217;t be an issue for a modern system. And if the <em>malloc()<\/em> or <em>realloc()<\/em> function can&#8217;t find more memory, <code>NULL<\/code> is returned and the program reacts to the situation accordingly.<\/p>\n<p>When you really want to know the limits, however, you can use the <em>getrlimit()<\/em> function, defined in the <code>sys\/resource.h<\/code> header file. Here&#8217;s the man page format:<\/p>\n<p><code>int getrlimit(int resource, struct rlimit *rlp);<\/code><\/p>\n<p>The first argument is one of several defined constants, which are listed on the man page. Here are two choices:<\/p>\n<p><code>RLIMIT_DATA<\/code>, which directs the function to return the maximum number of bytes the process (your program) can access.<\/p>\n<p><code>RLIMIT_FSIZE<\/code>, which directs the function to return the largest file size (in bytes) that can be created.<\/p>\n<p>Other options are available return other maximum values, depending on which resources you&#8217;re trying to pinch.<\/p>\n<p>The second argument is a pointer to an <em>rlimit<\/em> structure. This structure has two members:<\/p>\n<p><em>rlim_t rlim_cur<\/em>, which represents the current limit.<\/p>\n<p><em>rlim_t rlim_max<\/em>, which represents the maximum limit.<\/p>\n<p>The <em>rlim_t<\/em> typecast is either a <em>long unsigned<\/em> or <em>double-long unsigned<\/em> integer value.<\/p>\n<p>The <em>getrlimit()<\/em> function returns zero upon success, -1 otherwise. The <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1735\">global <em>errno<\/em> variable<\/a> is set accordingly and can be examined for further details.<\/p>\n<p>In this sample code, the limit on how much data the program can access is output:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;sys\/resource.h&gt;\r\n\r\nint main()\r\n{\r\n    struct rlimit limit;\r\n    int r;\r\n\r\n    r = getrlimit( RLIMIT_DATA, &amp;limit );\r\n    if( r==-1 )\r\n    {\r\n        fprintf(stderr,\"Some kinda error\\n\");\r\n        return(1);\r\n    }\r\n\r\n    printf(\"Current soft limit on data is %llu\\n\",\r\n            limit.rlim_cur\r\n          );\r\n    printf(\"Current hard limit on data is %llu\\n\",\r\n            limit.rlim_max\r\n          );\r\n    \r\n    return(0);\r\n}<\/pre>\n<p>Line 6 declares the <em>rlimit<\/em> structure variable <code>limit<\/code> as a regular variable, not a pointer. For some reason, accessing the members proved difficult when I declared it as a pointer.<\/p>\n<p>The <em>getrlimit()<\/em> function is called at Line 9. The two structure members <code>limit.rlim_cur<\/code> and <code>limit.rlim_max<\/code> are output from Lines 16 onward. (Your compiler may balk at the <code>%llu<\/code> placeholder. If so, use <code>%lu<\/code> instead.)<\/p>\n<p>Here&#8217;s the output on my machine:<\/p>\n<p><code>Current soft limit on data is 9223372036854775807<br \/>\nCurrent hard limit on data is 9223372036854775807<\/code><\/p>\n<p>That&#8217;s one huge number, effectively 9 quintillion bytes, which is surprising given this machine has 16GB of RAM installed. This result leads me to believe the function erred, but -1 wasn&#8217;t returned so go figure.<\/p>\n<p>Curious, I test-ran the code on Ubuntu Linux and the output was twice as much, or around 18 quintillion bytes. So I&#8217;m not feeling good about the results, but at least the function is available, should you ever consider having your code access and use such humongous quantities of system resources.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>getrlimit()<\/em> function discloses details on programming resources. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3688\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3688","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3688"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3688\/revisions"}],"predecessor-version":[{"id":3694,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3688\/revisions\/3694"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}