{"id":30256,"date":"2023-01-14T08:21:19","date_gmt":"2023-01-14T02:51:19","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/"},"modified":"2023-01-14T08:21:19","modified_gmt":"2023-01-14T02:51:19","slug":"solved-how-does-gcc-store-member-functions-in-memory","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/","title":{"rendered":"[Solved] How does GCC store member functions in memory?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-30414492\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"30414492\" data-parentid=\"30414365\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>For the purpose of in-memory data representation, a C++ <code>class<\/code> can have either plain or static member functions, or <code>virtual<\/code> member functions (including some <code>virtual<\/code>destructor, if any).<\/p>\n<p>Plain or static member functions do not take any space in data memory, but of course their compiled code take some resource, e.g. as binary code in the text or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Code_segment\">code segment<\/a> of your executable or your process. Of course, they can also require <code>static<\/code> data (or thread-local data), or local data (e.g. local variables) on the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Call_stack\">call stack<\/a>.<\/p>\n<p><sup>My answer is Linux oriented. I don&#8217;t know Windows, and don&#8217;t know how GCC work on it.<\/sup><\/p>\n<p>Virtual member functions are very often implemented thru <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Virtual_method_table\">virtual method table<\/a> (or <em>vtable<\/em>); a <code>class<\/code> having some virtual member functions usually have instances with a single (assuming single-<a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Inheritance_%28object-oriented_programming%29\">inheritance<\/a>) vtable-pointer pointing to that vtable (which is practically some data packed in the text segment).<\/p>\n<p><sup>Notice that vtables are not mandatory and are not required by C++11 standard. But I don&#8217;t know any C++ implementation not using them.<\/sup><\/p>\n<p>When you are using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Multiple_inheritance\">multiple-inheritance<\/a> things become more complex, objects might have <em>several<\/em> vtable pointers.<\/p>\n<p>So if you have a <code>class<\/code> (either a root class, or using <em>single-inheritance<\/em>), the consumption for virtual member functions is one vtable pointer per instance (plus the small space needed by the <em>single<\/em> vtable itself). It won&#8217;t change (for each instance) if you have only one virtual member function (or destructor) or a thousand of them (what would change is the vtable itself). Each class has its own <em>single<\/em> vtable (unless it has no virtual member function), and each instance has generally one (for single-inheritance case) vtable pointer.<\/p>\n<p>The GCC compiler is free to organize the vtable as it wishes (and its order and layout is an implementation detail you should not care about); see also this. In practice (for single-inheritance) for most recent GCC versions, the vtable pointer is the first word of the object, and the vtable contain function pointers in the order of virtual method declaration, but you should not depend on such details.<\/p>\n<p>The GCC compiler is free to organize the functions in the code segment as it wishes, and it would actually reorder them (e.g. for optimizations). Last time I looked, it ordered them in reverse order. But you certainly should not depend on that order! BTW GCC can inline functions (even when not marked <code>inline<\/code>) and clone functions when optimizing. You could also compile <em>and link<\/em> with <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Interprocedural_optimization\">link-time optimizations<\/a> (e.g. <code>make CXX='g++ -flto -Os'<\/code>), and you could ask for <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Profile-guided_optimization\">profile-guided optimizations<\/a> (for <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Optimize-Options.html\">GCC<\/a>: <code>-fprofile-generate<\/code>, <code>-fprofile-use<\/code>, <code>-fauto-profile<\/code> etc&#8230;)<\/p>\n<p><strong>You should not depend on how the compiler<\/strong> (and linker)  <strong>is organizing function code or <em>vtables<\/em><\/strong>. <em>Leave the optimizations to the compiler<\/em> (and such optimizations depend upon your target machine, your compiler flags, and the compiler version). You might also use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Function-Attributes.html\">function attributes<\/a> to give hints to the GCC (or Clang\/LLVM) compiler (e.g. <code>__attribute__((cold))<\/code>, <code>__attribute__((noinline))<\/code> etc etc&#8230;.)<\/p>\n<p>If you really need to know how functions are placed (which IMHO is very wrong), study the generated assembly code (e.g. using <code>g++ -O -fverbose-asm -S<\/code>) and be aware that it could vary with compiler versions!<\/p>\n<p>If you need on Linux and Posix systems at runtime to find out the address of a function from its name, consider using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/pubs.opengroup.org\/onlinepubs\/009695399\/functions\/dlsym.html\">dlsym<\/a> (for Linux, see <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man3\/dlsym.3.html\">dlsym(3)<\/a>, which also documents <code>dladdr<\/code>). Be aware of <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Name_mangling\">name mangling<\/a>, which you can disable by declaring such functions as <code>extern \"C\"<\/code> (see <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/tldp.org\/HOWTO\/C++-dlopen\/\">C++ dlopen minihowto<\/a>).<\/p>\n<p>BTW, you might compile and link with <code>-rdynamic<\/code> (which is very useful for <code>dlopen<\/code> etc&#8230;). If you really need to know the address of functions, use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/man7.org\/linux\/man-pages\/man1\/nm.1.html\">nm(1)<\/a> as <code>nm -C<\/code> <em>your-executable<\/em>.<\/p>\n<p>You might also read the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Application_binary_interface\">ABI<\/a> specification and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Calling_convention\">calling conventions<\/a> for your target platform (and compiler), e.g. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.x86-64.org\/documentation\/abi.pdf\">Linux x86-64 ABI spec<\/a>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How does GCC store member functions in memory? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] For the purpose of in-memory data representation, a C++ class can have either plain or static member functions, or virtual member functions (including some virtualdestructor, if any). Plain or static member functions do not take any space in data memory, but of course their compiled code take some resource, e.g. as binary code in &#8230; <a title=\"[Solved] How does GCC store member functions in memory?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/\" aria-label=\"More on [Solved] How does GCC store member functions in memory?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,4945,608,1369,325],"class_list":["post-30256","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-compiler-optimization","tag-gcc","tag-optimization","tag-performance"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] How does GCC store member functions in memory? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How does GCC store member functions in memory? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] For the purpose of in-memory data representation, a C++ class can have either plain or static member functions, or virtual member functions (including some virtualdestructor, if any). Plain or static member functions do not take any space in data memory, but of course their compiled code take some resource, e.g. as binary code in ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-14T02:51:19+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-does-gcc-store-member-functions-in-memory\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-does-gcc-store-member-functions-in-memory\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How does GCC store member functions in memory?\",\"datePublished\":\"2023-01-14T02:51:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-does-gcc-store-member-functions-in-memory\\\/\"},\"wordCount\":631,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"c++\",\"compiler-optimization\",\"gcc\",\"optimization\",\"performance\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-does-gcc-store-member-functions-in-memory\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-does-gcc-store-member-functions-in-memory\\\/\",\"name\":\"[Solved] How does GCC store member functions in memory? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2023-01-14T02:51:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-does-gcc-store-member-functions-in-memory\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-does-gcc-store-member-functions-in-memory\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-does-gcc-store-member-functions-in-memory\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How does GCC store member functions in memory?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How does GCC store member functions in memory? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How does GCC store member functions in memory? - JassWeb","og_description":"[ad_1] For the purpose of in-memory data representation, a C++ class can have either plain or static member functions, or virtual member functions (including some virtualdestructor, if any). Plain or static member functions do not take any space in data memory, but of course their compiled code take some resource, e.g. as binary code in ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/","og_site_name":"JassWeb","article_published_time":"2023-01-14T02:51:19+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How does GCC store member functions in memory?","datePublished":"2023-01-14T02:51:19+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/"},"wordCount":631,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","compiler-optimization","gcc","optimization","performance"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/","url":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/","name":"[Solved] How does GCC store member functions in memory? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-14T02:51:19+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-does-gcc-store-member-functions-in-memory\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How does GCC store member functions in memory?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=30256"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30256\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=30256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=30256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=30256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}