{"id":4728,"date":"2021-05-01T00:01:01","date_gmt":"2021-05-01T07:01:01","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4728"},"modified":"2022-02-26T10:42:22","modified_gmt":"2022-02-26T18:42:22","slug":"putting-the-memset-function-to-work","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4728","title":{"rendered":"Putting the <em>memset()<\/em> Function to Work"},"content":{"rendered":"<p>My approach for initializing a buffer is to use a loop and assign each byte a value such as zero or the null character, <code>'\\0'<\/code>. It&#8217;s tedious, but necessary. And for the impatient, some functions are available to perform this task for you.<br \/>\n<!--more--><br \/>\nThe following code shows how I typically initialize a buffer. This method works and it&#8217;s good code, especially for someone like me who is teaching programming and wants to illustrate multiple points:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_05_01-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_05_01-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[BUFSIZ];\r\n    int x = 0;\r\n\r\n    while( x&lt;BUFSIZ )\r\n    {\r\n        buffer[x] = '\\0';\r\n        x++;\r\n    }\r\n    puts(\"Buffer initialized\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop at Line 8 plows through character array <code>buffer[]<\/code>, filling each element with the null character byte (Line 10). Simple. Elegant. Refer to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4711\">last week&#8217;s Lesson<\/a> for info on the defined constant <code>BUFSIZ<\/code>.<\/p>\n<p>The program outputs the text <code>Buffer initialized<\/code>, which is accurate but useless.<\/p>\n<p>Another, more direct way to pack a buffer with a given byte value is to use the <em>memset()<\/em> function. Prototyped in the <code>string.h<\/code> header file, <em>memset()<\/em> uses this format:<\/p>\n<p><code>void *memset(void *b, int c, size_t len);<\/code><\/p>\n<p>The function takes three arguments: <code>b<\/code> for a <em>char<\/em> buffer, <code>c<\/code> for the character to pack into the buffer, and <code>len<\/code> for the buffer&#8217;s size. The function returns the first argument, the buffer&#8217;s address.<\/p>\n<p>Here is an update to my code, showing how the <em>memset()<\/em> function makes things tighter:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_05_01-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_05_01-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char buffer[BUFSIZ];\r\n\r\n    memset(buffer, '\\0', BUFSIZ);\r\n    puts(\"Buffer initialized\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop from the first example is replaced by a single statement at Line 8:<\/p>\n<p><code>memset(buffer, '\\0', BUFSIZ);<\/code><\/p>\n<p>All the pieces are present: The buffer&#8217;s address, the character to fill, and the size. These appear in the first listing as well, but sprinkled throughout the code. Variable <code>x<\/code> is no longer needed. The <code>string.h<\/code> header file must be included; string functions are essentially memory-manipulation functions in C, which is why <em>memset()<\/em> is found there.<\/p>\n<p>With the <em>memset()<\/em> function, you can set any character code value for initializing the buffer. So if you want a buffer filled with the letter A, you set <code>'A'<\/code> as the second argument. But when you need to fill a buffer with null characters, you can instead use the handy (yet non-standard) <em>bzero()<\/em> function. Here is its format:<\/p>\n<p><code>void bzero(void *s, size_t n);<\/code><\/p>\n<p>Argument <code>s<\/code> is the <em>char<\/em> buffer to fill with null characters. Argument <code>n<\/code> is the buffer&#8217;s size in bytes. This function is prototyped in the <code>strings.h<\/code> header file. Many compilers include this header file when you use <code>string.h<\/code>, though for conformity I specify <code>strings.h<\/code> directly in the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_05_01-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2021_05_01-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;strings.h&gt;\r\n\r\nint main()\r\n{\r\n    char buffer[BUFSIZ];\r\n\r\n    bzero(buffer, BUFSIZ);\r\n    puts(\"Buffer initialized\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>bzero()<\/em> function appears at Line 8, replacing <em>memset()<\/em> from the preceding example. The program&#8217;s output is the same, though unlike <em>memset()<\/em>, <em>bzero()<\/em> always fills a buffer with null characters.<\/p>\n<p>I find <em>memset()<\/em> and <em>bzero()<\/em> to be delightful options for initializing a buffer. Still, I may write my own loops out of habit. Nothing wrong with that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a quick and effective way to initialize a buffer. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4728\">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-4728","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\/4728","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=4728"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4728\/revisions"}],"predecessor-version":[{"id":5244,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4728\/revisions\/5244"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}