<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by exploreIT on Medium]]></title>
        <description><![CDATA[Stories by exploreIT on Medium]]></description>
        <link>https://medium.com/@exploreit-askdoubt?source=rss-4961b3a701------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ddGY6DFKXtxeBlEasJNPqg.png</url>
            <title>Stories by exploreIT on Medium</title>
            <link>https://medium.com/@exploreit-askdoubt?source=rss-4961b3a701------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 21:40:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@exploreit-askdoubt/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Unraveling Network Packets: The Journey of Data Across the Internet]]></title>
            <link>https://exploreit-askdoubt.medium.com/unraveling-network-packets-the-journey-of-data-across-the-internet-2069ba505b72?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/2069ba505b72</guid>
            <category><![CDATA[internet-backbone]]></category>
            <category><![CDATA[data-transmission]]></category>
            <category><![CDATA[tcp-ip]]></category>
            <category><![CDATA[internet]]></category>
            <category><![CDATA[network-packets]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Fri, 29 Aug 2025 17:27:33 GMT</pubDate>
            <atom:updated>2025-08-29T17:27:33.992Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever wondered what exactly happens when you hit ‘Send’ on that WhatsApp message or click a YouTube video? Like, where does your data actually go and how does it magically appear on the other person’s screen?</p><p>Well, it’s not magic at all! It’s an incredible journey that your data takes, chopped up into tiny pieces called <strong>packets</strong>, traveling through a complex network of routers, switches, and cables to reach its destination.</p><p>Umm… sounds pretty complex, right? Don’t worry, I’m gonna walk you through this entire journey step by step, and trust me, by the end of this article you’ll have a clear picture of how the internet actually works behind the scenes.</p><h4>But Wait, What Exactly is a Network Packet?</h4><p>Think of a packet as a small envelope containing a piece of your data. When you send a message or request a webpage, your computer doesn’t send it all at once as one big chunk. Instead, it breaks it down into smaller, manageable pieces called packets.</p><p>Each packet contains:</p><ul><li><strong>Header</strong>: Information about where it’s going (destination), where it came from (source), and how to put all packets back together</li><li><strong>Payload</strong>: The actual data (part of your message, image, video, etc.)</li><li><strong>Footer</strong>: Error checking information to make sure data didn’t get corrupted</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*aqGm-UZu3ACiaTqskVhZvg.png" /><figcaption>Fig-1: Network packet acrhitecture</figcaption></figure><p>Pretty neat, isn’t it? <strong>But why break it into packets? </strong>Well, imagine trying to send a huge file through a narrow pipe — it would take forever and block everything else. By breaking it into packets, multiple communications can share the same network efficiently.</p><h3>The Journey Begins: Your Computer’s Network Stack</h3><p>Alright, let’s trace a packet’s journey from your computer. Suppose you’re trying to access www.google.com from your browser.</p><h4>Step 1: Application Layer (HTTP/HTTPS)</h4><p>Your browser creates an HTTP request. If you’re using modern browsers, it might use <strong>HTTP/2</strong> or even <strong>QUIC</strong> protocol. These newer protocols are pretty cool:</p><ul><li><a href="https://en.wikipedia.org/wiki/HTTP/2"><strong>HTTP/2</strong></a>: Allows multiple requests simultaneously over a single connection</li><li><a href="https://en.wikipedia.org/wiki/QUIC"><strong>QUIC</strong></a>: Built on UDP, provides faster connection establishment (developed by Google, now standardized)</li></ul><p>But let’s stick with traditional HTTP for now to keep things simple.</p><h4>Step 2: Where is google.com Actually Located?</h4><p>Here comes <strong>DNS (Domain Name System). </strong>Your computer doesn’t know where www.google.com actually lives on the internet. It&#39;s like knowing someone&#39;s name but not their address.</p><p>So your computer first sends a DNS query:</p><pre>&quot;Hey DNS server, what&#39;s the IP address of www.google.com?&quot;</pre><p>The DNS server responds:</p><pre>&quot;Oh, that&#39;s 142.250.190.78&quot; (or some other IP address)</pre><p>This DNS resolution usually happens so fast you don’t even notice it!</p><h4>Step 3: Transport Layer (TCP)</h4><p>Now that we have the IP address, <strong>TCP (Transmission Control Protocol)</strong> takes over. TCP is like a reliable postal service — it makes sure all packets reach their destination and in the right order.</p><p>TCP does something called a <strong>Three-Way Handshake</strong>:</p><ol><li>Your computer: “Hey, I want to talk!” (SYN)</li><li>Google’s server: “Sure, I’m ready to listen!” (SYN-ACK)</li><li>Your computer: “Great, let’s start!” (ACK)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*wNmApFTld-yaSXFkK7cQJQ.png" /><figcaption>Fig-2: Three-Way Handshake</figcaption></figure><p>Only after this handshake, actual data transmission begins.</p><h4>Step 4: Network Layer (IP)</h4><p>The <strong>Internet Protocol (IP)</strong> adds routing information to each packet. Think of it as writing the complete address on your envelope — it includes:</p><ul><li>Source IP (your computer’s address)</li><li>Destination IP (Google’s server address)</li><li>Some routing information</li></ul><h4>Step 5: Data Link &amp; Physical Layer</h4><p>Finally, the packet gets converted into electrical signals (or light signals if fiber optic) and sent through your network cable or WiFi.</p><h3>The Great Network Adventure</h3><p>Now comes the exciting part — the packet’s journey through the internet!</p><h4>Step 1: Your Home Router</h4><p>First stop: your home router. This little device is smarter than you think! It maintains something called a <strong>routing table</strong> — basically a map of where different networks are located.</p><p>Your router looks at the packet and thinks:</p><pre>&quot;Hmm, this packet wants to go to 142.250.190.78. <br>That&#39;s not on my local network, so I&#39;ll send it to my ISP.&quot;</pre><h4>Step 2: Internet Service Provider (ISP)</h4><p>Your ISP has much bigger routers with more comprehensive routing tables. These routers use algorithms like <strong>OSPF (Open Shortest Path First)</strong> or <strong>BGP (Border Gateway Protocol)</strong> to determine the best path for your packet.</p><p>It’s like GPS for packets. The router analyzes various paths and selects the one that’s:</p><ul><li>Shortest (in terms of network hops)</li><li>Least congested</li><li>Most reliable</li></ul><h4>Step 3: The Internet Backbone</h4><p>Your packet now travels through the internet backbone — high-speed fiber optic cables connecting major cities, countries, and continents. These are operated by <strong>Tier 1 ISPs</strong> like AT&amp;T, Verizon, and others.</p><p>At each hop, routers examine the packet’s destination and forward it closer to Google’s servers. This process is called <strong>packet forwarding</strong>.</p><h4>Step 4: Reaching Google’s Network</h4><p>Eventually, your packet reaches Google’s network infrastructure. Google has data centers worldwide, and through a process called <strong>Anycast routing</strong>, your packet gets directed to the nearest data center.</p><h4>Step 5: The Response Journey</h4><p>Google’s server processes your request and sends back a response (like the HTML content of their homepage). This response also gets broken into packets and follows a similar journey back to your computer.</p><p><strong>Here’s where TCP’s reliability shines:</strong></p><ul><li>If any packet gets lost during transmission, TCP detects it and requests retransmission</li><li>TCP also ensures packets are reassembled in the correct order</li><li>It handles flow control to prevent overwhelming your connection</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/1*w9sK1B8YIGO0m07wZtIqJw.png" /><figcaption>Fig-3: Journey of a Network Packet</figcaption></figure><h3>What Can Go Wrong?</h3><p>Of course, not everything always goes smoothly in this journey:</p><h4>Packet Loss</h4><p>Sometimes packets get dropped due to:</p><ul><li>Network congestion</li><li>Hardware failures</li><li>Transmission errors</li></ul><p>TCP handles this by retransmitting lost packets.</p><h4>Latency Issues</h4><p>The physical distance and number of hops can cause delays. This is why:</p><ul><li>Gaming servers are often located regionally</li><li>CDNs (Content Delivery Networks) cache content closer to users</li><li>Companies like Netflix have servers at ISP locations</li></ul><h4>Network Congestion</h4><p>During peak hours, networks can get congested, similar to traffic jams. Modern protocols use various techniques to handle this:</p><ul><li><strong>Traffic shaping</strong>: Prioritizing certain types of traffic</li><li><strong>Load balancing</strong>: Distributing traffic across multiple paths</li><li><strong>QoS (Quality of Service)</strong>: Ensuring critical traffic gets priority</li></ul><h3>The Complete Picture</h3><p>So, to summarize this incredible journey:</p><ol><li><strong>Your Browser</strong> creates an HTTP request</li><li><strong>DNS</strong> resolves the domain name to an IP address</li><li><strong>TCP</strong> ensures reliable delivery with its three-way handshake</li><li><strong>IP</strong> handles the routing and addressing</li><li><strong>Your Router</strong> forwards the packet toward the destination</li><li><strong>ISP Routers</strong> use sophisticated algorithms to find the best path</li><li><strong>Internet Backbone</strong> carries the packet across vast distances</li><li><strong>Destination Network</strong> receives and processes the packet</li><li><strong>Server</strong> sends back the response following the reverse path</li><li><strong>Your Browser</strong> receives and displays the content</li></ol><p>And all of this happens in milliseconds! Pretty amazing, isn’t it?</p><h3>Why Understanding This Matters</h3><p>You might wonder, “Why should I care about all this technical stuff?” Well, understanding how packets travel helps you:</p><ul><li><strong>Troubleshoot network issues</strong> — When your internet is slow, you can better understand where the bottleneck might be</li><li><strong>Make better architectural decisions</strong> — If you’re developing applications, understanding network behavior helps you design better systems</li><li><strong>Understand security implications</strong> — Knowing how data travels helps you understand why HTTPS and VPNs are important</li></ul><p>The next time your video call lags or a webpage takes time to load, you’ll know there’s a complex dance of millions of packets happening behind the scenes, each finding its own path through the vast network we call <strong>the internet</strong>.</p><blockquote><strong>Hey, did you like the explanation?</strong></blockquote><blockquote>If yes then don’t forget to follow <a href="https://exploreit-askdoubt.medium.com/about"><strong>exploreIT </strong></a>as I regularly come up with interesting topics related to programming. And of course, <strong><em>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</em></strong></blockquote><blockquote>Feel free to connect — <a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/">https://www.linkedin.com/in/salman-shaikh-82989b1b9/</a></blockquote><blockquote><strong><em>See you in the next article, Bye Bye!</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2069ba505b72" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Inside the Vtable: How C++ Achieves Polymorphism]]></title>
            <link>https://exploreit-askdoubt.medium.com/inside-the-vtable-how-c-achieves-polymorphism-698be2229eec?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/698be2229eec</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[vtable]]></category>
            <category><![CDATA[polymorphism]]></category>
            <category><![CDATA[virtual-function]]></category>
            <category><![CDATA[cpp-programming]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Mon, 25 Aug 2025 17:58:15 GMT</pubDate>
            <atom:updated>2025-08-25T18:02:33.694Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever wondered what happens behind the scenes when you call a virtual function in C++? I mean, how does the compiler know which function to call at runtime?</p><p>Well, the answer lies in something called <strong>vtable</strong> (Virtual Table). But wait, before we dive into vtables, let me ask you something- <strong>have you ever thought why virtual functions are “costlier” than regular functions?</strong> Or why people say <strong>“there’s a hidden cost to polymorphism”</strong>?</p><p>Hmm… If you’re curious about these questions, then you’re in the right place! Today we’re gonna explore the mysterious world of vtables and see how C++ actually achieves runtime polymorphism.</p><h4>First Things First: What Problem Are We Solving?</h4><p>Let me show you a simple code to understand what we’re dealing with:</p><pre>class Animal {<br>public:<br>    void speak() { cout &lt;&lt; &quot;Some sound&quot; &lt;&lt; endl; }<br>};<br><br>class Dog : public Animal {<br>public:<br>    void speak() { cout &lt;&lt; &quot;Woof!&quot; &lt;&lt; endl; }<br>};<br><br>int main() {<br>    Animal* ptr = new Dog();<br>    ptr-&gt;speak();  // Output: Some sound<br>    return 0;<br>}</pre><p>Oops! Even though we’re pointing to a Dog object, we get “Some sound” instead of “Woof!”. This is because without virtual, the compiler uses static binding- it decides at compile time to call Animal&#39;s speak() based on the pointer type.</p><p>Now let’s make it virtual:</p><pre>class Animal {<br>public:<br>    virtual void speak() { cout &lt;&lt; &quot;Some sound&quot; &lt;&lt; endl; }<br>};<br><br>class Dog : public Animal {<br>public:<br>    void speak() { cout &lt;&lt; &quot;Woof!&quot; &lt;&lt; endl; }<br>};<br><br>int main() {<br>    Animal* ptr = new Dog();<br>    ptr-&gt;speak();  // Output: Woof!<br>    return 0;<br>}</pre><p>Yesss! Now we get “Woof!” as expected. But HOWWW...? This is where vtables come into play!</p><h4>So, What Exactly is a Vtable?</h4><p>A vtable (or Virtual Function Table) is basically a lookup table of function pointers. Think of it as a menu card in a restaurant- you look at the menu (vtable) to see what dishes (functions) are available, and then you order (call) the one you want.</p><p>Here’s what happens internally:</p><ul><li>Every class that has at least one virtual function gets its own vtable</li><li>This vtable is created at compile time (yes, compile time!)</li><li>Each object of that class contains a hidden pointer called <strong>vptr</strong> (virtual pointer) that points to its class’s vtable</li></ul><p>Umm… sounds theoretical? Let me draw a simple diagram to make it clearer:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*kkV0qgK1oiF-zMPUivo9ww.png" /><figcaption>fig-1: Virtual Pointer to VTable Mapping</figcaption></figure><h4>The Hidden Cost: Your Object Just Got Bigger!</h4><p>Here’s something interesting- when you add virtual functions to a class, your objects become larger! Let me prove it:</p><pre>class WithoutVirtual {<br>    int data;<br>public:<br>    void func() { }<br>};<br><br>class WithVirtual {<br>    int data;<br>public:<br>    virtual void func() { }<br>};<br><br>int main() {<br>    cout &lt;&lt; &quot;Size without virtual: &quot; &lt;&lt; sizeof(WithoutVirtual) &lt;&lt; endl;<br>    cout &lt;&lt; &quot;Size with virtual: &quot; &lt;&lt; sizeof(WithVirtual) &lt;&lt; endl;<br>    return 0;<br>}</pre><p>Output (on a 64-bit system):</p><pre>Size without virtual: 4<br>Size with virtual: 16</pre><p>Whoa! The object with virtual function is 16 bytes instead of 4. That extra space? It’s the vptr (8 bytes on 64-bit systems) plus padding for alignment.</p><h4>How Does the Compiler Build These Vtables?</h4><p>The compiler is pretty smart about this. Here’s what it does:</p><ol><li><strong>During Compilation</strong>: It scans each class and creates a vtable for classes with virtual functions</li><li><strong>Vtable Contents</strong>: For each virtual function, it adds an entry with the most derived version’s address</li><li><strong>Object Construction</strong>: When you create an object, the constructor secretly sets up the vptr to point to the correct vtable</li></ol><p>Let’s see this in action with a bit more complex example:</p><pre>class Base {<br>public:<br>    virtual void func1() { cout &lt;&lt; &quot;Base::func1&quot; &lt;&lt; endl; }<br>    virtual void func2() { cout &lt;&lt; &quot;Base::func2&quot; &lt;&lt; endl; }<br>    void func3() { cout &lt;&lt; &quot;Base::func3&quot; &lt;&lt; endl; }  // Not virtual<br>};<br><br>class Derived : public Base {<br>public:<br>    void func1() override { cout &lt;&lt; &quot;Derived::func1&quot; &lt;&lt; endl; }<br>    // func2 not overridden<br>    void func3() { cout &lt;&lt; &quot;Derived::func3&quot; &lt;&lt; endl; }<br>};</pre><p>The vtables would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*n4zAbV2QKQSC2fCORU7evA.png" /><figcaption>fig-2: Base-Derived VTable Structure Comparison</figcaption></figure><h4>The Performance Trade-offs</h4><p>Now you may think- “Is this vtable thing slow?”</p><p>Well, there’s definitely some overhead:</p><ol><li><strong>Memory Cost</strong>: Each object needs extra space for vptr (usually 8 bytes on 64-bit systems)</li><li><strong>Indirection Cost</strong>: Calling a virtual function involves:</li></ol><ul><li>Load the vptr from the object</li><li>Load the function address from vtable</li><li>Call the function</li></ul><p>This is basically two extra memory accesses compared to a regular function call!</p><p><strong>3. Cache Misses</strong>: The vtable might not be in CPU cache, causing additional delays</p><p><strong>4. Inlining Prevention</strong>: Virtual functions generally can’t be inlined (compiler doesn’t know which function will be called)</p><p>But hold on! Before you go removing all virtual functions from your code, remember- this overhead is usually negligible compared to the benefits of polymorphism. Unless you’re writing a game engine or high-frequency trading system, you probably won’t notice the difference.</p><h4>Can We See the Vtable in Action?</h4><p>If you’re really curious (and a bit adventurous), you can actually peek at the vtable using some pointer tricks:</p><pre>class Test {<br>public:<br>    virtual void func1() { cout &lt;&lt; &quot;func1&quot; &lt;&lt; endl; }<br>    virtual void func2() { cout &lt;&lt; &quot;func2&quot; &lt;&lt; endl; }<br>};<br><br>int main() {<br>    Test t;<br>    <br>    // Warning: This is implementation-specific and not portable!<br>    void** vtable = *(void***)&amp;t;<br>    <br>    cout &lt;&lt; &quot;Vtable address: &quot; &lt;&lt; vtable &lt;&lt; endl;<br>    cout &lt;&lt; &quot;func1 address: &quot; &lt;&lt; vtable[0] &lt;&lt; endl;<br>    cout &lt;&lt; &quot;func2 address: &quot; &lt;&lt; vtable[1] &lt;&lt; endl;<br>    <br>    return 0;<br>}</pre><h4>The Bottom Line</h4><p>So now you know the secret behind C++’s runtime polymorphism! Vtables are basically the compiler’s clever way of implementing dynamic dispatch. Yes, there’s a cost- a bit of extra memory and indirection, but in return, we get the power of polymorphism.</p><p>Think about it- without vtables, we’d have to write tons of if-else statements or switch cases to achieve the same behavior. Isn’t a small performance cost worth the elegant code we can write with virtual functions?</p><p><strong>Remember:<em> “premature optimization is the root of all evil”</em></strong>. Use virtual functions when they make your design cleaner and more maintainable. Only optimize when you’ve measured and found an actual performance problem!</p><p><em>Reference — </em><a href="https://isocpp.org/"><em>ISO C++ Standard</em></a><em> and Others</em></p><blockquote><strong><em>Hey, did you like the explanation?</em></strong></blockquote><blockquote><em>If yes then don’t forget to follow </em><a href="https://exploreit-askdoubt.medium.com/about"><strong><em>exploreIT </em></strong></a><em>as I regularly come up with interesting topics related to programming. And of course, </em><strong>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</strong></blockquote><blockquote><em>Feel free to connect — </em><a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/"><em>https://www.linkedin.com/in/salman-shaikh-82989b1b9/</em></a></blockquote><blockquote><strong>See you in the next article, Bye Bye!</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=698be2229eec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What happens on a function call: Understanding Call Stacks, Recursion, and Tail Call Optimization]]></title>
            <link>https://exploreit-askdoubt.medium.com/what-happens-on-a-function-call-understanding-call-stacks-recursion-and-tail-call-optimization-05abb395ceef?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/05abb395ceef</guid>
            <category><![CDATA[stackoverflow]]></category>
            <category><![CDATA[tail-call-optimization]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[function]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Sun, 06 Jul 2025 19:19:05 GMT</pubDate>
            <atom:updated>2025-07-06T19:19:05.959Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever wondered what exactly happens when you call a function? Like, what’s going on behind the scenes? Or maybe you’ve encountered a stack overflow error and thought “Why the heck did my program crash just because I called a function recursively?” Well, in this article we’re gonna deep dive into the fascinating world of function calls, and trust me, by the end of this you’ll have a much clearer picture of what’s really happening under the hood.</p><p>But wait, before we jump into the technical stuff, let me ask you this — do you think calling a function is just a simple jump to another piece of code? If yes, then you’re in for a surprise!</p><h3>The Journey of a Function Call</h3><p>When you write something like foo(10, 20) in your code, it&#39;s not just magically executing the function. There&#39;s a whole process that happens, and it&#39;s pretty interesting actually.</p><p>Let me break this down for you in simple terms:</p><p><strong>Step 1: Creating the Stack Frame -</strong> When a function is called, the system creates what we call a “stack frame” (or activation record). Think of it like a little box that contains all the information needed for that particular function call.</p><p><strong>Step 2: Saving the Current State -</strong> The program needs to remember where it was before calling the function, so it can return back to that exact point later. This is called the “return address.”</p><p><strong>Step 3: Parameter Passing -</strong> All the arguments you pass to the function are copied into the stack frame.</p><p><strong>Step 4: Local Variables Allocation</strong> — Space is allocated for all the local variables that the function will use.</p><p>Here’s a simple diagram to visualize this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*2creD9BUDJ2YYuCAoaGf3g.png" /><figcaption>fig-1: stack frame design</figcaption></figure><p>Let’s see this in action with a simple example:</p><pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int add(int a, int b) {<br>    int result = a + b;<br>    cout &lt;&lt; &quot;Inside add function: &quot; &lt;&lt; result &lt;&lt; endl;<br>    return result;<br>}<br><br>int main() {<br>    int x = 5, y = 10;<br>    cout &lt;&lt; &quot;Before calling add&quot; &lt;&lt; endl;<br>    int sum = add(x, y);<br>    cout &lt;&lt; &quot;After calling add: &quot; &lt;&lt; sum &lt;&lt; endl;<br>    return 0;<br>}</pre><p>When add(x, y) is called:</p><ol><li>A new stack frame is created</li><li>Values of x and y are copied to parameters a and b</li><li>Space is allocated for local variable result</li><li>The function executes</li><li>Return value is passed back</li><li>Stack frame is destroyed</li></ol><p>Pretty neat, right?</p><h3>The Call Stack: Your Program’s Memory Manager</h3><p>Now, here’s where it gets really interesting. All these stack frames are managed by something called the “call stack.” It’s basically like a stack of plates — you can only add or remove plates from the top (Last In, First Out — LIFO).</p><p>Let me show you what happens when we have multiple function calls:</p><pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>void funcC() {<br>    cout &lt;&lt; &quot;Inside funcC&quot; &lt;&lt; endl;<br>}<br><br>void funcB() {<br>    cout &lt;&lt; &quot;Inside funcB, calling funcC&quot; &lt;&lt; endl;<br>    funcC();<br>    cout &lt;&lt; &quot;Back in funcB&quot; &lt;&lt; endl;<br>}<br><br>void funcA() {<br>    cout &lt;&lt; &quot;Inside funcA, calling funcB&quot; &lt;&lt; endl;<br>    funcB();<br>    cout &lt;&lt; &quot;Back in funcA&quot; &lt;&lt; endl;<br>}<br><br>int main() {<br>    cout &lt;&lt; &quot;In main, calling funcA&quot; &lt;&lt; endl;<br>    funcA();<br>    cout &lt;&lt; &quot;Back in main&quot; &lt;&lt; endl;<br>    return 0;<br>}</pre><p>The call stack would look like this at different points:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*k565Edny3kAap95wJNzx3Q.png" /><figcaption>fig-2: call stack execution diagram</figcaption></figure><p>As functions return, they get popped off the stack. This is why you can trace back through your program execution — each function returns to exactly where it was called from.</p><h3>Recursion: When Functions Call Themselves</h3><p>Now comes the fun part — recursion! This is where a function calls itself, and it’s where the call stack really shows its importance.</p><p>Let’s take the classic factorial example:</p><pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int factorial(int n) {<br>    cout &lt;&lt; &quot;Computing factorial of &quot; &lt;&lt; n &lt;&lt; endl;<br>    <br>    if (n &lt;= 1) {<br>        cout &lt;&lt; &quot;Base case reached!&quot; &lt;&lt; endl;<br>        return 1;<br>    }<br>    <br>    int result = n * factorial(n - 1);<br>    cout &lt;&lt; &quot;Returning &quot; &lt;&lt; result &lt;&lt; &quot; for factorial(&quot; &lt;&lt; n &lt;&lt; &quot;)&quot; &lt;&lt; endl;<br>    return result;<br>}<br><br>int main() {<br>    int result = factorial(5);<br>    cout &lt;&lt; &quot;Final result: &quot; &lt;&lt; result &lt;&lt; endl;<br>    return 0;<br>}</pre><p>When factorial(5) is called, here&#39;s what the call stack looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*-_bL5peogknjO1jPwqp5BQ.png" /><figcaption>fig-3: recursion stack diagram</figcaption></figure><p>Each recursive call creates a new stack frame, and they all stay on the stack until the base case is reached and the functions start returning.</p><p>But here’s the catch — what happens if you have too many recursive calls?</p><h3>Stack Overflow: When Things Go Wrong</h3><p>You’ve probably heard of “stack overflow” (no, not the website!). This happens when the call stack grows too large and runs out of memory. Each stack frame takes up space, and if you have too many recursive calls, you’ll eventually hit the limit.</p><p>Try this (but be careful — it might crash your program):</p><pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>void infiniteRecursion(int depth) {<br>    cout &lt;&lt; &quot;Depth: &quot; &lt;&lt; depth &lt;&lt; endl;<br>    infiniteRecursion(depth + 1);  // This will eventually crash!<br>}<br><br>int main() {<br>    infiniteRecursion(0);<br>    return 0;<br>}</pre><p>This will keep creating new stack frames until the system runs out of stack memory, resulting in a stack overflow error.</p><h3>Tail Call Optimization: The Smart Solution</h3><p>Now here’s where it gets really clever. Some compilers can optimize a special type of recursive call called “tail recursion.”</p><p>What is tail recursion? It’s when the recursive call is the very last thing the function does — no other operations happen after the recursive call returns.</p><p>Let’s rewrite our factorial function using tail recursion:</p><pre>#include &lt;iostream&gt;<br>using namespace std;<br><br>int factorialTailRecursive(int n, int accumulator = 1) {<br>    cout &lt;&lt; &quot;Computing factorial of &quot; &lt;&lt; n &lt;&lt; &quot; with accumulator &quot; &lt;&lt; accumulator &lt;&lt; endl;<br>    <br>    if (n &lt;= 1) {<br>        cout &lt;&lt; &quot;Base case reached! Returning &quot; &lt;&lt; accumulator &lt;&lt; endl;<br>        return accumulator;<br>    }<br>    <br>    // This is tail recursion - the recursive call is the last operation<br>    return factorialTailRecursive(n - 1, n * accumulator);<br>}<br><br>int main() {<br>    int result = factorialTailRecursive(5);<br>    cout &lt;&lt; &quot;Final result: &quot; &lt;&lt; result &lt;&lt; endl;<br>    return 0;<br>}</pre><p>The magical thing about tail call optimization is that the compiler can recognize this pattern and instead of creating a new stack frame, it can reuse the current one! This is because when the recursive call returns, there’s nothing more to do in the current function.</p><p>Here’s what happens <strong>with and without tail call optimization</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*osoCxD3If7O9eKiAHlCWfw.png" /><figcaption>fig-4: call stack without tail call optimization</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/1*2IvMiBzRcE1ERWSnhAeNIQ.png" /><figcaption>fig-5: call stack with tail call optimization</figcaption></figure><h3>When Should You Care About This?</h3><p>You might think “This is all cool, but when will I actually use this knowledge?” Well, here are some practical scenarios:</p><ol><li>Debugging stack overflow errors: Now you know why they happen and how to fix them</li><li>Writing efficient recursive algorithms: Understanding tail recursion can help you write better code</li><li>Performance optimization: Knowing how function calls work can help you make better design decisions</li><li>Interview questions: This stuff comes up a lot in technical interviews!</li></ol><p>Understanding these concepts will make you a better programmer and help you debug those tricky stack-related issues. Plus, it’s just fascinating to know what’s happening under the hood when you write that simple function call!</p><p><strong><em>Next time you write a recursive function, think about whether you can make it tail-recursive. Your stack (and your users) will thank you for it! 😉</em></strong></p><blockquote><strong>Hey, did you like the explanation?</strong></blockquote><blockquote>If yes then don’t forget to follow <a href="https://exploreit-askdoubt.medium.com/about"><strong>exploreIT </strong></a>as I regularly come up with interesting topics related to programming. And of course, <strong><em>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</em></strong></blockquote><blockquote>Feet free to connect — <a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/">https://www.linkedin.com/in/salman-shaikh-82989b1b9/</a></blockquote><blockquote><strong><em>See you in the next article, Bye Bye!</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=05abb395ceef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tracing a Web Request: From Your Code to the Kernel and Back]]></title>
            <link>https://exploreit-askdoubt.medium.com/tracing-a-web-request-from-your-code-to-the-kernel-and-back-8bc93ffa512f?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/8bc93ffa512f</guid>
            <category><![CDATA[https]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[networking]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Sun, 04 May 2025 20:40:52 GMT</pubDate>
            <atom:updated>2025-08-26T19:10:28.660Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever clicked a button on a web app and wondered what actually happens behind the scenes?</p><p>I mean, we all build or use web applications, and sometimes things feel slow. But where exactly does that slowness come from? The server? The network? Our own code? Or maybe the browser?</p><p>Well, let’s walk through what really happens when we make a web request — from the moment a button is clicked in the browser, all the way down to the operating system’s kernel and back. Don’t worry, this is gonna be a conceptual and beginner-friendly explanation. No hands-on development — just the story behind the scenes. Umm… the diagram may look scary, but wait, this is all gonna be clear in a while!!</p><h4><em>Step 1: The Button Click That Starts It All</em></h4><p>You’re on a website. Maybe filling out a form. You hit the “Submit” button.</p><p>Boom — that’s when the magic starts.</p><p>Your browser takes this action and builds an HTTP request. Think of it as a nicely packed envelope: it has the address (the server’s URL), method (like GET or POST), headers, maybe a body with form data — all neatly bundled.</p><p>But before it even goes out… the browser has a few jobs to do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/755/1*mISpuoTz2Oqiv0HFmzfiFw.png" /><figcaption>Fig-1: What happens when you hit that button?</figcaption></figure><h4>Step 2: Browser: “Huh!! where do I send This!?”</h4><p>If the browser doesn’t already know the IP address of the server (like <em>api.example.com</em>), it asks the DNS — the internet’s phone book — for it.</p><p>Once it gets that, it opens a connection. For secure websites (which is almost all of them today), this means a TLS handshake — a quick “hello” to make sure both sides agree on how to talk securely.</p><p>This whole step happens before a single line of your backend code runs.</p><p>So, if a user is experiencing delays here, your server might be fine — it’s the name resolution or connection setup that’s slow.</p><h4>Step 3: Sending the Request Over the Wire</h4><p>Once everything is ready, the browser sends the HTTP request through the network — hopping through routers, ISPs, maybe even a CDN.</p><p>It finally reaches the server machine that’s supposed to handle it.</p><blockquote><strong>Now here’s something important:</strong> even though you might think of “the server” as your application, the first thing to receive the request is the operating system.</blockquote><p>The <strong>kernel </strong>(that core part of the OS) listens for network traffic, receives the request, and queues it for whichever program is listening on that port — maybe it’s Nginx, or your own web app.</p><p>So, even before your app’s routing logic runs, the OS has done some work: <strong><em>catching the packet, acknowledging it, and passing it to your app</em></strong>.</p><h4>Step 4: Your Web Framework Takes Over</h4><p>Once your app (or web server) gets the request, it passes through the routing layer — where it figures out which controller or handler should deal with it.</p><p>This is where your actual code begins to run. You might be fetching data <em>(from database in our diagram)</em>, running some logic, calling another API, or querying a database.</p><p>Depending on how you’ve structured things, this part can be fast — or a total bottleneck.</p><p>If something feels slow here, it’s usually because:</p><ul><li>A database query is unoptimized</li><li>An external API call is taking too long</li></ul><p>There’s some blocking operation in the middle.</p><blockquote><strong>Note:</strong> if your endpoint runs fine locally but lags in production, the issue might not be in your code but in the environment — like network latency to a cloud DB.</blockquote><h4>Step 5: The Response Goes Back the Way It Came</h4><p>After your app is done processing, it creates an HTTP response — a status code, maybe a JSON or HTML body, and headers — and hands it off.</p><p>From there, the OS kernel takes over again. It breaks it into packets, hands it to the network interface, and sends it back through the routers, ISPs, and the internet jungle — eventually reaching the browser.</p><p>The browser reads the response, and (if it’s HTML) starts rendering the page. <strong>It might also fire off more requests — like for CSS, images, or JS — restarting this whole cycle.</strong></p><h4>Why This Journey Matters?</h4><p>You might be like — well, this is a whole lot of information. Why should we even bother about all of these!? Okay, here’s why tracing this journey matters —</p><ul><li>Sometimes we blame our backend when things are slow — but the problem could be in <strong>DNS resolution</strong>, <strong>TLS handshake</strong>, <strong>slow routers</strong>, or even the <strong>user’s own network</strong>.</li><li>Other times, our backend is the bottleneck — maybe we’re doing too much work in a single request, or not caching enough.</li></ul><p>When you understand each layer — <strong>from browser to network to OS to your app and back </strong>— it becomes much easier to debug performance issues or weird behavior, isn’t it?</p><blockquote><strong>Hey, did you like the explanation?</strong></blockquote><blockquote><em>If yes then don’t forget to follow </em><a href="https://exploreit-askdoubt.medium.com/about"><strong><em>exploreIT </em></strong></a><em>as I regularly come up (oops, this one came after 2 years, but it will now keep coming) with interesting topics related to programming. And of course, </em><strong>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</strong></blockquote><blockquote>Feel free to connect — <a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/">https://www.linkedin.com/in/salman-shaikh-82989b1b9/</a></blockquote><blockquote><strong>See you in the next article, Bye Bye!</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8bc93ffa512f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why Can’t We Overload Functions/Methods Based On Their Return Type (in C++/Java)?]]></title>
            <link>https://exploreit-askdoubt.medium.com/why-cant-we-overload-functions-methods-based-on-their-return-type-in-c-java-f3a79996cc83?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3a79996cc83</guid>
            <category><![CDATA[function-overloading]]></category>
            <category><![CDATA[polymorphism-in-cpp]]></category>
            <category><![CDATA[programming-facts]]></category>
            <category><![CDATA[method-overloading]]></category>
            <category><![CDATA[compile-time-polymorphism]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Mon, 07 Nov 2022 14:20:36 GMT</pubDate>
            <atom:updated>2023-06-17T12:04:10.859Z</atom:updated>
            <content:encoded><![CDATA[<p>I won’t go deeper into ‘What Function Overloading actually is’, but just a short definition for our discussion purpose-</p><blockquote><a href="https://www.geeksforgeeks.org/function-overloading-c/"><strong><em>What is Function Overloading?</em></strong></a> Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.</blockquote><p>So, as we can see it’s just a feature by which we can use a same function name (with different signatures) for accomplishing different tasks. But the point that we need to keep in our mind is- It’s a <a href="https://www.geeksforgeeks.org/cpp-polymorphism/">Compile-Time Polymorphism</a>, that means during the compilation of our code it is decided which one to call among all other overloaded versions. So, the compiler is not aware of the function body and what is actually going to be returned from the function (after the execution), because the return type that we have mentioned (in the function prototype) that may or may not be the proper one, as there can be some kind of implicit typecasting that may happen inside the function body, so it’d be difficult for a compiler to determine which function to call based on the return type only. So, it would create ambiguity in runtime (because, our program is indeed allowed to perform possible typecasting), that means we can’t be strict on the return type. So, isn’t it better to restrict doing function overloading based on return type and show an error message in Compile Time rather than Runtime error?</p><p><strong><em>Here’s what compiler shows when we try to do this-</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VkEhtJ7KTEBkadPGdIJPvQ.png" /><figcaption>Fig-1: Error message from compiler</figcaption></figure><p>You may think, the 1st one should call the first version and the 2nd one should call the 2nd version as the variables in which I’m storing the returned values they are of type ‘int’ and ‘float’ respectively. But, isn’t a=10 is acceptable in both ‘int’ and ‘float’ data-types? So, why create ambiguity?</p><blockquote><strong>Hey, did you like the explanation?</strong></blockquote><blockquote><em>If yes then don’t forget to follow </em><a href="https://exploreit-askdoubt.medium.com/about"><strong>exploreIT </strong></a><em>as I regularly come up with interesting topics related to programming. And of course, </em><strong>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</strong></blockquote><blockquote>Feet free to connect — <a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/">https://www.linkedin.com/in/salman-shaikh-82989b1b9/</a></blockquote><blockquote><strong>See you in the next article, Bye Bye!</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3a79996cc83" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can I declare a C++ Constructor in Private? What is Singleton class?]]></title>
            <link>https://exploreit-askdoubt.medium.com/can-i-declare-a-c-constructor-in-private-what-is-singleton-class-9421b8b8d715?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/9421b8b8d715</guid>
            <category><![CDATA[constructor]]></category>
            <category><![CDATA[public-constructors]]></category>
            <category><![CDATA[singleton-class]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[singleton-design-pattern]]></category>
            <category><![CDATA[private-constructor]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Tue, 26 Jul 2022 15:27:00 GMT</pubDate>
            <atom:updated>2023-06-17T12:07:59.105Z</atom:updated>
            <content:encoded><![CDATA[<p>Yeah, for sure! Not all situations require or demand public constructor. You can use it in both public and private as long as you know when and how to use it. So, let’s try to understand the ‘<strong>when</strong>’ and ‘<strong>how</strong>’-</p><p>The reason we use constructor is- to construct an object and assign values to the object’s members. More formally, it is used to create an object of a class with <strong>proper initial state</strong>, and in order to create the object of the class anywhere in the code (in any class, any package) we generally make it <strong>public</strong>. So, by the simple logic you can make a constructor private if you don’t want a class’s objects to be used anywhere in the code (just the opposite one). But the question arises- <strong><em>If we can’t use it, why should we declare then?</em></strong></p><p>Ahh, not actually, you make a constructor private that doesn’t mean it can’t be used. Moreover, there are certain cases when we actually want our constructor to be private. Now, there are a few ways you can declare constructor in private and access them (like- using friend class), but here I’m gonna show you a practical example when we actually require <strong>private</strong> constructors, which is <strong>Singleton Class</strong>.</p><h3>What is Singleton class?</h3><p>It is basically a <strong>design pattern</strong>. Sometimes we need to have only one instance of our class. For example, a single DB connection shared by multiple objects, as creating a separate DB connection for every object may be costly. Or, if you are using a logger, that writes logs to a file, you can use a singleton class to create such a logger. Here’s a simple example of a Singleton design pattern (we’ll later discuss how the private constructor is helping us in this scenario)-</p><p><strong>PROGRAM:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CeE4ag9cVf0bOwYg6pWa6g.png" /><figcaption>Fig-1: A very basic example of Singleton class in C++</figcaption></figure><p><strong>OUTPUT: </strong><em>(I have attached the code in ‘</em><strong><em>comment section’</em></strong><em>, in case you want to execute it)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzJ7ASI0ucFyepdi_H1grg.png" /><figcaption>Fig-2: Output of the above program</figcaption></figure><p><strong>EXPLANATION:</strong> If you observe, you can see- we are not creating any object by directly calling the class’s constructor (we can’t do that BTW, hence it is in private), what we are doing is- just calling another member function for creating instances, and the interesting thing is- the function internally checks whether any object of the class is already instantiated or not, if it is already there then it returns that one (doesn’t create a new one). Hence we were tried to create a new object twice (by calling ‘<strong><em>getSingleton()</em></strong>’) we get a message saying <em>“One instance is already present”</em>, and also when we print values in both the objects (created by ourselves) we get the same value (Even if one was set to ‘100’ and another ‘999’. This is how a single copy is maintained here throughout the program. (This is a very basic example, but the idea is used in a lot of scenarios)</p><blockquote><strong>Hey, did you like the article?</strong></blockquote><blockquote><em>If yes then don’t forget to follow </em><a href="https://exploreit-askdoubt.medium.com/about"><strong>exploreIT </strong></a><em>as I regularly come up with interesting topics related to programming. And of course, </em><strong>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</strong></blockquote><blockquote>Feet free to connect — <a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/">https://www.linkedin.com/in/salman-shaikh-82989b1b9/</a></blockquote><blockquote><strong>See you in the next article, Bye Bye!</strong></blockquote><p><strong><em>Related Article(s)</em></strong></p><p><a href="https://exploreit-askdoubt.medium.com/why-does-c-c-allow-undefined-behavior-9c8529da54f1">Why does C/C++ allow Undefined Behavior?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9421b8b8d715" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When and Why should we calculate Big-O?]]></title>
            <link>https://exploreit-askdoubt.medium.com/when-and-why-should-we-calculate-big-o-3c42fcd80f93?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c42fcd80f93</guid>
            <category><![CDATA[why-big-o]]></category>
            <category><![CDATA[when-big-o]]></category>
            <category><![CDATA[asymptotic-notation]]></category>
            <category><![CDATA[big-o]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Sat, 11 Dec 2021 21:13:44 GMT</pubDate>
            <atom:updated>2023-06-17T12:09:12.811Z</atom:updated>
            <content:encoded><![CDATA[<p>Yeah! That’s an interesting question. If you are a programmer, you must have a clear understanding about this notation.</p><p><strong><em>Wait! A short NOTE:</em></strong><em> Once you read this article completely and understand this concept properly, I’ll recommend you to visit </em><a href="https://beginnersbook.com/2018/10/ds-asymptotic-notation/"><strong><em>this </em></strong></a><em>or any other site for knowing about other different types of </em><a href="https://beginnersbook.com/2018/10/ds-asymptotic-notation/"><strong><em>Asymptotic Notations</em></strong></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oKp6uQxD3pgh-oqr" /></figure><p>Okay, let’s try to answer your question. But before that let me ask you- <strong><em>Do you know what Big-O actually is?</em></strong></p><p>Don’t worry if you don’t, I’m going to provide a very simple definition here.</p><blockquote><strong>Big-O: </strong>In computer science, Big-O represents the efficiency or performance of an algorithm. Big-O makes it easy to compare algorithm speeds and gives you a general idea of how long it will take the algorithm to run.</blockquote><p>Not clear?</p><p>Well, what would be your answer if I ask-<strong><em> What is a good code?</em></strong></p><p>If you don’t know- there are 2 main things by which you can judge whether a code is good or not, and these are —<strong> Readability </strong>and <strong>Scalability</strong>. <em>(If you want to know these two in depth, please </em><a href="https://exploreit-askdoubt.medium.com/what-does-it-mean-by-scalable-code-359510199aeb"><strong><em>Visit!</em></strong></a><em>)</em></p><p><strong>Scalability </strong>is something where comes the <strong>Big-O</strong>. When we talk about Big-O and scalability of code, we simply mean when we grow bigger and bigger with our input, how much does the algorithm or function slows down. The less it slows down or the slower it slows down, the better it is. keep this in mind, <em>Big-O doesn’t measure things in seconds. Instead, it focuses on how quickly the runtime grows.</em></p><p><strong>So, is this the reason why we calculate Big-O?</strong></p><p>Yes! We want to measure the efficiency of our code in terms of <strong>Time </strong>and <strong>Space Complexity</strong> as these are some of the resources that have a very big importance in our lives.</p><p><strong><em>NOTE:</em></strong><em> In </em><strong><em>Big-O</em></strong><em>, we are always worried about the worst case that our code can have. And yes, when inputs are small the </em><strong><em>Big-O</em></strong><em> notation doesn’t matter that much.</em><strong><em> </em></strong><em>Scalable code means thinking outside of just the small, so that when things grow, we don’t have to fix things again and again.</em></p><p>Now, if you ask- <strong>When should we calculate Big-O?</strong></p><p>Off course, when you want to measure your code’s efficiency. It may be because of you have more than one working solution and you want to choose the efficient one, or there might be some other reason as well, but these are the very basic idea about why we calculate Big-O.</p><blockquote><strong>Hey, did you like the explanation?</strong></blockquote><blockquote><em>If yes then don’t forget to follow </em><a href="https://exploreit-askdoubt.medium.com/about"><strong>exploreIT </strong></a><em>as I regularly come up with interesting topics related to programming. And of course, </em><strong>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</strong></blockquote><blockquote>Feet free to connect — <a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/">https://www.linkedin.com/in/salman-shaikh-82989b1b9/</a></blockquote><blockquote><strong>See you in the next article, Bye Bye!</strong></blockquote><p><strong><em>Related Article(s)</em></strong></p><p><a href="https://exploreit-askdoubt.medium.com/o-n-vs-o-2n-are-they-same-if-yes-then-how-c473ede74440">O(N) vs O(2N): Are They Same? If yes then How?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c42fcd80f93" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[O(N) vs O(2N): Are They Same? If yes then How?]]></title>
            <link>https://exploreit-askdoubt.medium.com/o-n-vs-o-2n-are-they-same-if-yes-then-how-c473ede74440?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/c473ede74440</guid>
            <category><![CDATA[asymptotic-notation]]></category>
            <category><![CDATA[big-o]]></category>
            <category><![CDATA[interesting-facts]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Wed, 18 Aug 2021 12:51:18 GMT</pubDate>
            <atom:updated>2023-06-17T12:11:29.750Z</atom:updated>
            <content:encoded><![CDATA[<p>Umm… Yes! Both are same.</p><p>If you are getting confused with O(N) and O(2N) and thinking they are different then it is because while calculating <a href="https://runestone.academy/runestone/books/published/pythonds/AlgorithmAnalysis/BigONotation.html"><strong><em>Big-O</em></strong></a> you are focusing on <strong><em>‘Number Of Operations’</em></strong><em> </em>that a function have instead of <strong><em>‘how quickly the runtime grows depending on the size of the input’</em></strong>.</p><p>Confused? Well, let me explain it!</p><p>When we talk about Big-O and scalability of code, we simply mean- <em>when we grow bigger and bigger with our input, how much does the algorithm or function slows down. The less it slows down or the slower it slows down, the better it is.</em></p><p>Let’s have a look on the below diagrams-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*F6-7OPa4D7vR6vVCnX2JUw.png" /><figcaption><em>Fig-1: Graphical Representation Of O(N)</em></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*jFnz-7XI4ZRfBk_50gf2fQ.png" /><figcaption>Fig-2: Graphical Representation Of O(2N)</figcaption></figure><p>You can see in both the cases our <em>‘No Of Operations’</em> is increasing linearly depending on the <em>‘Input Size’</em>. <em>But yes, practically O(N) will definitely have a shorter running time than O(2N)</em>, but not significant. When N value increases O(N) will dominate the growth and coefficient 2 becomes insignificant. So eventually when our N will be very large, the running time of both will be the identical.</p><p><strong><em>Let’s take an example:</em></strong></p><p>T(N) = 4N² + 3N + 9999</p><p>For n= 1 or 2, the constant 9999 seems to be the dominant part of the function but for larger values of N, we can ignore the constants and 3N and can just concentrate on 4N² as it will contribute more to the growth, If the N value still increases the coefficient 4 also seems insignificant and we can say complexity is O(N²).</p><p>Now you may think <em>‘</em><strong><em>why will I take the larger values?</em></strong><em>’</em></p><p>Well, that is because when inputs are small the Big-O notations don’t matter that much and if we come to the Scalability of a code we have to think outside of just the small, so that when things grow we don’t have to fix things again and again.</p><p>So, if you compare O(N) and O(2N) in terms of <em>asymptotic </em>complexity then they are equal, because they represent the same growth rate <em>(what we know as ‘</em><strong><em>linear</em></strong><em>’).</em></p><p><strong>Reference </strong>—<a href="https://stackoverflow.com/questions/25777714/which-algorithm-is-faster-on-or-o2n"> StackOveflow</a> and Others</p><blockquote><strong>Hey, did you like the explanation?</strong></blockquote><blockquote><em>If yes then don’t forget to follow </em><a href="https://exploreit-askdoubt.medium.com/about"><strong>exploreIT </strong></a><em>as I regularly come up with interesting topics related to programming. And of course, </em><strong>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</strong></blockquote><blockquote>Feet free to connect — <a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/">https://www.linkedin.com/in/salman-shaikh-82989b1b9/</a></blockquote><blockquote><strong>See you in the next article, Bye Bye!</strong></blockquote><p><strong><em>Related Article(s)</em></strong></p><p><a href="https://exploreit-askdoubt.medium.com/when-and-why-should-we-calculate-big-o-3c42fcd80f93">When and Why should we calculate Big-O?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c473ede74440" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why does CHAR reserve 16 bits (2 bytes) in Java!?]]></title>
            <link>https://exploreit-askdoubt.medium.com/why-does-char-reserve-16-bits-in-java-148c9d79b442?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/148c9d79b442</guid>
            <category><![CDATA[cool-programming-facts]]></category>
            <category><![CDATA[char-in-java]]></category>
            <category><![CDATA[char-data-type]]></category>
            <category><![CDATA[ascii-code-vs-unicode]]></category>
            <category><![CDATA[char-in-cpp]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Thu, 12 Aug 2021 18:48:04 GMT</pubDate>
            <atom:updated>2023-06-17T12:13:05.146Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*9ucILE2ta8LwCPkKBpx_BA.jpeg" /><figcaption>Why does CHAR reserve 16 bits in Java!!</figcaption></figure><p>If you are from C/C++ background and currently working with Java you must be familiar with <a href="https://www.geeksforgeeks.org/c-data-types/"><strong><em>C/C++ Data Type</em></strong></a><strong><em>s</em></strong> and you must know the size of ‘char’ datatype.</p><p>Yes you are right! it reserves only 1 byte in C/C++ , but when you look at <a href="https://www.geeksforgeeks.org/data-types-in-java/"><strong><em>Java Data Types</em></strong></a> you must be wondered seeing it has ‘char’ datatype that reserves 2 bytes (or 16 bits). <strong><em>BUT WHY? What makes the difference?</em></strong></p><p>Hmm… the same question also came in my mind when I first observed this.</p><p>Yeah definitely! I’ll share the concept with you guys also. Well, before that we should know why C/C++ ‘char’ datatype takes 8 bits(or 1 byte) only-</p><p>The simple answer is C/C++ supports <a href="https://en.wikipedia.org/wiki/ASCII"><strong><em>ASCII Code</em></strong></a>, and the reason Java ‘char’ takes 2 bytes is- it supports <a href="https://en.wikipedia.org/wiki/Unicode"><strong><em>Unicode</em></strong></a>.</p><p>Hey hey! what you just said? Can you explain a little bit deeper?</p><p>Sure, here it is- You see I mentioned two terms ‘ASCII’ and ‘Unicode’, these are basically two different character encoding standards where in ASCII the character set ranges from <strong><em>0–255</em></strong> and on the other hand Unicode have a much wider range where the character set varies from <strong><em>0–65535</em></strong>. Back in the days there were a lot of such standards, like- <a href="https://en.wikipedia.org/wiki/ISO/IEC_8859-5"><strong><em>ISO/IEC 8859–5</em></strong></a>, <a href="https://en.wikipedia.org/wiki/Extended_ASCII"><strong><em>Extended ASCII</em></strong></a>, <a href="https://en.wikipedia.org/wiki/Code_page"><strong><em>Code page</em></strong></a>, <a href="https://en.wikipedia.org/wiki/Multinational_Character_Set"><strong><em>Multinational Character Set</em></strong></a> and so on. But having such different standards caused some problems, such as-</p><blockquote><em>a) A particular code value corresponds to different letters in the various language standards.</em></blockquote><blockquote><em>b) The encoding for languages with large character sets have variable length. Some common characters are encoded as single bytes, other require two or more byte.</em></blockquote><p>Now here comes the Unicode that is considered as the <a href="https://en.wikipedia.org/wiki/Universal_Coded_Character_Set#:~:text=The%20Universal%20Coded%20Character%20Set%20%28UCS%2C%20Unicode%29%20is,characters%20from%20previously%20unrepresented%20writing%20systems%20are%20added."><strong><em>universal character encoding standard</em></strong></a> which is able to handle a very big number of characters (from 0–65535). Hence Java supports Unicode standard that’s why its ‘char’ datatype takes 2 bytes or 16 bits so that it fit in the range 0–65535 and on the other hand C/C++ supports ASCII, so now you know why its ‘char’ takes 1 byte only.</p><p><strong>Reference </strong>—<a href="https://www.quora.com/Why-is-the-size-of-char-in-Java-2-bytes"> Quora</a> and Others</p><blockquote><strong>Hey, did you like the explanation?</strong></blockquote><blockquote><em>If yes then don’t forget to follow </em><a href="https://exploreit-askdoubt.medium.com/about"><strong>exploreIT </strong></a><em>as I regularly come up with interesting topics related to programming. And of course, </em><strong>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</strong></blockquote><blockquote>Feet free to connect — <a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/">https://www.linkedin.com/in/salman-shaikh-82989b1b9/</a></blockquote><blockquote><strong>See you in the next article, Bye Bye!</strong></blockquote><p><strong><em>Related Article(s)</em></strong></p><p><a href="https://exploreit-askdoubt.medium.com/does-delete-really-deletes-the-specified-memory-block-95cab3829603">Does ‘delete’ really delete the specified memory block?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=148c9d79b442" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Does ‘delete’ really delete the specified memory block?]]></title>
            <link>https://exploreit-askdoubt.medium.com/does-delete-really-deletes-the-specified-memory-block-95cab3829603?source=rss-4961b3a701------2</link>
            <guid isPermaLink="false">https://medium.com/p/95cab3829603</guid>
            <category><![CDATA[exporeit]]></category>
            <category><![CDATA[deleting-data-in-cpp]]></category>
            <category><![CDATA[cool-facts-in-programming]]></category>
            <category><![CDATA[delete-operator]]></category>
            <category><![CDATA[undefined-behavior]]></category>
            <dc:creator><![CDATA[exploreIT]]></dc:creator>
            <pubDate>Wed, 11 Aug 2021 18:50:16 GMT</pubDate>
            <atom:updated>2023-06-17T12:16:55.088Z</atom:updated>
            <content:encoded><![CDATA[<p>The answer is NO! <strong><em>‘delete’ doesn’t delete anything</em></strong>.</p><p>Hmm… you may think HOWWW…!</p><p>This is basically one of the <a href="https://exploreit-askdoubt.medium.com/why-does-c-c-allow-undefined-behavior-9c8529da54f1"><strong><em>Undefined Behavior</em></strong></a> of C++ (Well, if you don’t know what is undefined behavior and why it is allowed in some of the programming languages please go <a href="https://exploreit-askdoubt.medium.com/why-does-c-c-allow-undefined-behavior-9c8529da54f1"><strong><em>here</em></strong></a>).</p><p>Before moving into the explanation let me tell you what ‘delete’ is?</p><blockquote><a href="https://en.wikipedia.org/wiki/New_and_delete_(C%2B%2B)"><strong><em>delete</em></strong></a><strong><em>:</em></strong> <strong>delete </strong>is an <strong><em>operator</em> </strong>that is used to destroy array and non-array(pointer) objects which are created by <a href="https://en.wikipedia.org/wiki/New_and_delete_(C%2B%2B)"><strong>new </strong></a>expression.</blockquote><p>Well, thanks for the definition but we want the answer of the above ‘HOWWW…!’</p><p>Hold on, hold on! I’m gonna explain that :)</p><p>Try to execute the below code and see what results are coming-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/1*hu1oZDmXCQkMGHcpwfModQ.png" /><figcaption>We are still able to access a data even after deleting it using ‘delete’</figcaption></figure><blockquote>Output:</blockquote><blockquote>100</blockquote><blockquote>100</blockquote><p>Oh no! we got the previous data which we deleted (Note that, it’s an <a href="https://exploreit-askdoubt.medium.com/why-does-c-c-allow-undefined-behavior-9c8529da54f1"><strong><em>undefined behavior</em></strong></a>, you may not get the same output as it is compiler dependent. Check <a href="https://exploreit-askdoubt.medium.com/why-does-c-c-allow-undefined-behavior-9c8529da54f1"><strong><em>this </em></strong></a>for more info!)</p><p>Basically we say “<strong><em>delete </em></strong><em>is used to </em><strong><em>destroy array</em></strong><em> and </em><strong><em>non-array</em></strong><em>(pointer) </em><strong><em>objects</em></strong><em> which are created by </em><strong><em>new </em></strong><em>expression”</em>, but how the destruction is done?</p><p>Let us have a deep dive into the destruction process-</p><p>When we create a memory block using ‘<strong>new’ </strong>keyword the specified block is allocated in the <strong><em>heap memory</em></strong>. Now if we call <strong><em>delete </em></strong>what happens is that- the memory is marked as available, so the next time someone does <strong><em>new</em></strong>, the memory may be used. <strong><em>But it is not deleted.</em></strong> You see I said “<em>delete doesn’t delete anything”</em>, so what it does is, it just marks the memory as <strong><em>“being free for reuse”</em></strong>. Until some other allocation call reserves and fills that space it will have the old data. <em>However, you should not rely on that data, basically if you delete something should just forget about it!</em></p><p>Yessss…! this is the reason you were getting that output while you were running the code. But as I mentioned this is one the <a href="https://exploreit-askdoubt.medium.com/why-does-c-c-allow-undefined-behavior-9c8529da54f1"><strong><em>Undefined Behaviors in C++</em></strong></a> you may or may not be able to access the data and of course you should not rely on that data which is said to be freed.</p><p><strong>If you think about it, it’s logical also</strong>- when you tell the compiler that you are no longer interested in the memory (using <em>delete</em>), why should the computer spend time on zeroing it? Isn’t it?</p><p><strong><em>One of the practices in this regard that is often encountered in libraries is a Delete function:</em></strong></p><blockquote>template&lt; class T &gt; void Delete( T*&amp; pointer )</blockquote><blockquote>{</blockquote><blockquote>delete pointer;</blockquote><blockquote>pointer = NULL;</blockquote><blockquote>}</blockquote><p>This prevents us from accidentally accessing invalid memory. Note that it is perfectly okay to call <strong><em>delete NULL</em></strong>;</p><p><strong>Reference </strong>—<a href="https://stackoverflow.com/questions/1930459/c-delete-it-deletes-my-objects-but-i-can-still-access-the-data"> StackOverflow</a> and Others</p><blockquote><strong>Hey, did you like the explanation?</strong></blockquote><blockquote><em>If yes then don’t forget to follow </em><a href="https://exploreit-askdoubt.medium.com/about"><strong>exploreIT </strong></a><em>as I regularly come up with interesting topics related to programming. And of course, </em><strong>if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!</strong></blockquote><blockquote>Feet free to connect — <a href="https://www.linkedin.com/in/salman-shaikh-82989b1b9/">https://www.linkedin.com/in/salman-shaikh-82989b1b9/</a></blockquote><blockquote><strong>See you in the next article, Bye Bye!</strong></blockquote><p><strong><em>Related Article(s)</em></strong></p><p><a href="https://exploreit-askdoubt.medium.com/why-does-c-c-allow-undefined-behavior-9c8529da54f1">Why does C/C++ allow Undefined Behavior?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95cab3829603" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>