{"id":5432,"date":"2022-07-09T00:01:07","date_gmt":"2022-07-09T07:01:07","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5432"},"modified":"2022-07-16T07:16:09","modified_gmt":"2022-07-16T14:16:09","slug":"the-getline-function-update","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5432","title":{"rendered":"The <em>getline()<\/em> Function &#8211; Update"},"content":{"rendered":"<p>Back in early 2015, I <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1112\">wrote about<\/a> the <em>getline()<\/em> function, which is a handy way to read a string. I&#8217;ve been informed that my post is the top result for a Google search on <em>getline()<\/em> &mdash; and I have an update!<br \/>\n<!--more--><br \/>\nIn my post, I showed how to create storage for the <em>getline()<\/em> function&#8217;s first argument, the address of a pointer &mdash; the buffer to hold input. This step is unnecessary as the <em>getline()<\/em> function automatically allocates storage for the input string, but only providing that you first initialize the buffer&#8217;s address to <code>NULL<\/code>.<\/p>\n<p>Here is an update to my <em>getline()<\/em> sample code from back in 2015:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_07_09-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2022_07_09-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char *buffer = NULL;\r\n    size_t bufsize = 32;\r\n    size_t characters;\r\n\r\n    printf(\"Type something: \");\r\n    characters = getline(&amp;buffer,&amp;bufsize,stdin);\r\n    printf(\"%zu characters were read.\\n\",characters);\r\n    printf(\"You typed: '%s'\\n\",buffer);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>char<\/em> pointer <code>buffer<\/code> is declared and initialized at Line 5. This step is important! You must set the location to <code>NULL<\/code> or the program crashes. (I suppose that <em>getline()<\/em> tests for <code>NULL<\/code> to determine whether to allocate storage.) The program runs if you choose to instead allocate the buffer, as I did in the original post, but why bother if the function allocates (and reallocates) storage anyway?<\/p>\n<p>Aside from initializing buffer, the remainder of the code (above) is the same as the original. Here&#8217;s sample output:<\/p>\n<p><code>Type something: Today is July 9, 2022<br \/>\n22 characters were read.<br \/>\nYou typed: 'Today is July 9, 2022<br \/>\n'<\/code><\/p>\n<p>In my original post, I next went on to describe how to use <em>getline()<\/em> with an array. So instead of:<\/p>\n<p><code>char *buffer = NULL;<\/code><\/p>\n<p>I specified this ugly monster:<\/p>\n<p><code>char buffer[32];<br \/>\nchar *b = buffer;<\/code><\/p>\n<p>This technique is classified as unpredictable. The reason that disaster looms is when the <em>getline()<\/em> function reallocates storage, which it can do when input exceeds the value set in the second argument. (An amazing trick!) Here is the sample code for reference:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_07_09-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2022_07_09-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char buffer[32];\r\n    char *b = buffer;\r\n    size_t bufsize = 32;\r\n    size_t characters;\r\n\r\n    printf(\"Type something: \");\r\n    characters = getline(&amp;b,&amp;bufsize,stdin);\r\n    printf(\"%zu characters were read.\\n\",characters);\r\n    printf(\"You typed: '%s'\\n\",buffer);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is the output (macOS) when I overflow the buffer:<\/p>\n<p><code>Type something: This is a very long string that overflows the buffer<br \/>\na.out(48229,0x111d55600) malloc: *** error for object 0x7ff7bdcf78b0: pointer being realloc'd was not allocated<br \/>\na.out(48229,0x111d55600) malloc: *** set a breakpoint in malloc_error_break to debug<br \/>\nAbort trap: 6<\/code><\/p>\n<p>If I try to overflow the buffer with the pointer example, I see this output:<\/p>\n<p><code>Type something: This is a very long string that overflows the buffer<br \/>\n53 characters were read.<br \/>\nYou typed: 'This is a very long string that overflows the buffer<br \/>\n'<\/code><\/p>\n<p>Obviously, the first method works and the second does not. Please avoid using an array with the <em>getline()<\/em> function!<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5445\">next week&#8217;s Lesson<\/a>, I play around with <em>getline()<\/em> to further test its guts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A popular line input function, here&#8217;s an update to the original post I made about <em>getline()<\/em>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5432\">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-5432","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\/5432","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=5432"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5432\/revisions"}],"predecessor-version":[{"id":5458,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5432\/revisions\/5458"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}