<?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: Anuj Raghuwanshi</title>
    <description>The latest articles on DEV Community by Anuj Raghuwanshi (@anuj_raghuwanshi_).</description>
    <link>https://dev.to/anuj_raghuwanshi_</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%2F1997787%2F2f33f7f7-e3e5-47ad-a2f8-a82297bdf860.png</url>
      <title>DEV Community: Anuj Raghuwanshi</title>
      <link>https://dev.to/anuj_raghuwanshi_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anuj_raghuwanshi_"/>
    <language>en</language>
    <item>
      <title>Dynamic Memory Allocation | Memory Management in C</title>
      <dc:creator>Anuj Raghuwanshi</dc:creator>
      <pubDate>Sun, 08 Sep 2024 06:59:39 +0000</pubDate>
      <link>https://dev.to/anuj_raghuwanshi_/dynamic-memory-allocation-memory-management-in-c-1bn</link>
      <guid>https://dev.to/anuj_raghuwanshi_/dynamic-memory-allocation-memory-management-in-c-1bn</guid>
      <description>&lt;p&gt;&lt;strong&gt;A statically allocated variable or array has a fixed size in memory.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dynamic Memory Allocation is a way in which the size of a data structure can be changed during the runtime.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory assigned to a program in a typical architecture can be broken down into four segments:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Code&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;- Static/global variables&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;- Stack&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;- Heap&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dynamic memory allocation takes place in Heap.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Static memory allocation takes place in Stack.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functions for dynamic memory allocation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Malloc&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Calloc&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Realloc&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. Free&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  malloc() stands for memory allocation.
&lt;/h2&gt;

&lt;p&gt;It reserves a block of memory with the given amount of bytes,sizeof operator is used.&lt;br&gt;
&lt;code&gt;Syntax:&lt;br&gt;
ptr = (ptr-type*) malloc(size-in-bytes);&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6twhejuvl6j0n9dgjsbp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6twhejuvl6j0n9dgjsbp.png" alt="Image description" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The return value is a void pointer to the allocated space.&lt;br&gt;
Therefore, the void pointer needs to be casted to the appropriate type as per the requirements.&lt;br&gt;
However, if the space is insufficient, allocation of memory fails and it returns a NULL pointer.&lt;br&gt;
All the values at allocated memory are initialized to garbage values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;

int main() {
    int *ptr;
    int n;

    printf("Enter size: \n");
    scanf("%d", &amp;amp;n);

    ptr = (int*) malloc(n * sizeof(int));

    for (int i = 0; i &amp;lt; n; i++) {
        printf("Enter the value no %d of array\n", i);
        scanf("%d", &amp;amp;ptr[i]);
    }

    for (int i = 0; i &amp;lt; n; i++) {
        printf("The value at %d is %d\n", i, ptr[i]);
    }

    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  calloc() stands for contiguous allocation.
&lt;/h2&gt;

&lt;p&gt;It reserves n blocks of memory with the given amount of bytes.&lt;br&gt;
It initializes each block with a default value ‘0’.&lt;br&gt;
It has two parameters or arguments as compare to malloc().&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fii7dmdp1qnnwy2yo050s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fii7dmdp1qnnwy2yo050s.png" alt="Image description" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The return value is a void pointer to the allocated space.&lt;br&gt;
Therefore, the void pointer needs to be casted to the appropriate type as per the requirements.&lt;br&gt;
However, if the space is insufficient, allocation of memory fails and it returns a NULL pointer.&lt;br&gt;
&lt;code&gt;Syntax:&lt;br&gt;
ptr = (ptr-type*) calloc(n, size-in-bytes);&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  realloc() stands for reallocation.
&lt;/h2&gt;

&lt;p&gt;If the dynamically allocated memory is insufficient, we can change the size of previously allocated memory using realloc() function.&lt;br&gt;
&lt;code&gt;Syntax:&lt;br&gt;
ptr = (ptr-type*) realloc(ptr, new-size-in-bytes);&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2o1qnhn23juturgpyuif.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2o1qnhn23juturgpyuif.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  free()
&lt;/h2&gt;

&lt;p&gt;free is used to free the allocated memory.&lt;br&gt;
If the dynamically allocated memory is not required anymore, we can free it using free function.&lt;br&gt;
This will free the memory being used by the program in the heap.&lt;br&gt;
&lt;code&gt;Syntax:&lt;br&gt;
free(ptr)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6wa1tnso4avzguu8j6gg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6wa1tnso4avzguu8j6gg.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>STORAGE CLASSES IN C</title>
      <dc:creator>Anuj Raghuwanshi</dc:creator>
      <pubDate>Thu, 29 Aug 2024 20:09:15 +0000</pubDate>
      <link>https://dev.to/anuj_raghuwanshi_/storage-classes-in-c-4ib2</link>
      <guid>https://dev.to/anuj_raghuwanshi_/storage-classes-in-c-4ib2</guid>
      <description>&lt;h2&gt;
  
  
  Hello everyone, This is my first post to dev.to in which I wanna tell you about STORAGE CLASSES IN C..
&lt;/h2&gt;

&lt;p&gt;Storage classes in C are a set of keywords that guide the compiler in storing and manipulating variables in memory.&lt;br&gt;
&lt;strong&gt;A Storage Class defines following attributes about a variable in C.&lt;/strong&gt;&lt;br&gt;
1.Scope :- Where will this variable be available.&lt;br&gt;
2.Default initial value :- what will be the initial value&lt;br&gt;
3.LifeTime :- Life of that variable in a program&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are FOUR Storage Classes in C which are most often used:&lt;/strong&gt;&lt;br&gt;
1.&lt;u&gt;Automatic&lt;/u&gt;&lt;br&gt;
2.&lt;u&gt;External&lt;/u&gt;&lt;br&gt;
3.&lt;u&gt;Static&lt;/u&gt;&lt;br&gt;
4.&lt;u&gt;Register&lt;/u&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Automatic Storage Class
&lt;/h2&gt;

&lt;p&gt;A variable defined without any Storage Class specification is by default an automatic variable.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int variable_name and auto int variable_name are same.&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scope :- Local to the function body they are defined in&lt;br&gt;
Default Value :- Garbage value&lt;br&gt;
LifeTime :- Till the end of the function block they are defined in.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  External Storage Class
&lt;/h2&gt;

&lt;p&gt;They are same as global variable. External storage class in C, represented by the keyword 'extern', is used to give a reference of a global variable that is visible to all program files. When a variable is declared with the 'extern' keyword, it indicates that the variable is defined in another file and the current file is using its reference.&lt;br&gt;
Extern variables cannot be initialised. They only point to an existing variable.&lt;br&gt;
&lt;code&gt;extern int variable_name&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scope :- Global to the program they are defined in&lt;br&gt;
Default Value :- zero&lt;br&gt;
LifeTime :- They are available throughout the lifetime of the program&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Static Storage Class
&lt;/h2&gt;

&lt;p&gt;Static storage class in C, represented by the keyword 'static'.&lt;br&gt;
&lt;code&gt;static int variable_name&lt;/code&gt;&lt;br&gt;
static variable retains its value between function calls.Static variables are initialised only once.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scope :- Local to the block they are defined in&lt;br&gt;
Default Value :- zero&lt;br&gt;
LifeTime :- throughout the program&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Register Storage Class
&lt;/h2&gt;

&lt;p&gt;Register variable requests the compiler to store the variable in the CPU register instead of storing it in the memory to have faster access.&lt;br&gt;
&lt;code&gt;register int variable_name&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scope :- Local to the function they are defined in&lt;br&gt;
Default Value :- Garbage Value&lt;br&gt;
LifeTime :- Available till the end of the function block in which the variable is defined.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
  </channel>
</rss>
