<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pierre Kitz</title>
    <description>The latest articles on DEV Community by Pierre Kitz (@pkitz).</description>
    <link>https://dev.to/pkitz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2828546%2F9a84d10d-da05-4d3d-97a5-7bc99ed8e552.png</url>
      <title>DEV Community: Pierre Kitz</title>
      <link>https://dev.to/pkitz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pkitz"/>
    <language>en</language>
    <item>
      <title>Stack, Heap and How Function Calls Really Work</title>
      <dc:creator>Pierre Kitz</dc:creator>
      <pubDate>Fri, 30 May 2025 22:00:00 +0000</pubDate>
      <link>https://dev.to/pkitz/stack-heap-and-how-function-calls-really-work-3mc5</link>
      <guid>https://dev.to/pkitz/stack-heap-and-how-function-calls-really-work-3mc5</guid>
      <description>&lt;p&gt;Most programmers hear about stack and heap memory, but many don’t really get what that means until a bug bites them hard. Especially in languages like C or C++, where you are the garbage collector—understanding memory matters. A lot.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Memory Map You Should Know
&lt;/h2&gt;

&lt;p&gt;When you run a C program, memory is usually split into these segments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code: Holds compiled instructions, i.e. your program translated to machine language.&lt;/li&gt;
&lt;li&gt;Initialized Data: Some constants and literals, global variables and variables marked &lt;code&gt;static&lt;/code&gt; inside functions.&lt;/li&gt;
&lt;li&gt;Uninitialized Data: Reserved space for uninitialized global variables. This is often called 'Block Started by Symbol' (BSS), named after an old computer instruction.&lt;/li&gt;
&lt;li&gt;Heap: An unordered lot of memory that is organized by the programmer using &lt;code&gt;new&lt;/code&gt; or &lt;code&gt;malloc&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Stack: An automatically neatly stacked pile of memory used for function calls. It holds function arguments, local variables and bookkeeping information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F360w575fvh6ol6ow0gdy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F360w575fvh6ol6ow0gdy.png" alt="Memory Segments" width="246" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What Happens on a Function Call?
&lt;/h2&gt;

&lt;p&gt;Let's take a simple function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;increment(3)&lt;/code&gt; is called:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The return address is pushed to the stack (so the program knows where to come back).&lt;/li&gt;
&lt;li&gt;A stack frame is created with:

&lt;ul&gt;
&lt;li&gt;The argument &lt;code&gt;x&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The local variable &lt;code&gt;result&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3wkedf1cubifk3rj8t0a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3wkedf1cubifk3rj8t0a.png" alt="Stack Example" width="680" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the function returns, its stack frame is 'popped', and that memory is officially gone.&lt;/li&gt;
&lt;li&gt;However, in practice its still there but available to be overwritten as soon as another function is called. This is what can make this problem so mysterious.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So local variables exist only as long as the function runs. They live on the stack. And that is where people run into problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why This Matters – A Classic Mistake
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;create_message&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print_date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_message&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;print_date&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The local variable &lt;code&gt;result&lt;/code&gt; lives on the stack. When &lt;code&gt;create_message()&lt;/code&gt; returns, the stack frame is done. Now a pointer is returned to memory that will be used by the next function call. This is undefined behavior in C. A polite way of saying "good luck".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuhhp1z0leb2dht7jpuj9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuhhp1z0leb2dht7jpuj9.png" alt="Stack Evolution" width="775" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After calling &lt;code&gt;print_date&lt;/code&gt; the value created in &lt;code&gt;create_message()&lt;/code&gt; is likely overwritten by something else, often not even with the correct data type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Want to keep the result alive?
&lt;/h3&gt;

&lt;p&gt;Use the heap instead&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;create_message&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// allocate memory on the heap&lt;/span&gt;
    &lt;span class="n"&gt;strncpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print_date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_message&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;print_date&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// deallocate memory on the heap&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo1k2u6t5nnwiorw2y9zy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo1k2u6t5nnwiorw2y9zy.png" alt="Using the Heap" width="797" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the content of the message is created on the heap where it will continue to exist until it is &lt;code&gt;free()&lt;/code&gt;ed, independent of any function calls or stack manipulations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Stack memory is fast and automatically managed, but short-lived.&lt;/li&gt;
&lt;li&gt;Heap memory is manually managed and persistent across function calls but easy to leak.&lt;/li&gt;
&lt;li&gt;In non-garbage-collected languages, forgetting this distinction leads to real bugs: use-after-free, memory leaks, dangling pointers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This kind of foundational understanding unlocks many mysteries in C, C++, Rust, and even explains why some things behave weirdly in Java or Python.&lt;/p&gt;

&lt;h3&gt;
  
  
  Contact
&lt;/h3&gt;

&lt;p&gt;I am a university lecturer who enjoys helping developers understand how things work under the hood. If you are learning programming or struggling with C or C++, transitioning from Java, or just want to chat about memory, debugging, or low-level programming, feel free to reach out. You can contact via &lt;code&gt;&amp;lt;my username&amp;gt;&lt;/code&gt;@&lt;code&gt;mailbox&lt;/code&gt;.&lt;code&gt;org&lt;/code&gt;, or leave a comment below. I am happy to offer friendly advice or tutoring. If you are looking for 1:1 help or tutoring on C/C++, systems programming, or debugging, I'm exploring offering support sessions. Reach out and we can see what works.&lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Why You Do Not Love C or C++</title>
      <dc:creator>Pierre Kitz</dc:creator>
      <pubDate>Thu, 29 May 2025 12:55:59 +0000</pubDate>
      <link>https://dev.to/pkitz/why-you-do-not-love-c-or-c-d2o</link>
      <guid>https://dev.to/pkitz/why-you-do-not-love-c-or-c-d2o</guid>
      <description>&lt;p&gt;You might have dabbled in C or C++ before. It is versatile, ubiquitous and fast. But it can easily become fragile, cryptic and frustrating.&lt;/p&gt;

&lt;p&gt;Have you been hit be one or more of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cryptic compiler errors&lt;/li&gt;
&lt;li&gt;mysterious segmentation faults&lt;/li&gt;
&lt;li&gt;so called 'undefined behavior'&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You are not alone.&lt;/p&gt;

&lt;p&gt;C and C++ give you big power with minimal safety rails. And unless you understand what is going on under the hood, you are going to trip over things that feel invisible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Mistakes That Burn Newcomers
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Returning a pointer to a local variable&lt;/li&gt;
&lt;li&gt;Forgetting to free allocated memory (or freeing it twice)&lt;/li&gt;
&lt;li&gt;Accessing memory after it has been freed&lt;/li&gt;
&lt;li&gt;Buffer overflows while using &lt;code&gt;char*&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;new&lt;/code&gt; without &lt;code&gt;delete&lt;/code&gt;, or mixing with &lt;code&gt;malloc&lt;/code&gt; and &lt;code&gt;free&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;int*&lt;/code&gt; as if it where a Java reference&lt;/li&gt;
&lt;li&gt;Assuming arrays know their size (they do not)&lt;/li&gt;
&lt;li&gt;Ignoring return values from &lt;code&gt;malloc&lt;/code&gt;, &lt;code&gt;fopen&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;Believing &lt;code&gt;nullptr&lt;/code&gt; will save you&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What If It Did Not Have to Be This Way?
&lt;/h2&gt;

&lt;p&gt;What if you had:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A clear mental model of how memory really works&lt;/li&gt;
&lt;li&gt;Some simple rules to avoid 80% of bugs&lt;/li&gt;
&lt;li&gt;Tools that show you where your program is breaking&lt;/li&gt;
&lt;li&gt;A way to write C/C++ that is not just fast—but simple and safe&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Coming Soon: A Mini-Series for Beginners
&lt;/h2&gt;

&lt;p&gt;Topics that are often glossed over when you need to learn a language quickly. Knowledge that is not only useful for C or C++ but will help make you a better programmer in many languages.&lt;/p&gt;

&lt;p&gt;You will learn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How the stack and heap really work&lt;/li&gt;
&lt;li&gt;Why memory bugs happen (and how to catch them)&lt;/li&gt;
&lt;li&gt;Tools like &lt;a href="https://valgrind.org/" rel="noopener noreferrer"&gt;Valgrind&lt;/a&gt; and &lt;a href="https://github.com/google/sanitizers/wiki/AddressSanitizer" rel="noopener noreferrer"&gt;AddressSanitizer&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;How to write simpler, safer code using modern idioms&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No heavy theory. Just practical examples and clear takeaways.&lt;/p&gt;

&lt;p&gt;Stay tuned!&lt;/p&gt;

&lt;h3&gt;
  
  
  Contact
&lt;/h3&gt;

&lt;p&gt;I am a university lecturer who enjoys helping developers understand how things work under the hood. If you are learning programming or struggling with C or C++, transitioning from Java, or just want to chat about memory, debugging, or low-level programming, feel free to reach out. You can contact via &lt;code&gt;&amp;lt;my username&amp;gt;&lt;/code&gt;@&lt;code&gt;mailbox&lt;/code&gt;.&lt;code&gt;org&lt;/code&gt;, or leave a comment below. I am happy to offer friendly advice or tutoring. If you are looking for 1:1 help or tutoring on C/C++, systems programming, or debugging, I'm exploring offering support sessions. Reach out and we can see what works.&lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
