{"id":293,"date":"2011-03-03T23:55:42","date_gmt":"2011-03-03T22:55:42","guid":{"rendered":"http:\/\/trigonakis.com\/blog\/?p=293"},"modified":"2011-03-06T12:34:18","modified_gmt":"2011-03-06T11:34:18","slug":"structures-and-memory-consumption-in-c","status":"publish","type":"post","link":"http:\/\/trigonakis.com\/blog\/2011\/03\/03\/structures-and-memory-consumption-in-c\/","title":{"rendered":"Structures and Memory Consumption in C"},"content":{"rendered":"<h3>Motivation<\/h3>\n<p>The way you design you structures in C can have a (significant) impact on the <strong>size they occupy in memory<\/strong>!<\/p>\n<h3>Memory structure of a structure<\/h3>\n<p>A structure occupies <strong>contiguous space<\/strong> in memory. Though, in order to provide efficient access to the structure&#8217;s fields, C uses <i>padding<\/i>. In other words, the fields are aligned to the memory <code>words<\/code>.<\/p>\n<h4>Example<\/h4>\n<p>In a system with:<\/p>\n<ul>\n<li><code>word : 4 bytes<\/code><\/li>\n<li><code>char : 1 byte<\/code><\/li>\n<li><code>int  : 4 bytes<\/code><\/li>\n<\/ul>\n<p>without padding would be:<\/p>\n<table>\n<tr>\n<th width=\"20%\"><code>char (8bit)<\/code><\/th>\n<th><code>int (32bit)<\/code><\/th>\n<\/tr>\n<\/table>\n<p>and since the <code>word<\/code> size is <code>4 bytes<\/code> the integer would belong to 2 different <code>words<\/code>, so in order to access it, two memory accesses would be necessary.<br \/>\nIn order to avoid it, the actual memory organization is:<\/p>\n<table>\n<tr>\n<th width=\"13%\"><code>char (8bit)<\/code><\/th>\n<th width=\"37%\"><code>padding (24bit)<\/code><\/th>\n<th><code>int (32bit)<\/code><\/th>\n<\/tr>\n<\/table>\n<h3>The Possible Problem<\/h3>\n<p>A bad designed structure can possible occupy more space than needed.<\/p>\n<h4>Example<\/h4>\n<p>Take a look at the following structure:<\/p>\n<pre lang=\"c\">\r\nstruct bad {\r\n  char c1;\r\n  int i1;\r\n  char c2;\r\n};\r\n<\/pre>\n<p>One would possibly say that since <code>1 + 4 + 1 = 6 bytes<\/code> is the total size of the fields, <code>2 words = 8 bytes<\/code> will be needed to store it. This <strong>is not true<\/strong>. Due to the padding we have:<\/p>\n<table>\n<tr>\n<th width=\"8%\"><code>c1 (8bit)<\/code><\/th>\n<th width=\"26%\"><code>padding (24bit)<\/code><\/th>\n<th><code>i1 (32bit)<\/code><\/th>\n<th width=\"8%\"><code>c2 (8bit)<\/code><\/th>\n<th width=\"26%\"><code>padding (24bit)<\/code><\/th>\n<\/tr>\n<\/table>\n<p><code>3 words = 12 bytes<\/code>, <strong>4 more <code>bytes<\/code> than necessary<\/strong>.<\/p>\n<h4>Solution<\/h4>\n<p>Just take into account how the types fit together:<\/p>\n<pre lang=\"c\">\r\nstruct good {\r\n  char c1; \/\/c1 + c2 = 2 bytes\r\n  char c2;\r\n  int i1;\r\n};\r\n<\/pre>\n<p>This design needs the minimum number of <code>2 words = 8 bytes<\/code>.<\/p>\n<h3>Outcome<\/h3>\n<p>The bigger the structure gets, the more space you can lose.. So, take care of it while designing the structures!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Motivation The way you design you structures in C can have a (significant) impact on the size they occupy in memory! Memory structure of a structure A structure occupies contiguous space in memory. Though, in order to provide efficient access to the structure&#8217;s fields, C uses padding. In other words, the fields are aligned to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[28],"tags":[29,52,46,45],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ouW6-4J","_links":{"self":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/293"}],"collection":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/comments?post=293"}],"version-history":[{"count":17,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":368,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/293\/revisions\/368"}],"wp:attachment":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/media?parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/categories?post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/tags?post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}