<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sujeet Pandey</title>
    <description>The latest articles on DEV Community by Sujeet Pandey (@sujeetpandey1204).</description>
    <link>https://dev.to/sujeetpandey1204</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3602996%2F1bc9850f-bc61-4875-bd4f-6de706828708.jpg</url>
      <title>DEV Community: Sujeet Pandey</title>
      <link>https://dev.to/sujeetpandey1204</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sujeetpandey1204"/>
    <language>en</language>
    <item>
      <title>Memory Leak in C++</title>
      <dc:creator>Sujeet Pandey</dc:creator>
      <pubDate>Wed, 31 Dec 2025 17:18:42 +0000</pubDate>
      <link>https://dev.to/sujeetpandey1204/memory-leak-in-c-515n</link>
      <guid>https://dev.to/sujeetpandey1204/memory-leak-in-c-515n</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Definition&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A memory leak occurs when a program allocates memory dynamically (using operators like new or functions such as malloc) but fails to release it back to the system after use. As a result, the reserved block of memory remains inaccessible to the program yet unavailable for future allocations. Over time, this wasted memory can accumulate, leading to reduced performance or even program crashes.&lt;/p&gt;

&lt;p&gt;In C++, there is no automatic garbage collection. It means that any memory that is dynamically allocated by the programmer needs to be freed after its usage manually by the programmer. If the programmer forgets to free this memory, it will not be deallocated till the program lives and will be unavailable to other processes. This is called memory leak.&lt;/p&gt;

&lt;h2&gt;
  
  
  How It Happens in C++
&lt;/h2&gt;

&lt;p&gt;1.Dynamic Allocation Without Deallocation&lt;br&gt;
         int* ptr = new int(10);&lt;br&gt;
        // Forgot to delete ptr&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here, memory is allocated for an integer, but since delete ptr; is missing, the memory remains occupied until the program terminates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Lost References&lt;br&gt;
      int* arr = new int[5];&lt;br&gt;
      arr = nullptr;&lt;br&gt;
      // Original pointer lost&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The pointer to the allocated block is overwritten, making it impossible to free the memory later.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Consequences-
&lt;/h2&gt;

&lt;p&gt;1.Gradual increase in memory usage during program execution.&lt;br&gt;
2.Slower performance due to reduced available memory.&lt;br&gt;
3.In severe cases, the operating system may terminate the program for exhausting resources.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prevention Strategies-
&lt;/h2&gt;

&lt;p&gt;1.Always pair new with delete and new[] with delete[].&lt;br&gt;
2.Use Smart Pointers (std::unique_ptr, std::shared_ptr). These automatically manage memory and reduce the risk of leaks.&lt;br&gt;
3.Adopt RAII (Resource Acquisition Is Initialization). Bind resource management to object lifetime so that memory is released when objects go out of scope.&lt;br&gt;
4.Employ Tools. Utilities like Valgrind or AddressSanitizer can detect leaks during testing.&lt;/p&gt;

</description>
      <category>memoryleak</category>
      <category>cpp</category>
      <category>computerscience</category>
      <category>coding</category>
    </item>
    <item>
      <title>Deadlock(OS) vs Deadlock(DBMS)</title>
      <dc:creator>Sujeet Pandey</dc:creator>
      <pubDate>Sun, 30 Nov 2025 18:33:23 +0000</pubDate>
      <link>https://dev.to/sujeetpandey1204/deadlockos-vs-deadlockdbms-4nog</link>
      <guid>https://dev.to/sujeetpandey1204/deadlockos-vs-deadlockdbms-4nog</guid>
      <description>&lt;h2&gt;
  
  
  &lt;em&gt;Deadlock in Operating Systems (OS)&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;What it means in OS&lt;/p&gt;

&lt;p&gt;Processes (or threads) request resources (mutexes, files, devices, memory pages). A deadlock occurs when processes each hold some resources and wait forever for resources others hold.&lt;/p&gt;

&lt;p&gt;Simple OS example (classic)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Process P1 locks resource A, then tries to lock resource B.&lt;/li&gt;
&lt;li&gt;Process P2 locks resource B, then tries to lock resource A.&lt;/li&gt;
&lt;li&gt;Both block waiting for the other → deadlock.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Detection (OS)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build a resource-allocation graph or wait-for graph and detect cycles.&lt;/li&gt;
&lt;li&gt;Cycle detection via DFS. If cycle exists → deadlock.&lt;/li&gt;
&lt;li&gt;Complexity: graph traversal O(V+E).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Prevention &amp;amp; Avoidance (OS)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prevention: disallow one Coffman condition (e.g., enforce ordering of resource acquisition, or disallow hold-and-wait by forcing processes to request all resources at once).&lt;/p&gt;

&lt;p&gt;Avoidance: Banker's algorithm (process claims max resources; OS grants only if system stays in a safe state). Works well only with fixed, known maximums.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recovery (OS)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Preemption: forcibly take resources (if safe) and roll back process.&lt;/li&gt;
&lt;li&gt;Kill one or more processes (victim selection: low priority, least work done).&lt;/li&gt;
&lt;li&gt;Rollback and restart.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Deadlock in DBMS (databases)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What it means in DBMS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Transactions acquire locks (row/page/table locks) to ensure isolation. Two (or more) transactions can form a cycle waiting for locks the others hold.&lt;/p&gt;

&lt;p&gt;DBMSs commonly use lock managers and wait-for graphs to detect deadlocks and resolve them automatically.&lt;br&gt;
DB-specific concepts&lt;/p&gt;

&lt;p&gt;Lock modes: Shared (S) vs Exclusive (X).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Granularity: row, page, table — coarser locks increase chance of contention.&lt;/li&gt;
&lt;li&gt;Two-Phase Locking (2PL): transactions first acquire, then release only after commit/rollback. Strict 2PL holds exclusive locks until commit to ensure serializability — but increases deadlock chance.&lt;/li&gt;
&lt;li&gt;Wait-for graph: DB creates waits-for graph of transactions; cycles mean deadlock.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Detection (DBMS)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DB periodically or on demand builds waits-for graph and detects cycles (pick a victim).&lt;/li&gt;
&lt;li&gt;Some systems use timeouts (if a transaction waits too long, abort it).&lt;/li&gt;
&lt;li&gt;Many RDBMS (MySQL/InnoDB, PostgreSQL) detect cycles and rollback one transaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Prevention &amp;amp; Avoidance (DBMS)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Timeouts: simpler but may abort long legitimate waits.&lt;/li&gt;
&lt;li&gt;Ordering: acquire locks in consistent order by key or index (e.g., always acquire locks by primary key order).&lt;/li&gt;
&lt;li&gt;Optimistic concurrency: versioning (MVCC) avoids many locks for readers.&lt;/li&gt;
&lt;li&gt;Deadlock prevention protocols:&lt;/li&gt;
&lt;li&gt;Wait-die and Wound-wait (timestamp-based):&lt;/li&gt;
&lt;li&gt;Wait-die: older transaction waits for younger; younger requesting older will die (abort).&lt;/li&gt;
&lt;li&gt;Wound-wait: older transaction preempts (wounds) younger (younger aborts); younger waits for older otherwise.&lt;/li&gt;
&lt;li&gt;Avoid locking when possible: shorter transactions, proper indexing, accessing rows in deterministic order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Recovery (DBMS)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On deadlock detection, DB chooses a victim (roll back that transaction). Criteria: minimal work lost, lowest priority, few locks, etc.&lt;/li&gt;
&lt;li&gt;After rollback, other transactions continue; victim can retry.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>computerscience</category>
      <category>database</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
