{"id":32113,"date":"2023-01-26T20:17:28","date_gmt":"2023-01-26T14:47:28","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/"},"modified":"2023-01-26T20:17:28","modified_gmt":"2023-01-26T14:47:28","slug":"solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/","title":{"rendered":"[Solved] function returns address of local variable, but it still compile in c, why?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-63285925\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"63285925\" data-parentid=\"63283986\" data-score=\"6\" 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<blockquote>\n<p>Even I get an warning a function returns an address from local<br \/>\nvariable, it compiles. Isn&#8217;t it then UB of compiler?<\/p>\n<\/blockquote>\n<p>No, but if it were, how could you tell?  You seem to have a misunderstanding of undefined behavior.  It does not mean &#8220;the compiler must reject it&#8221;, &#8220;the compiler must warn about it&#8221;, &#8220;the program must terminate&#8221;, or any such thing.  Those indeed <em>may<\/em> be a manifestations of UB, but if the language specification required such behavior then it wouldn&#8217;t be <em>undefined<\/em>.  <strong>Ensuring that a C program does not exercise undefined behavior is the responsibility of the programmer, not the C implementation.<\/strong>  Where a programmer does not fulfill that responsibility, the C implementation explicitly has no reciprocal responsibility &#8212; it can do anything within its capabilities.<\/p>\n<p>Moreover, there is no single &#8220;the&#8221; C compiler.  Different compilers may do things differently and still conform to the C language specifications.  This is where implementation-defined, unspecified, and undefined behaviors come in.  Allowing such variance is intentional on the part of the C language designers. Among other things, it allows implementations to operate in ways that are natural for their particular target hardware and execution environments.<\/p>\n<p>Now let&#8217;s go back to &#8220;no&#8221;.  Here is a prototypical example of a function returning the address of an automatic variable:<\/p>\n<pre><code>int *foo() {\n    int bar = 0;\n    return &amp;bar;\n}\n<\/code><\/pre>\n<p>What about that is supposed to have undefined behavior?  It is well defined for the function to compute the address of <code>bar<\/code>, and the resulting pointer value has the correct type to be returned by the function.  After <code>bar<\/code>&#8216;s lifetime ends when the function returns, the return value becomes indeterminate (paragraph 6.2.4\/2 of the standard), but that does not in itself give rise to any undefined behavior.<\/p>\n<p>Or consider a caller:<\/p>\n<pre><code>void test1() {\n    int *bar_ptr = foo();  \/\/ OK under all circumstances\n}\n<\/code><\/pre>\n<p>As already discussed, our particular <code>foo()<\/code>&#8216;s return value will always be indeterminate, so in particular, it might be a trap representation.  But that&#8217;s a runtime consideration, not a compile-time one.  And even if the value were a trap representation, C does not require that the implementation refuse or fail to store it.  In particular, footnote 50 to C11 is explicit on this point:<\/p>\n<blockquote>\n<p>Thus, an automatic variable can be initialized to a trap<br \/>\nrepresentation without causing undefined behavior, but the value of<br \/>\nthe variable cannot be used until a proper value is stored in it.<\/p>\n<\/blockquote>\n<p>Note also that <code>foo()<\/code> and <code>test1()<\/code> can be compiled by different runs of the compiler, such that when compiling <code>test1()<\/code>, the compiler knows nothing about the behavior of <code>foo()<\/code> beyond what is indicated by its prototype.  C does not place translation-time requirements on implementations that depend on the runtime behavior of programs.<\/p>\n<p>On the other hand, the requirements around trap representations would apply differently if the function were modified slightly:<\/p>\n<pre><code>void test2() {\n    int *bar_ptr = NULL;\n    bar_ptr = foo();      \/\/ UB (only) if foo() returns a trap representation\n}\n<\/code><\/pre>\n<p>If the return value of <code>foo()<\/code> turns out to be a trap representation, then <em>storing<\/em> it in <code>bar_ptr<\/code> (as opposed to <em>initializing<\/em> <code>bar_ptr<\/code> with it) produces undefined behavior at runtime.  Again, however, &#8220;undefined&#8221; means just what it says on the tin.  C does not define any particular behavior for implementations to exhibit under the circumstances, and in particular, it does not require that programs terminate or manifest any externally-visible behavior at all.  And again, that&#8217;s a runtime consideration, not a compile-time one.<\/p>\n<p>Furthermore, if <code>foo()<\/code>&#8216;s return value turns out not to be a trap representation (being instead a pointer value that is not the address of any live object), then there&#8217;s nothing wrong with reading that value itself, either:<\/p>\n<pre><code>void test3() {\n    int *bar_ptr = foo();\n    \/\/ UB (only) if foo() returned a trap representation:\n    printf(\"foo() returned %p\\n\", (void *) bar_ptr);\n}\n<\/code><\/pre>\n<p>The biggest and most commonly-exercised undefined behavior in this area would be that of trying to dereference the return value of <code>foo()<\/code>, which, trap representation or not, almost surely does not point to a live <code>int<\/code> object:<\/p>\n<pre><code>void test4() {\n    int *bar_ptr = foo();\n    \/\/ UB under all circumstances for the given foo():\n    printf(\"foo() returned a pointer to an int with value %d\\n\", *bar_ptr);\n}\n<\/code><\/pre>\n<p>But again, that&#8217;s a runtime consideration, not a compile-time one. And again, undefined means undefined.  The C implementation should be expected to translate that successfully as long as there are in-scope declarations for the functions involved, and although some compilers might warn, they have no obligation to do so.  The runtime behavior of function <code>test4<\/code> is undefined, but that does not mean the program necessarily will segfault or terminate in some other manner.  It might, but I expect that in practice, the undefined behavior manifested by a great many implementations would be to print &#8220;foo() returned a pointer to an int with value 0&#8221;.  Doing so is in no way inconsistent with C&#8217;s requirements.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved function returns address of local variable, but it still compile in c, why? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Even I get an warning a function returns an address from local variable, it compiles. Isn&#8217;t it then UB of compiler? No, but if it were, how could you tell? You seem to have a misunderstanding of undefined behavior. It does not mean &#8220;the compiler must reject it&#8221;, &#8220;the compiler must warn about it&#8221;, &#8230; <a title=\"[Solved] function returns address of local variable, but it still compile in c, why?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/\" aria-label=\"More on [Solved] function returns address of local variable, but it still compile in c, why?\">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":[465,324,943,2797,523],"class_list":["post-32113","post","type-post","status-publish","format-standard","hentry","category-solved","tag-assembly","tag-c","tag-compiler-errors","tag-undefined-behavior","tag-x86"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] function returns address of local variable, but it still compile in c, why? - 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-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] function returns address of local variable, but it still compile in c, why? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Even I get an warning a function returns an address from local variable, it compiles. Isn&#8217;t it then UB of compiler? No, but if it were, how could you tell? You seem to have a misunderstanding of undefined behavior. It does not mean &#8220;the compiler must reject it&#8221;, &#8220;the compiler must warn about it&#8221;, ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-26T14:47:28+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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] function returns address of local variable, but it still compile in c, why?\",\"datePublished\":\"2023-01-26T14:47:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/\"},\"wordCount\":744,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"assembly\",\"c++\",\"compiler-errors\",\"undefined-behavior\",\"x86\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/\",\"name\":\"[Solved] function returns address of local variable, but it still compile in c, why? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-26T14:47:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] function returns address of local variable, but it still compile in c, why?\"}]},{\"@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\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] function returns address of local variable, but it still compile in c, why? - 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-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] function returns address of local variable, but it still compile in c, why? - JassWeb","og_description":"[ad_1] Even I get an warning a function returns an address from local variable, it compiles. Isn&#8217;t it then UB of compiler? No, but if it were, how could you tell? You seem to have a misunderstanding of undefined behavior. It does not mean &#8220;the compiler must reject it&#8221;, &#8220;the compiler must warn about it&#8221;, ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/","og_site_name":"JassWeb","article_published_time":"2023-01-26T14:47:28+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] function returns address of local variable, but it still compile in c, why?","datePublished":"2023-01-26T14:47:28+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/"},"wordCount":744,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["assembly","c++","compiler-errors","undefined-behavior","x86"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/","url":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/","name":"[Solved] function returns address of local variable, but it still compile in c, why? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-26T14:47:28+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-function-returns-address-of-local-variable-but-it-still-compile-in-c-why\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] function returns address of local variable, but it still compile in c, why?"}]},{"@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\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","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\/32113","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=32113"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/32113\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=32113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=32113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=32113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}