<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>https://hamza-cskn.com/</id>
  <title>Hamza Coşkun</title>
  <subtitle>Notes on programming, systems and technology.</subtitle>
  <link href="https://hamza-cskn.com/"/>
  <link rel="self" type="application/atom+xml" href="https://hamza-cskn.com/feed.xml"/>
  <updated>2026-11-16T00:00:00.000Z</updated>
  <author><name>Hamza Coşkun</name></author>
  <entry>
    <id>https://hamza-cskn.com/blog/tls-certificates-gta-6</id>
    <title>TLS Certificates, Explained Through a GTA 6 Leak</title>
    <link href="https://hamza-cskn.com/blog/tls-certificates-gta-6"/>
    <published>2026-11-16T00:00:00.000Z</published>
    <updated>2026-11-16T00:00:00.000Z</updated>
    <summary>How a hacker spelling their name in bullets explains signatures, certificate authorities, and the paranoia that keeps HTTPS alive.</summary>
    <content type="html">&lt;p&gt;&lt;img alt=&quot;Cartoon cat signing a pink contract titled “Slavery Clause, 20 Years” that a hand in a suit holds out to him&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1843&quot; height=&quot;1125&quot; src=&quot;https://hamza-cskn.com/_astro/cover.FX9TvEsw.jpeg&quot;&gt;&lt;/p&gt;
&lt;p&gt;This is not AI content, so there is no gaslighting here.&lt;/p&gt;
&lt;p&gt;In August 2026, X.com was shaken by a leaked video of GTA 6. GTA 6 teaser hadn’t been published yet. However, the spoiler is not the big thing. In the video, the player spelled their name in bullets.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;People hadn’t played GTA 6 before but everyone understood it was GTA 6.&lt;/li&gt;
&lt;li&gt;The hacker proved they can play GTA 6 by doing arbitrary actions in the game.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s abstract this mechanism. You can test whether something is derived from something else. You can say if a video is from GTA 6.&lt;/p&gt;
&lt;p&gt;You cannot create a derivative from something else. You cannot create a video from GTA 6 without playing it.&lt;/p&gt;
&lt;p&gt;So, if you see a person who can create arbitrary derivatives, then you can believe they are the owner. This is the key idea behind certificates.&lt;/p&gt;
&lt;p&gt;To integrate this mechanism into code, we can write some pseudo code like below.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Signature &lt;/span&gt;&lt;span&gt;sign&lt;/span&gt;&lt;span&gt;(Owner owner);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;boolean&lt;/span&gt;&lt;span&gt; verify&lt;/span&gt;&lt;span&gt;(Owner owner, Signature signature);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The sign() function returns a Signature object by taking an owner. Once you have the Signature object, you can ask ‘Is this person the owner of the signature?’ by using the verify() function.&lt;/p&gt;
&lt;p&gt;Assume you have a friend. Your  friend is the only person who have sign() function and everyone have verify() function. To prove somebody is your friend, you can request the signature then verify the signature belongs to your friend.&lt;/p&gt;
&lt;p&gt;However, what happens if someone steal the signature? If your friend gave you this signature, then technically you can share it with everyone and everyone else can too. We have to create specialized signatures for everyone.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Signature &lt;/span&gt;&lt;span&gt;sign&lt;/span&gt;&lt;span&gt;(Owner owner, String payload);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;boolean&lt;/span&gt;&lt;span&gt; verify&lt;/span&gt;&lt;span&gt;(Owner owner, Signature signature, String payload);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, your friend can put an arbitrary payload into the signature. When somebody wants to prove they have a working sign() function, request a signature with your own arbitrary random payload. All you need is to verify this signature is owned by your friend and it is the payload that you made up.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;being-convinced-the-server-is-genuine&quot;&gt;Being convinced the server is genuine&lt;/h2&gt;
&lt;p&gt;We defined imaginary functions and objects to understand the mechanism. Now let’s map them to the real implementation.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;sign() function:&lt;/strong&gt; Signing a payload with the private key.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;verify() function:&lt;/strong&gt; The public key.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Owner objects:&lt;/strong&gt; Abstract identification of entities. (Server in our case.)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Some genius mathematicians introduced mathematical functions to implement our imaginary sign and verify functions. I really respect them. They introduced private key and public key pairs. Under the hood, they are just numbers.&lt;/p&gt;
&lt;p&gt;You can transform any payload using your private key and your public key can verify this transformation was done by exactly its private key. We call this transformation ‘signing’ and we call the transformed payload ‘signature’.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;Server holds a private and a public key; the client holds a copy of the public key. 1: the client asks for a signature of a random number, 91825. 2: the server signs it with its private key and sends it back. 3: the client checks that the signature is for 91825 and matches the server’s public key; if both are true, the server is genuine&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;2622&quot; height=&quot;984&quot; src=&quot;https://hamza-cskn.com/_astro/signing.BkRWuCYn.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;Now the server can prove itself to any client, without having to trust them to keep its signature secret.&lt;/p&gt;
&lt;p&gt;However, there is a very big problem here. I told you everyone has the verify() function therefore, the client in the diagram contains server’s public key. However in practice it means all servers we want to communicate with, have to serve their public keys to all possible clients. Even if we do that, the client still needs to know that the public key it received comes from trusted services. We just reintroduced same problem while solving it.&lt;/p&gt;
&lt;h2 id=&quot;how-can-the-client-be-sure-the-public-key-it-has-is-the-right-one&quot;&gt;How can the client be sure the public key it has is the right one?&lt;/h2&gt;
&lt;p&gt;Meet the certificates. Certificates unlock vouching from one entity to another. A certificate is just another signature that basically says ‘public key of that host is that’. To handle countless entities’ public keys we just link them to a central entity. If a trusted entity vouches that this public key belongs to this host, I can believe it. Therefore, I can store only the central entities’ public keys. It is the key idea of the solution.&lt;/p&gt;
&lt;p&gt;Today, we call these ‘central entities’ as ‘certificate authorities’. Every program that wants to use TLS protocol uses a predefined, built-in set of public keys of certificate authorities.&lt;/p&gt;
&lt;p&gt;When you vouch for millions of certificates, the risk of leaking your private key becomes unacceptable. Besides, signing certificates means you have to use your private key many times which also increases possibility of a leak. That is our next problem.&lt;/p&gt;
&lt;h2 id=&quot;extreme-paranoia-of-certificate-authorities&quot;&gt;Extreme Paranoia of Certificate Authorities&lt;/h2&gt;
&lt;p&gt;Imagine, you have entered your bank’s website. No UI difference, no warnings, no domain typo. There is nothing suspicious. You enter your login password.&lt;/p&gt;
&lt;p&gt;Minutes later, all your money is used to purchase Bitcoin. You didn’t do anything wrong. The worst thing is, you are not an exception.  Now, imagine government websites, military related services, e-commerce websites, search engines, social media and many more. Everybody would stop trusting websites.&lt;/p&gt;
&lt;p&gt;Leaking a private key that signs millions of certificates trusted by millions would be a disaster. It is almost more important than my Steam account.&lt;/p&gt;
&lt;p&gt;Firstly, nobody uses these private keys directly as they want. When you’ve purchased a certificate, your certificate will be signed by an ‘intermediate certificate’ which is signed by a ‘root certificate’. So, the root certificate’s private key is stored offline and used rarely. There are many intermediate certificates. Intermediate certificates are easy to invalidate because the programs’ built-in stores keep root certificate not intermediate ones.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;Certificate chain: the root private key signs the intermediate certificate, and the intermediate private key signs the server’s certificate for ilovemymom.com. The server keeps its private key, its certificate and the intermediate certificate; the client keeps only the root certificate. The client asks for certificates and a signature of a number it made up, then checks the domain, that the root signed the intermediate, that the intermediate signed the server certificate, and that the server certificate verifies the signature&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1890&quot; height=&quot;1506&quot; src=&quot;https://hamza-cskn.com/_astro/certificate-chain.CS70vzPm.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;This is the full picture of how certificates typically work.&lt;/p&gt;
&lt;p&gt;If an intermediate certificate’s private key got leaked, then we just tell everyone this certificate is no longer trusted. This affects a smaller part of websites. The untrusted certificates list is called a ‘Certificate revocation list’.&lt;/p&gt;
&lt;p&gt;The root certificate’s private key is stored offline and generated via very specific and strict procedures. This process is called ‘&lt;a href=&quot;https://en.wikipedia.org/wiki/Key_ceremony&quot;&gt;key ceremony&lt;/a&gt;’. The root private key is stored in a tamper-resistant offline &lt;a href=&quot;https://en.wikipedia.org/wiki/Hardware_security_module&quot;&gt;hardware security module&lt;/a&gt; (HSM) in a box in a vault in a cage in a secure room in a secure building. Nobody (including the ceremony admin) can enter the room alone. Any detected tampering operation to the HSM causes zeroization in microseconds.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;A man in a suit crouches by an open safe holding a sealed tamper-evident bag, with ceremony documents on top of the safe&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;560&quot; height=&quot;370&quot; src=&quot;https://hamza-cskn.com/_astro/key-ceremony.DA1hxp8O.jpg&quot;&gt;
&lt;em&gt;Key Ceremony. Photo: &lt;a href=&quot;https://www.techtarget.com/news/COVID-19-strains-critical-certificate-authority-processes/839580/&quot;&gt;TechTarget&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;ICANN’s DNSSEC key ceremonies are very cool. The whole process of key ceremony must be recorded and sometimes streamed on YouTube. It is good example that gives an idea of the procedures.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=o8iIB_ZD3Ig&quot;&gt;Watch a key ceremony on YouTube&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There are ~150 root certificates managed by various certificate authorities. Also, the most important part of the certificate security is logging.&lt;/p&gt;
&lt;h3 id=&quot;certificate-transparency&quot;&gt;Certificate Transparency&lt;/h3&gt;
&lt;p&gt;All publicly trusted TLS certificates must be logged when they are signed and shared in public. This mechanism is called ‘certificate transparency’ and described in &lt;a href=&quot;https://www.rfc-editor.org/info/rfc6962/&quot;&gt;RFC 6962&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Comodo and DigiNotar were two certificate authorities. Comodo in &lt;strong&gt;March 2011&lt;/strong&gt; and DigiNotar in &lt;strong&gt;June 2011&lt;/strong&gt; got hacked &lt;strong&gt;within 3 months&lt;/strong&gt;. In the DigiNotar case, 300,000 unique IP addresses in Iran were affected. After that, Ben Laurie and Adam Langley from Google said “whatta f* ???” ohm. I mean.. They just suggested a mechanism that is known as ‘certificate transparency’. By the way, DigiNotar went bankrupt a month later.&lt;/p&gt;
&lt;p&gt;These logs can be discovered on &lt;a href=&quot;https://crt.sh&quot;&gt;crt.sh&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;crt.sh identity search for hamza-cskn.com: a table of certificates with crt.sh ID, validity dates, common name, matching identities and issuers such as Let’s Encrypt and Google Trust Services&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;2444&quot; height=&quot;1650&quot; src=&quot;https://hamza-cskn.com/_astro/crt-sh.B1GFf4yC.png&quot;&gt;
&lt;em&gt;Typical result page of Certificate Transparency logs.&lt;/em&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>https://hamza-cskn.com/blog/cancellable-events-are-not-events</id>
    <title>Cancellable events are not events.</title>
    <link href="https://hamza-cskn.com/blog/cancellable-events-are-not-events"/>
    <published>2026-10-31T00:00:00.000Z</published>
    <updated>2026-10-31T00:00:00.000Z</updated>
    <summary>What events actually solve, and why cancellable events bring back the coupling events were meant to remove. They are votes, not events.</summary>
    <content type="html">&lt;p&gt;This is not AI content. So there is no gaslighting here.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;I’m sorry that I long ago coined the term “objects” for this topic because it gets many people to focus on the lesser idea. The big idea is “messaging”&lt;/em&gt;&lt;br&gt;
&lt;em&gt;&lt;strong&gt;— Alan Kay (who coined the term ‘OOP’), in a &lt;a href=&quot;http://c2.com/cgi/wiki?AlanKayOnMessaging&quot;&gt;email&lt;/a&gt;.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Events are quite important concept that unlocks loosely coupled software architectures. They have proven themselves in countless production systems, and there’s a lot to learn from &lt;strong&gt;why&lt;/strong&gt; they work well.&lt;/p&gt;
&lt;p&gt;Best way to understand something is understanding problem behind them. Before go for ‘cancellable events’, we first need to look at what events solve.&lt;/p&gt;
&lt;h2 id=&quot;what-events-solves&quot;&gt;What events solves?&lt;/h2&gt;
&lt;p&gt;I worked very hard and finally split the problems that events solve into 2 pure element.&lt;/p&gt;
&lt;h3 id=&quot;epistemic-dependency-problem&quot;&gt;Epistemic Dependency Problem&lt;/h3&gt;
&lt;p&gt;Imagine you’re writing a function. Your function have to do some operations. Also you know a few exist functions. Each of these functions do one part of the operations you need therefore your function must call these functions. Everything looks simple.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;function&lt;/span&gt;&lt;span&gt; placeOrder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;order&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  saveOrder&lt;/span&gt;&lt;span&gt;(order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  sendConfirmationEmail&lt;/span&gt;&lt;span&gt;(order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  reserveInventory&lt;/span&gt;&lt;span&gt;(order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  trackAnalytics&lt;/span&gt;&lt;span&gt;(order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;During writing your function you were know what operations your function would need. It is the crucial part. Maybe tomorrow you’ll want to send SMS. It is enough to add one line and call another function.&lt;/p&gt;
&lt;p&gt;However, what happen if you are not the author of the code? Maybe you added it as dependency. Editing your dependency is not sustainable.&lt;/p&gt;
&lt;p&gt;You want to add new line there but you can’t. Root cause of that problem is that the author cannot know what operations you’ll need.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;A two-by-two grid: you know what to do but not when; your dependency knows when to do it but not what&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1302&quot; height=&quot;734&quot; src=&quot;https://hamza-cskn.com/_astro/when-vs-what.Uoi9zG8T.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;Your dependency knows &lt;strong&gt;when&lt;/strong&gt;. You know &lt;strong&gt;what&lt;/strong&gt;. This is the classic case for inversion of control, or as it’s often called, the Hollywood Principle: “Don’t call us, we’ll call you.”&lt;/p&gt;
&lt;p&gt;If author want to allow external operations they must manage ‘functions’ as objects. So author can handle these ‘operations’ while being agnostic of their details. Once that harness complete, you can provide your functions as objects to there and they can call your functions.&lt;/p&gt;
&lt;p&gt;Usually we use a system that takes events and runs functions they want. We call these mechanism ‘Event Bus’ .&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;function&lt;/span&gt;&lt;span&gt; placeOrder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;order&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  saveOrder&lt;/span&gt;&lt;span&gt;(order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  sendConfirmationEmail&lt;/span&gt;&lt;span&gt;(order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  reserveInventory&lt;/span&gt;&lt;span&gt;(order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  trackAnalytics&lt;/span&gt;&lt;span&gt;(order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  events.&lt;/span&gt;&lt;span&gt;emit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;orderPlaced&quot;&lt;/span&gt;&lt;span&gt;, order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;// your codebase&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;events.&lt;/span&gt;&lt;span&gt;on&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;orderPlaced&quot;&lt;/span&gt;&lt;span&gt;, sendSms);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;temporal-dependency-problem&quot;&gt;Temporal Dependency Problem&lt;/h3&gt;
&lt;p&gt;Imagine you wrote 2 function. Ones input is others output. Such as:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;true&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  consume&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;produce&lt;/span&gt;&lt;span&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The two functions take turns. The consumer waits for the producer, which is fine. But the producer also waits for the consumer, and that’s the problem.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;queue &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;//worker1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;true&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  queue.&lt;/span&gt;&lt;span&gt;put&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;produce&lt;/span&gt;&lt;span&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;//worker2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;true&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  consume&lt;/span&gt;&lt;span&gt;(queue.&lt;/span&gt;&lt;span&gt;get&lt;/span&gt;&lt;span&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With that, we’ve fixed the problem. The producer puts its result into a queue without waiting for the consumer. (Technically, it still waits for the queue, but a push is fast enough to ignore.) Events make this possible. A producer that announces what happened needs no answer, so its events can be queued.&lt;/p&gt;
&lt;p&gt;Now, imagine you have too many object to produce and consume. However your producer (or consumer) is too slow while other one is too fast. Let’s say the every producer produces every &lt;em&gt;p&lt;/em&gt; seconds and every consumer consumes every &lt;em&gt;c&lt;/em&gt; seconds. So you thought you can reduce consumer count to save your resources (maybe for more producers). The best idea is using a single common queue, &lt;em&gt;p&lt;/em&gt; amount of producer and &lt;em&gt;c&lt;/em&gt; amount of consumer.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;p producers push items into one shared queue, and c consumers take items from it; each producer makes one item every p seconds, each consumer handles one every c seconds&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;2000&quot; height=&quot;1091&quot; src=&quot;https://hamza-cskn.com/_astro/shared-queue.BhoQUBD4.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;Congratulations. Now you’ve a system that abstracts producers and consumers via queue. Eugster et al. reached the same three in &lt;a href=&quot;https://dl.acm.org/doi/10.1145/857076.857078&quot;&gt;&lt;em&gt;The Many Faces of Publish/Subscribe&lt;/em&gt;&lt;/a&gt;, a well-regarded 2003 survey in ACM Computing Surveys.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Space decoupling:&lt;/strong&gt; Your producer counts and consumer counts are does not matter.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Time decoupling:&lt;/strong&gt; Consumers can be interrupted without missing anything produced in the meantime. (persistence)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Synchronization decoupling:&lt;/strong&gt; Your producers do not wait for the consumers.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt=&quot;Three diagrams of a publisher, an event service and subscribers: space decoupling (one publish notifies many subscribers), time decoupling (publisher and subscriber need not be up at the same time) and synchronization decoupling (neither side blocks the other)&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1440&quot; height=&quot;656&quot; src=&quot;https://hamza-cskn.com/_astro/pubsub-decoupling.DQwCxqVH.png&quot;&gt;
&lt;em&gt;The Many Faces of Publish/Subscribe, ACM Computing Surveys, Vol. 35, No2, June 2003.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;what-is-cancellable-events&quot;&gt;What is cancellable events?&lt;/h2&gt;
&lt;p&gt;Imagine you have a function that you know when to run and how to do. Looks like you do not need an event mechanism but you want to allow other dependencies, aborting your function before do its job.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;function&lt;/span&gt;&lt;span&gt; placeOrder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;order&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  const&lt;/span&gt;&lt;span&gt; event&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; { order, cancelled: &lt;/span&gt;&lt;span&gt;false&lt;/span&gt;&lt;span&gt; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  events.&lt;/span&gt;&lt;span&gt;emit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;beforeOrderPlaced&quot;&lt;/span&gt;&lt;span&gt;, event);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  if&lt;/span&gt;&lt;span&gt; (event.cancelled) &lt;/span&gt;&lt;span&gt;return&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  saveOrder&lt;/span&gt;&lt;span&gt;(order);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I won’t lie. It’s attractive. Countless codebases use it and they’re happy with their life. The DOM lets you cancel default behaviors, like following a link or submitting a form. Game modding APIs like Spigot let you cancel in-game actions, which is exactly what makes mods possible. Today I won’t say you the idea behind ‘cancellable events’ is bad.&lt;/p&gt;
&lt;h2 id=&quot;why-cancellable-events-are-not-events&quot;&gt;Why cancellable events are not events?&lt;/h2&gt;
&lt;p&gt;In event streaming, events are information flowing from producers to consumers. In programming, they’re signals to run some functions. Both borrow the word from its everyday meaning: something that happens. Once it has happened, it can’t be changed.&lt;/p&gt;
&lt;p&gt;An event cannot be cancelled due to its nature. You can only create another event that says it’s cancelled. You may create an Outlook Calendar event then cancel it. But it is two distinct event. Create and cancel events.&lt;/p&gt;
&lt;h2 id=&quot;problems-with-cancellable-events&quot;&gt;Problems with cancellable events&lt;/h2&gt;
&lt;p&gt;Imagine you are developer of a plugin. There are many event listener(consumer) in your plugin and the platform author may add more plugins like yours.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You may want to cancel an event, but you need to ensure other consumers can’t uncancel it. This can be solved by disallowing uncancelling by design, if you’re okay with losing that ability.&lt;/li&gt;
&lt;li&gt;You may want to do an operation if the event is not cancelled. Some systems partially solve this issue by adding a monitor level priority that disallows cancelling events and guarantees these consumers are last. So, you know the state you see is final. However if you want to also cancel the event (it is required than probably you think), you cannot.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In case of you are just an author of single consumer, it makes impossible to guarantee knowing what priority should your consumer introduce.&lt;/p&gt;
&lt;p&gt;In case of you are author of event bus, it requires being aware what consumer touches what state. So, you can configure priority of your consumers. This is a good case. You have to be careful when updating behavior of the consumers and you have to sacrifice multi-threading, lockless design. In theory, you can. In practice, if you are a system admin you probably does not want to inspect all listeners of all plugins you’ve loaded.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;The problem with ‘cancellable events’ is they contradicts with event definition and recreates problems that typical events solve. The problem is not idea behind cancellable events.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Producer must be synchronized with consumers. &lt;strong&gt;It recreates the temporal coupling issue.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Consumers always share a mutable state: is the event cancelled? flag. It inherently makes order of consumption matters. &lt;strong&gt;It recreates the epistemic coupling.&lt;/strong&gt; In events, if your consumers does not share a mutable state, order of consumption is not important. However in cancellable events that situation is guaranteed. So you have to prioritize your consumers.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The idea behind cancellable events is a simple voting mechanism. You choose how votes are resolved: the last vote wins, the majority wins, the highest priority wins, or everyone has to agree (like &lt;a href=&quot;https://en.wikipedia.org/wiki/Two-phase_commit_protocol&quot;&gt;&lt;strong&gt;two-phase commit&lt;/strong&gt;&lt;/a&gt;). Call it a vote, and it no longer breaks any promise an event makes.&lt;/p&gt;
&lt;p&gt;Voting is a good concept that solves important problems. But they aren’t the problems events solve. An event answers “what happened?” A vote answers “should it happen?”. Intentionally when you see name of ‘event’, probably you won’t think about priorities, majority, consensus etc.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>https://hamza-cskn.com/blog/ping-under-the-hood</id>
    <title>What’s really going on under the hood when you use the ping?</title>
    <link href="https://hamza-cskn.com/blog/ping-under-the-hood"/>
    <published>2026-10-25T00:00:00.000Z</published>
    <updated>2026-10-25T00:00:00.000Z</updated>
    <summary>How ping works: ICMP, why it needs no port, how traceroute uses TTL, and how the kernel answers it instead of a socket.</summary>
    <content type="html">&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;1. What is the difference between typical TCP and ping requests (Linux)?&lt;/em&gt;&lt;br&gt;
&lt;em&gt;2. Why doesn’t ping need a port?&lt;/em&gt;&lt;br&gt;
&lt;em&gt;3. Why can you connect to a host but not ping it?&lt;/em&gt;&lt;br&gt;
&lt;em&gt;4. How does traceroute know the path your packet took?&lt;/em&gt;&lt;br&gt;
&lt;em&gt;5. If ICMP and IP are both in Layer 3, how is ICMP wrapped by IP?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is not AI content. So, there is no gaslighting here.&lt;/p&gt;
&lt;p&gt;You’ll understand every question. Don’t rush.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/iputils/iputils/blob/master/ping/ping.c&quot;&gt;Ping&lt;/a&gt; is a command-line utility for Linux to test the reachability of a host. Basically, it sends debug packets to a specific host and typically causes the target host to send a response (also known as ‘pong’) back. If you receive a response, it means the network path between you and the host works properly.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;Terminal running ping 8.8.8.8: five replies of 64 bytes with icmp_seq, ttl=63 and round-trip times, then the packet loss and rtt statistics&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1010&quot; height=&quot;450&quot; src=&quot;https://hamza-cskn.com/_astro/ping-output.C2XWkly3.png&quot;&gt;
&lt;em&gt;Typical ping command output&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-debug-protocol-icmp&quot;&gt;The debug protocol: ICMP&lt;/h2&gt;
&lt;p&gt;The debug packets I mentioned earlier are &lt;strong&gt;Internet Control Message Protocol&lt;/strong&gt; (ICMP) packets. ICMP is a special debug protocol to test network devices. Here is what it contains:&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;ICMP packet layout: Type (bits 0–7), Code (bits 8–15), Checksum (bits 16–31), followed by a variable-length Content field&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;336&quot; height=&quot;87&quot; src=&quot;https://hamza-cskn.com/_astro/icmp-packet.DChqOBax.png&quot;&gt;
&lt;em&gt;Structure of ICMP packets&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The first 8 bits&lt;/strong&gt; of ICMP packets represent the subject of the packet. There are many but we are interested in only two of them: Echo Request(used for ping), Echo Reply(used for pong).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The property ‘code’&lt;/strong&gt; is not important for our case but you can think of it as a variety of the type property.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The checksum property&lt;/strong&gt; is used to guarantee the entire packet is transmitted without corruption. There is a specific algorithm to generate this checksum. During the generation of this checksum it is set to zero (due to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Chicken_or_the_egg&quot;&gt;chicken-and-egg problem&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The property ‘content’&lt;/strong&gt; contains nothing mandatory in our case. Typically, it contains the timestamp of the sent ping. However, technically the allocated content part is not standard. It may be too short. In this case it can be ignored. Also, if the content part is too long, usually it is filled by zero bytes or sequential bytes like: 00,01,02,03…FF,00…&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;Table of the seven OSI layers and their protocols; the Network layer lists ICMP, IGMP, IPsec, IPv4, IPv6, IPX and RIP, the Transport layer TCP and UDP&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1072&quot; height=&quot;706&quot; src=&quot;https://hamza-cskn.com/_astro/osi-model.B4SURZm9.png&quot;&gt;
&lt;em&gt;The famous OSI model and protocols where they sit.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;how-is-it-possible-that-icmp-and-ipv4-sit-on-the-same-layer&quot;&gt;How is it possible that ICMP and IPv4 sit on the same layer?&lt;/h2&gt;
&lt;p&gt;You may sense that the &lt;strong&gt;I&lt;/strong&gt;CM&lt;strong&gt;P&lt;/strong&gt; is an internet protocol because it is Internet Control Message Protocol. Also we’ve seen above that the ICMP header doesn’t contain any IP address. Yet both are classified as Layer 3 protocols. Then they cannot depend on each other, right? Actually, no.&lt;/p&gt;
&lt;p&gt;The word ‘layer’ represents a group of protocols, not exact levels. ICMP is on top of IP and both are in Layer 3. In other words, ICMP is encapsulated by IP.&lt;/p&gt;
&lt;h2 id=&quot;why-doesnt-ping-need-a-port&quot;&gt;Why doesn’t ping need a port?&lt;/h2&gt;
&lt;p&gt;According to the OSI layers we discussed above, the port concept is introduced by TCP and UDP protocols. The objective of IP protocol is reaching the correct computer. A port is meaningful only when you have a computer. In other words, IP’s job ends where the port’s job starts.&lt;/p&gt;
&lt;p&gt;ICMP was created to debug network issues between computers, not applications. It also allows us to test the network without running new applications to listen. That is why it doesn’t need ports.&lt;/p&gt;
&lt;h2 id=&quot;why-can-you-connect-to-a-host-but-not-ping-it&quot;&gt;Why can you connect to a host but not ping it?&lt;/h2&gt;
&lt;p&gt;Firewalls. Firewalls do not like ICMP. They usually block ICMP packets to make scanning harder.&lt;/p&gt;
&lt;h2 id=&quot;how-does-traceroute-know-the-path-your-packet-took&quot;&gt;How does traceroute know the path your packet took?&lt;/h2&gt;
&lt;p&gt;&lt;img alt=&quot;Terminal running traceroute -n google.com: twelve numbered hops with router IPs and three round-trip times each; hops 2 and 6 show * * *&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;835&quot; height=&quot;239&quot; src=&quot;https://hamza-cskn.com/_astro/traceroute-output.-QYXOCxZ.png&quot;&gt;
&lt;em&gt;Typical traceroute command output.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Traceroute is a command-line utility for Linux to see paths where your ping requests visit during the transmission.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;IPv4 header layout by octet: Version, IHL, DSCP, ECN, Total Length, Identification, Flags, Fragment Offset, Time to Live (highlighted), Protocol, Header Checksum, Source Address, Destination Address and Options&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1528&quot; height=&quot;818&quot; src=&quot;https://hamza-cskn.com/_astro/ipv4-header.C_4ZcU-f.png&quot;&gt;
&lt;em&gt;Structure of IPv4 packets.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;IPv4 packets have a mechanism called ‘Time to Live’. This mechanism(and a little bit magic) unlocks tracking where our ping packet goes.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;A packet leaves the transmitting machine with TTL=6; each router decrements it, and the router that receives it at TTL=1 discards it before it reaches the receiving machine&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1071&quot; height=&quot;581&quot; src=&quot;https://hamza-cskn.com/_astro/ttl.C4RsPtVt.png&quot;&gt;
&lt;em&gt;How TTL works.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When an IP packet is sent, our packet goes to the target machine thanks to routers. Each router takes our packet and forwards it to the next appropriate router. Before the router forwards IP packets, it decreases the TTL of the packet by 1. If the TTL is 1 and the destination is not reached, the router doesn’t forward it further.&lt;/p&gt;
&lt;p&gt;So we can tell the network devices to stop forwarding earlier than usual. The crucial part is: the routers do not drop these TTL expired packets. They respond as ‘time exceeded’. When the response comes you see the router as the sender in the response packet.&lt;/p&gt;
&lt;p&gt;Basically, traceroute is a program that sends ping packets to the same destination with various TTLs. So we receive responses from all routers in the path. However if a machine does not like ICMP packets (firewall), it may choose silence. This is why some lines are “* * *” in the traceroute outputs.&lt;/p&gt;
&lt;h2 id=&quot;for-linux-view-what-is-the-difference-between-typical-tcpudp-packets-and-ping&quot;&gt;For Linux view, what is the difference between typical TCP/UDP packets and ping?&lt;/h2&gt;
&lt;p&gt;The ping utility sends an ICMP Echo Request to the target. Every ’64 bytes from 8.8.8.8: blah blah’ line represents a distinct Request/Response pair.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;A host sends an ICMP Echo-Request through a router to a server, and the server sends an ICMP Echo-Reply back&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;800&quot; height=&quot;226&quot; src=&quot;https://hamza-cskn.com/_astro/echo-request-reply.dTT2xJYk.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;In typical TCP/UDP communication, a received packet gets parsed and delivered to a user-space application. This delivery is done via network sockets. They act as a bridge between kernel-space and user-space to allow applications to handle network payloads. You can think of network sockets as special files (but this is not exactly what they are). The kernel writes the payloads to this file and applications read from it. This reading and writing step can be done by using the same system call as in the case of reading files in the filesystem.&lt;/p&gt;
&lt;p&gt;I want to talk more about sockets. Think of servers we use everywhere: Nginx, NodeJS servers, DotNet micro-service applications… It doesn’t matter which one you use. In Linux, when you run any kind of process that listens for TCP, your application definitely uses these system calls: socket(), bind(), listen(). To consolidate our understanding we need to know which parameters these system calls depend on. I don’t want to go off-topic so we’ll inspect only bind().&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt; bind&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt; sockfd&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;const&lt;/span&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; sockaddr &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;addr&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;socklen_t&lt;/span&gt;&lt;span&gt; addrlen&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The system call bind() needs 3 parameters. &lt;strong&gt;The first one&lt;/strong&gt; is the file descriptor of the socket. If you don’t know file descriptors you can think of them as file IDs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The second parameter&lt;/strong&gt; is a config for the binding operation. &lt;strong&gt;The third&lt;/strong&gt; parameter is needed for memory management. We ignore it.&lt;/p&gt;
&lt;p&gt;The sockaddr struct is not a concrete implementation of struct. This is a kind of polymorphism in C. I mean, if you go to the definition of sockaddr, you won’t find anything useful. This is an abstract type to merge IPv6 and IPv4 configs. Okay maybe I am over-explaining now. I just want to explain to you why we should look for the struct named ‘&lt;a href=&quot;https://man7.org/linux/man-pages/man3/sockaddr.3type.html&quot;&gt;&lt;strong&gt;sockaddr_in&lt;/strong&gt;&lt;/a&gt;’ (IPv4) not for sockaddr.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; sockaddr_in {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     sa_family_t&lt;/span&gt;&lt;span&gt;     sin_family;&lt;/span&gt;&lt;span&gt;     /* IPv4 */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     in_port_t&lt;/span&gt;&lt;span&gt;       sin_port;&lt;/span&gt;&lt;span&gt;       /* Port */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     struct&lt;/span&gt;&lt;span&gt; in_addr  sin_addr;&lt;/span&gt;&lt;span&gt;       /* IP address */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; };&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is how a regular user-space application listens to the network. However typical ICMP packets are not handled by user-space for the receiver. They are replied to by the kernel. This is the key difference between requests that go to your web server and ping packets.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>https://hamza-cskn.com/blog/memory-pointers-2</id>
    <title>How Are Data Structures Stored in Memory? (2/3): Array, Struct &amp; Linked List</title>
    <link href="https://hamza-cskn.com/blog/memory-pointers-2"/>
    <published>2023-08-24T00:00:00.000Z</published>
    <updated>2023-08-24T00:00:00.000Z</updated>
    <summary>How arrays, structs and linked lists are laid out in memory, and what that means for their complexity.</summary>
    <content type="html">&lt;h2 id=&quot;array-struct--linked-list&quot;&gt;Array, Struct &amp;amp; Linked List&lt;/h2&gt;
&lt;p&gt;In the &lt;a href=&quot;https://hamza-cskn.com/blog/memory-pointers-1&quot;&gt;previous section&lt;/a&gt;, I talked about how memory works. If you don't know what pointers are, I recommend reading that section first.&lt;/p&gt;
&lt;p&gt;As you know, there are many different data structures (Tree, Map, Trie, Queue, Stack, Graph…), and the way these structures are stored in memory actually relies on 3 basic data structures: Array, Struct and Linked List. Today we will examine these data structures. We will look at the complexity of the operations they perform.&lt;/p&gt;
&lt;p&gt;Throughout this blog I used C for the examples, but I don't think there is a major difference in other languages.&lt;/p&gt;
&lt;h2 id=&quot;array&quot;&gt;Array&lt;/h2&gt;
&lt;p&gt;Arrays keep their elements one after another in memory. For example, let's imagine an integer array.&lt;/p&gt;
&lt;p&gt;If addresses 1024–1028 in memory are allocated for the first element, addresses 1028–1032 are allocated for the second element, and it continues this way for each element. An equal amount of space is allocated for every element.&lt;/p&gt;
&lt;p&gt;We need to specify in advance how many elements this array will have, but in high-level languages this is done automatically.&lt;/p&gt;
&lt;p&gt;The most important advantage of arrays is that they can reach the value at the index you specify in O(1) complexity. The reason is that they can know at which memory address the value at any index is stored.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;size * index + address of the first element&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now, do you understand why we write &lt;code&gt;array[0]&lt;/code&gt; for the first element of an array?&lt;/p&gt;
&lt;p&gt;To prove what we have described, let me show you an interesting example. Carefully examine the C code below.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt; &amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt; main&lt;/span&gt;&lt;span&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  char&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;arr &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;'a'&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;'b'&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;'c'&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;'d'&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  char&lt;/span&gt;&lt;span&gt; first &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; 0&lt;/span&gt;&lt;span&gt;[arr];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;%d\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, first);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Above, we are actually doing the operation we always do, in reverse.&lt;br&gt;
Instead of taking index 0 of the array at address &lt;em&gt;arr&lt;/em&gt; (&lt;code&gt;arr[0]&lt;/code&gt;),&lt;br&gt;
we take the value at index '&lt;em&gt;arr&lt;/em&gt;' of the array at address 0 (&lt;code&gt;0[arr]&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;In the previous section, I prepared some code to practically show how arrays work. You can access that code from this &lt;a href=&quot;https://replit.com/@hamza-cskn/MemoryDebugger#main.c&quot;&gt;&lt;em&gt;&lt;strong&gt;Replit&lt;/strong&gt;&lt;/em&gt;&lt;/a&gt; page and observe arrays in memory.&lt;/p&gt;
&lt;h3 id=&quot;multidimensional-array&quot;&gt;Multidimensional Array&lt;/h3&gt;
&lt;p&gt;Multidimensional arrays are actually arrays that hold arrays. A 2-dimensional array holds one-dimensional arrays. In other words, it actually holds addresses.&lt;/p&gt;
&lt;p&gt;The complexity of multidimensional arrays is O(m), where m is the number of dimensions. However, since the number of dimensions usually stays constant, it can be considered O(1).&lt;/p&gt;
&lt;h2 id=&quot;struct&quot;&gt;Struct&lt;/h2&gt;
&lt;p&gt;Structs (also known as Record and Tuple) are a data structure that lets you keep several data types together. Unlike objects, their only purpose is to hold data.&lt;/p&gt;
&lt;p&gt;A struct keeps its data one after another, just like arrays, and when you want to access a field, it finds that field's location in memory in O(1) complexity based on its order within the struct.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; person {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; int&lt;/span&gt;&lt;span&gt; age;&lt;/span&gt;&lt;span&gt;    // 0-4 byte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; char&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;name;&lt;/span&gt;&lt;span&gt; // 4-12 byte&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;person.age &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;person.name &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; &quot;Hamza&quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;They hold data just like arrays. That's why if you create an array and cast it to a struct, you actually get a struct:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; person anyperson &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; person) {&lt;/span&gt;&lt;span&gt;19&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&quot;Ali&quot;&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let's examine how structs look in memory. First, let's find out how much space &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt;, a char pointer and our &lt;code&gt;person&lt;/code&gt; struct take up in memory, using the &lt;code&gt;sizeof&lt;/code&gt; operator.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt; &amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt; main&lt;/span&gt;&lt;span&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;sizeof(int):       &lt;/span&gt;&lt;span&gt;%lu\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;sizeof(char):      &lt;/span&gt;&lt;span&gt;%lu\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;sizeof(char *):    &lt;/span&gt;&lt;span&gt;%lu\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;sizeof(person):    &lt;/span&gt;&lt;span&gt;%lu\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; person));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; gcc test.c &lt;/span&gt;&lt;span&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span&gt;a.out&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img alt=&quot;Output of the sizeof program: int is 4, char is 1, char * is 8 and person is 16 bytes&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;354&quot; height=&quot;116&quot; src=&quot;https://hamza-cskn.com/_astro/sizeof-output.C-oJ0YU_.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;As you can see, our struct holds an integer (4 bytes) and a char pointer (8 bytes). However, it takes up 16 bytes in memory. So what are the remaining 4 bytes allocated for?&lt;/p&gt;
&lt;p&gt;To understand this, let's debug our code. I'm using the &lt;a href=&quot;https://replit.com/@hamza-cskn/MemoryDebugger#main.c&quot;&gt;memory debug code&lt;/a&gt; I shared in the previous section.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt; &quot;memory_debugger.h&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt; &amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; person {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; int&lt;/span&gt;&lt;span&gt; age;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; char&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt; main&lt;/span&gt;&lt;span&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; person hamza &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; { &lt;/span&gt;&lt;span&gt;20&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&quot;hamza&quot;&lt;/span&gt;&lt;span&gt; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;age address:       &lt;/span&gt;&lt;span&gt;%p\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;hamza.age);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;name address:      &lt;/span&gt;&lt;span&gt;%p\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;hamza.name);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;hamza address:     &lt;/span&gt;&lt;span&gt;%p\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;hamza);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;name pointer val.: &lt;/span&gt;&lt;span&gt;%p\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, hamza.name);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; print_memory&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;hamza, &lt;/span&gt;&lt;span&gt;16&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; gcc test.c memory_debugger.c &lt;/span&gt;&lt;span&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; .&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span&gt;a.out&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img alt=&quot;Memory dump of the struct: 4 bytes for age, 4 unused bytes, then the 8-byte name pointer&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1024&quot; height=&quot;154&quot; src=&quot;https://hamza-cskn.com/_astro/struct-memory-debug.Bv4rvFwC.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;These 4 bytes are actually not allocated for any data. If we added another 4-byte data type to our struct, it would still take up 16 bytes.&lt;/p&gt;
&lt;p&gt;The purpose of this gap is memory alignment (&lt;a href=&quot;https://en.m.wikipedia.org/wiki/Data_structure_alignment&quot;&gt;aligning&lt;/a&gt;). I won't go into this topic in detail today, but simply put, processors handle the information in memory in chunks of 4 or 8 bytes (this is where 32-bit and 64-bit come from). When you ask for address 17, the CPU actually fetches the bytes between 16 and 24. Unaligned data can sit right between 2 memory blocks and needlessly force the processor to read 2 separate blocks. That's why keeping all data aligned is important for performance.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; person {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; int&lt;/span&gt;&lt;span&gt; age;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; char&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; person {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; char&lt;/span&gt;&lt;span&gt; age;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; char&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; person {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; int&lt;/span&gt;&lt;span&gt; age;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; int&lt;/span&gt;&lt;span&gt; height;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; char&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All 3 of these structs take up 16 bytes in memory (on 64-bit computers).&lt;/p&gt;
&lt;h2 id=&quot;linked-list&quot;&gt;Linked List&lt;/h2&gt;
&lt;p&gt;&lt;img alt=&quot;Singly linked list: a head pointer and nodes that each hold a value and the address of the next node&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;716&quot; height=&quot;254&quot; src=&quot;https://hamza-cskn.com/_astro/linked-list-diagram.COj4rCnG.png&quot;&gt;
&lt;em&gt;Representative illustration of the Linked List structure.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;A Linked List consists of nodes, each pointing to the one after it. In object-oriented programming these nodes can be objects. In our case, they are structs. Each struct holds at least one piece of data and the address of the node that comes after it.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; node {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; int&lt;/span&gt;&lt;span&gt; data;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; node &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let's create a Linked List with 5 elements and debug the memory.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt; &quot;memory_debugger.h&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;#include&lt;/span&gt;&lt;span&gt; &amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; node {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; int&lt;/span&gt;&lt;span&gt; data;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; node &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;next;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;int&lt;/span&gt;&lt;span&gt; main&lt;/span&gt;&lt;span&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; node fiveth &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;NULL&lt;/span&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; node fourth &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;fiveth};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; node third &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;fourth};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; node second &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;third};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; struct&lt;/span&gt;&lt;span&gt; node first &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;second};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;first: &lt;/span&gt;&lt;span&gt;%p\n&lt;/span&gt;&lt;span&gt;second: &lt;/span&gt;&lt;span&gt;%p\n&lt;/span&gt;&lt;span&gt;third: &lt;/span&gt;&lt;span&gt;%p\n&lt;/span&gt;&lt;span&gt;fourth: &lt;/span&gt;&lt;span&gt;%p\n&lt;/span&gt;&lt;span&gt;fiveth: &lt;/span&gt;&lt;span&gt;%p\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;first, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;second, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;third, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;fourth, &lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;fiveth);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;sizeof(struct node): &lt;/span&gt;&lt;span&gt;%lu\n&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; node));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; print_memory&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;first, &lt;/span&gt;&lt;span&gt;sizeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;struct&lt;/span&gt;&lt;span&gt; node) &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt; 5&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; 20&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img alt=&quot;Program output: the addresses of the five nodes, the node size (16 bytes), and a hex dump of the memory they occupy&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1024&quot; height=&quot;271&quot; src=&quot;https://hamza-cskn.com/_astro/linked-list-memory-debug.DXUANxnx.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;In this example, 5 structs, each taking up 16 bytes, were allocated in different regions of memory. Since they are in an isolated environment, they sit right next to each other in memory, but they don't have to end up this way.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;Annotated memory dump marking each node&amp;#39;s data, next pointer and padding bytes&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1024&quot; height=&quot;167&quot; src=&quot;https://hamza-cskn.com/_astro/linked-list-memory-annotated.DJN7Q5Cd.png&quot;&gt;&lt;/p&gt;
&lt;p&gt;As you can see, memory alignment exists here too. If you look carefully, the values in the next part are the same as the addresses on the left. So we can see that we created nodes that point to each other.&lt;/p&gt;
&lt;p&gt;Linked Lists have O(n) complexity when reading. If you are going to search a large data set, you may suffer. However, since these structures depend on each other very little, they can be stretched easily. They don't have to have a fixed memory size. They grow as new nodes are added.&lt;/p&gt;
&lt;p&gt;When you want to insert a new node at any point, only the node before it in the list is affected. Therefore the data can be manipulated very easily. If you wanted to do the same with arrays, at best you would have to shift all the data after the point you inserted at.
However, on modern CPUs this shifting is so fast, and arrays are so cache-friendly, that arrays usually beat Linked Lists at writes too. So unless you have a very good reason to use a Linked List, always prefer an array.&lt;/p&gt;
&lt;p&gt;You might ask: why discuss Linked Lists at all if they're practically useless?&lt;/p&gt;
&lt;p&gt;Because understanding Linked Lists unlocks incredible new data structures for us. Thanks to them, we can build &lt;a href=&quot;https://en.wikipedia.org/wiki/Search_tree&quot;&gt;Search Trees&lt;/a&gt;, and these trees let us index databases so we can search numerical data with very low complexity, like log(n).&lt;/p&gt;
&lt;p&gt;We have reached the end of this section. Before leaving, let me share a table showing the complexities of data structures in Java. The information below doesn't change much from system to system.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;Table of Java collections (List, Set, Map and Queue implementations) with the time complexity of each operation and the underlying data structure&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;864&quot; height=&quot;794&quot; src=&quot;https://hamza-cskn.com/_astro/java-complexity-table.C2XWn8Ql.png&quot;&gt;
&lt;em&gt;Time complexities of Java's data structures and their operations.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In the next section, I will talk about &lt;em&gt;hash&lt;/em&gt; functions, which let us express any kind of data as a number and allow us to read in O(1) complexity, and about trees. I hope this was a useful post.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>https://hamza-cskn.com/blog/memory-pointers-1</id>
    <title>How Are Data Structures Stored in Memory? (1/3): How Memory Works, What a Pointer Is</title>
    <link href="https://hamza-cskn.com/blog/memory-pointers-1"/>
    <published>2023-08-13T00:00:00.000Z</published>
    <updated>2023-08-13T00:00:00.000Z</updated>
    <summary>How memory works, how data is stored, and what pointers really are.</summary>
    <content type="html">&lt;h2 id=&quot;how-does-memory-work-what-is-a-pointer&quot;&gt;How does memory work? What is a pointer?&lt;/h2&gt;
&lt;p&gt;Data structures are among the most fundamental building blocks of software. From machine learning to embedded software, wherever software exists, data structures exist. Since that is the case, shouldn’t we learn them deeply? No matter which language you write, knowing that language’s internals well is always important.&lt;/p&gt;
&lt;p&gt;In this blog, I will talk about how memory works and how computers save data structures in memory. This section will be only about memory. In the other sections, we will also examine other data structures, especially Linked Lists, Arrays, and HashSets.&lt;/p&gt;
&lt;h2 id=&quot;how-does-memory-work&quot;&gt;How does memory work?&lt;/h2&gt;
&lt;p&gt;If you have coded in languages like C, C++, or Go, you are already familiar with &lt;a href=&quot;https://en.wikipedia.org/wiki/Pointer_(computer_programming)&quot;&gt;pointers&lt;/a&gt;. In this section, I will explain only pointers.&lt;/p&gt;
&lt;p&gt;Memory keeps data in a one-dimensional structure. It gives each byte an address. This address simply indicates which byte of memory it is. As an example, let’s assign the value &lt;code&gt;&quot;hello&quot;&lt;/code&gt; to a variable called &lt;code&gt;foo&lt;/code&gt; and see what happens.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;string foo &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; &quot;hello&quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img alt=&quot;Memory view of the string &amp;quot;hello&amp;quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;359&quot; height=&quot;127&quot; src=&quot;https://hamza-cskn.com/_astro/memory-hello.CmKOtGNL.png&quot;&gt;
&lt;em&gt;Memory appearance of the text &quot;hello&quot;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When you do this, a contiguous 6-byte region is allocated in memory. The first 5 of those bytes store &lt;code&gt;'h'&lt;/code&gt;, &lt;code&gt;'e'&lt;/code&gt;, &lt;code&gt;'l'&lt;/code&gt;, &lt;code&gt;'l'&lt;/code&gt;, &lt;code&gt;'o'&lt;/code&gt;. The last byte stores a null character. The reason is that the computer needs a mechanism to understand how long a string is. In other words, you must tell the computer where in memory to read and how many bytes to read.&lt;/p&gt;
&lt;p&gt;In the memory region above, the number written under each byte is that byte’s address. When the computer wants to access this string, it uses the address of the first character (&lt;code&gt;'h'&lt;/code&gt;) (&lt;code&gt;1702&lt;/code&gt;). So you can actually think of &lt;code&gt;foo&lt;/code&gt;’s value as &lt;code&gt;1702&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;When you want to read this string, the computer goes to this address (&lt;code&gt;1702&lt;/code&gt;). It reads that byte, then reads the next byte (&lt;code&gt;1703&lt;/code&gt;), and continues this way. If the byte it reads is a null character, then it understands it reached the end of the text and stops. Let’s examine this a bit with examples.&lt;/p&gt;
&lt;h2 id=&quot;pseudocode-implementations&quot;&gt;Pseudocode implementations&lt;/h2&gt;
&lt;p&gt;Below, I shared some sample code so you can understand this system better. Before getting into code, I need to mention one small thing. For convenient explanation, we say the sample address is &lt;code&gt;1702&lt;/code&gt;, but in practical use these numbers are very large. So large that even in hexadecimal they are around 9-digit numbers. Therefore we cannot keep address values in an integer type. Also, since these numbers cannot be negative (there is no &lt;code&gt;-1&lt;/code&gt; address in memory), we can keep them in an unsigned type.&lt;/p&gt;
&lt;p&gt;So for now, there is no harm in thinking of addresses as &lt;code&gt;unsigned long long&lt;/code&gt;. Now let’s print our &lt;code&gt;&quot;hello&quot;&lt;/code&gt; text to the screen.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Please remember that the code below does not belong to any specific programming language and was written only to convey the core idea.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Using a loop:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;print_string&lt;/span&gt;&lt;span&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  unsigned&lt;/span&gt;&lt;span&gt; long&lt;/span&gt;&lt;span&gt; long&lt;/span&gt;&lt;span&gt; address &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; &quot;hello&quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; //1702&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;true&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    char&lt;/span&gt;&lt;span&gt; byte &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; get_byte_at&lt;/span&gt;&lt;span&gt;(address &lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span&gt; i);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    if&lt;/span&gt;&lt;span&gt; (byte &lt;/span&gt;&lt;span&gt;==&lt;/span&gt;&lt;span&gt; '&lt;/span&gt;&lt;span&gt;\0&lt;/span&gt;&lt;span&gt;'&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      break&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    print&lt;/span&gt;&lt;span&gt;(byte);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    i&lt;/span&gt;&lt;span&gt;++&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Recursively:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;print_string&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;unsigned&lt;/span&gt;&lt;span&gt; long&lt;/span&gt;&lt;span&gt; long&lt;/span&gt;&lt;span&gt; address&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;span&gt; //1702&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; char&lt;/span&gt;&lt;span&gt; byte &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; get_byte_at&lt;/span&gt;&lt;span&gt;(address);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt; if&lt;/span&gt;&lt;span&gt; (byte &lt;/span&gt;&lt;span&gt;==&lt;/span&gt;&lt;span&gt; '&lt;/span&gt;&lt;span&gt;\0&lt;/span&gt;&lt;span&gt;'&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    return&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  print&lt;/span&gt;&lt;span&gt;(byte);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  print_string&lt;/span&gt;&lt;span&gt;(address &lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span&gt; 1&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When you want to read an integer, the computer knows that value is 4 bytes, so it reads 4 bytes and stops. The same applies to other numeric types. So null-termination is mostly a situation seen only with strings.&lt;/p&gt;
&lt;p&gt;There is one more thing you should know here. What we call a byte is an 8-digit number in binary. So it is nothing more than a number between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;255&lt;/code&gt;. When we write characters in a string expression, we actually write their numeric equivalents from the &lt;a href=&quot;https://en.wikipedia.org/wiki/ASCII&quot;&gt;ASCII&lt;/a&gt; table. For example, when you want to write the letter &lt;code&gt;'a'&lt;/code&gt;, you are actually writing &lt;code&gt;97&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;Numeric memory view of &amp;quot;hello&amp;quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;930&quot; height=&quot;310&quot; src=&quot;https://hamza-cskn.com/_astro/memory-hello-numeric.B6qu3n0y.png&quot;&gt;
&lt;em&gt;Numeric memory view of the text &quot;hello&quot;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When a letter is mentioned, remember that it is actually a number. When a string is mentioned, know that it is an address, and every address is also a number.&lt;/p&gt;
&lt;p&gt;When you assign &lt;code&gt;97&lt;/code&gt; to an integer, because integers need 4 bytes, the computer writes &lt;code&gt;0&lt;/code&gt; to the first three bytes and &lt;code&gt;97&lt;/code&gt; to the last byte. (Can vary from system to system. See: &lt;a href=&quot;https://en.wikipedia.org/wiki/Endianness#:~:text=In%20computing%2C%20endianness%20is%20the,byte%20significance%20compared%20to%20earliness.&quot;&gt;Endianness&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;When you assign &lt;code&gt;'a'&lt;/code&gt; to a char, it writes &lt;code&gt;97&lt;/code&gt; to one byte.&lt;/p&gt;
&lt;p&gt;For computers, everything is a number. The only difference is how you choose to interpret it.&lt;/p&gt;
&lt;h2 id=&quot;what-is-a-pointer&quot;&gt;What is a pointer?&lt;/h2&gt;
&lt;p&gt;As I mentioned above, addresses in memory are stored and used as numbers. Variables that hold these numbers are called pointers.&lt;/p&gt;
&lt;p&gt;Programming languages have special types for pointers. If a variable stores the address of a byte, this means it is a pointer. If the data at the address held by this variable is of type &lt;code&gt;char&lt;/code&gt;, we call it a char pointer (&lt;code&gt;char *&lt;/code&gt;). For example, the variable &lt;code&gt;foo&lt;/code&gt; is a char pointer. Because at the address where its value points (&lt;code&gt;1702&lt;/code&gt;) there is a char (&lt;code&gt;'h'&lt;/code&gt;). Same for integers: we call them int pointers (&lt;code&gt;int *&lt;/code&gt;). If there is also an address at the held address, then we look at the data that address points to, and for example if it is &lt;code&gt;char&lt;/code&gt;, we call it &lt;code&gt;char **&lt;/code&gt;, and this continues.&lt;/p&gt;
&lt;p&gt;To get the address of a variable, put &lt;code&gt;'&amp;amp;'&lt;/code&gt; at the front. To go to an address, put &lt;code&gt;'*'&lt;/code&gt; at the front of the pointer that stores the address.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt; c;&lt;/span&gt;&lt;span&gt;  // allocates 1 byte in memory (assume address is 3201)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;c &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; 'a'&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; // writes 97 ('a') there&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;pointer_c;&lt;/span&gt;&lt;span&gt; // allocates 8 bytes in memory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;pointer_c &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;c;&lt;/span&gt;&lt;span&gt;  // writes 3201 there&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt; another_c;&lt;/span&gt;&lt;span&gt; // allocates 1 byte in memory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;another_c &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;pointer_c;&lt;/span&gt;&lt;span&gt; /* goes to address 3201, finds value 97,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                           assigns this value to another_c */&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the implementations above, since we had not yet learned what pointers are, I made up a function called &lt;code&gt;get_byte_at&lt;/code&gt;. Here is the implementation of that function:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt; get_byte_at&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;char&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;addr&lt;/span&gt;&lt;span&gt;) {&lt;/span&gt;&lt;span&gt; // when we pass 1702 (foo's value),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  return&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;addr;&lt;/span&gt;&lt;span&gt; // returns 104 ('h')&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;memory-view&quot;&gt;Memory View&lt;/h2&gt;
&lt;p&gt;Since each byte in memory holds a value between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;256&lt;/code&gt;, we can represent these values as 2-digit numbers in hexadecimal. Most of the time, when debugging memory, tools are used that show each byte in hexadecimal.&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;CLion memory view&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; data-astro-image=&quot;constrained&quot; data-astro-image-pos=&quot;center&quot; width=&quot;1024&quot; height=&quot;697&quot; src=&quot;https://hamza-cskn.com/_astro/clion-memory-view.CebmzFHB.png&quot;&gt;
&lt;em&gt;Memory view feature of CLion IDE.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In the interface above, the real addresses of bytes are written on the left. As you can see, the address we assumed as &lt;code&gt;1702&lt;/code&gt; corresponds to huge numbers in the practical world.&lt;/p&gt;
&lt;p&gt;In the middle section, the hexadecimal numeric equivalents of each byte are written. For example in hexadecimal: &lt;code&gt;0x00007ffeecf07910&lt;/code&gt; byte has value &lt;code&gt;d0&lt;/code&gt; (&lt;code&gt;208&lt;/code&gt;) &lt;code&gt;0x00007ffeecf07911&lt;/code&gt; byte has value &lt;code&gt;79&lt;/code&gt; (&lt;code&gt;121&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;In the right section, the ASCII equivalents of these bytes are written. The problem is that not every ASCII character is printable. So non-printable ones are shown as &lt;code&gt;.&lt;/code&gt;. Thanks to this section, we can easily understand strings in memory.&lt;/p&gt;
&lt;p&gt;To understand how memory looks, I wrote a small memory debugger code in C. To try it, you can visit this &lt;a href=&quot;https://gist.github.com/hamza-cskn/aa1fb6c8c8c0e4c1a7356288e3c53898&quot;&gt;Memory Debugger&lt;/a&gt; page.&lt;/p&gt;
&lt;p&gt;We have reached the end of this article. I hope you now have an idea about how memory works, how data is stored, and what pointers are. In the next sections, we will write implementations of data structures and observe what changes in memory.&lt;/p&gt;
</content>
  </entry>
</feed>
