{"id":5085,"date":"2014-03-30T18:43:55","date_gmt":"2014-03-30T18:43:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/a-free-invalid-next-size-fast-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:43:55","modified_gmt":"2014-03-30T18:43:55","slug":"a-free-invalid-next-size-fast-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/a-free-invalid-next-size-fast-in-c-collection-of-common-programming-errors\/","title":{"rendered":"A &ldquo;free(): invalid next size (fast)&rdquo; in C++-Collection of common programming errors"},"content":{"rendered":"<p>I just ran into a <code>free(): invalid next size (fast)<\/code> problem while writing a C++ program. And I failed to figure out why this could happen unfortunately. The code is given below.<\/p>\n<pre><code>bool not_corrupt(struct packet *pkt, int size)\n{\n    if (!size) return false;\n    bool result = true;\n    char *exp_checksum = (char*)malloc(size * sizeof(char));\n    char *rec_checksum = (char*)malloc(size * sizeof(char));\n    char *rec_data = (char*)malloc(size * sizeof(char));\n    \/\/memcpy(rec_checksum, pkt-&gt;data+HEADER_SIZE+SEQ_SIZE+DATA_SIZE, size);\n    \/\/memcpy(rec_data, pkt-&gt;data+HEADER_SIZE+SEQ_SIZE, size);\n    for (int i = 0; i &lt; size; i++) {\n        rec_checksum[i] = pkt-&gt;data[HEADER_SIZE+SEQ_SIZE+DATA_SIZE+i];\n        rec_data[i] = pkt-&gt;data[HEADER_SIZE+SEQ_SIZE+i];\n    }\n    do_checksum(exp_checksum, rec_data, DATA_SIZE);\n    for (int i = 0; i &lt; size; i++) {\n        if (exp_checksum[i] != rec_checksum[i]) {\n            result = false;\n            break;\n        }\n    }\n    free(exp_checksum);\n    free(rec_checksum);\n    free(rec_data);\n    return result;\n}\n<\/code><\/pre>\n<p>The macros used are:<\/p>\n<pre><code>#define RDT_PKTSIZE 128\n#define SEQ_SIZE 4\n#define HEADER_SIZE 1\n#define DATA_SIZE ((RDT_PKTSIZE - HEADER_SIZE - SEQ_SIZE) \/ 2)\n<\/code><\/pre>\n<p>The struct used is:<\/p>\n<pre><code>struct packet {\n    char data[RDT_PKTSIZE];\n};\n<\/code><\/pre>\n<p>This piece of code doesn&#8217;t go wrong every time. It would crash with the <code>free(): invalid next size (fast)<\/code> sometimes in the <code>free(exp_checksum);<\/code> part.<\/p>\n<p>What&#8217;s even worse is that sometimes what&#8217;s in <code>rec_checksum<\/code> stuff is just not equal to what&#8217;s in <code>pkt-&gt;data[HEADER_SIZE+SEQ_SIZE+DATA_SIZE]<\/code> stuff, which should be the same according to the watch expressions from my debugging tools. Both <code>memcpy<\/code> and <code>for<\/code> methods are used but this problem remains.<\/p>\n<p>I don&#8217;t quite understand why this would happen. I would be very thankful if anyone could explain this to me.<\/p>\n<p><strong>Edit:<\/strong><\/p>\n<p>Here&#8217;s the do_checksum() method, which is very simple:<\/p>\n<pre><code>void do_checksum(char* checksum, char* data, int size)\n{\n    for (int i = 0; i &lt; size; i++)\n    {\n        checksum[i] = ~data[i];\n    }\n}\n<\/code><\/pre>\n<p><strong>Edit 2:<\/strong><\/p>\n<p>Thanks for all.<\/p>\n<p>I switched other part of my code from the usage of STL queue to STL vector, the results turn to be cool then.<\/p>\n<p>But still I didn&#8217;t figure out why. I am sure that I would never pop an empty queue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I just ran into a free(): invalid next size (fast) problem while writing a C++ program. And I failed to figure out why this could happen unfortunately. The code is given below. bool not_corrupt(struct packet *pkt, int size) { if (!size) return false; bool result = true; char *exp_checksum = (char*)malloc(size * sizeof(char)); char *rec_checksum [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5085","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5085"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5085\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}