Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ul> <li><code>int *a;</code> This defines a variable which is a pointer to an integer type. The pointer type variable <code>a</code> at the creation contains garbage value.</li> <li>When you do <code>*a = 10;</code> it ties to use the value stored in <code>a</code> , which is garbage, as an address and store the value <code>10</code> there. Because we do not know what <code>a</code> contains and it is not allocated so <code>a</code> points to some memory location which is unknown and accessing it will be illegal, and will get you a segmentation fault (or something similar).</li> <li>Same in the case of <code>printf ("%d", *a);</code> . This also tries to access the value stored at some undefined memory location which you have not allocated.</li> </ul> <p>.</p> <pre><code>this variable is this is the location stored in some with the address address on the 'garbage'. You do not stack have permissions to access this +-------+---------+ | name | value | +-------+---------+ +---------+ | a | garbage |----&gt;| ????? | +-------+---------+ +---------+ </code></pre> <p>After you have defined the pointer type variable, you need to request for some memory location from the operating system and use that memory location value to store into <code>a</code>, and then use that memory location through <code>a</code>.</p> <p>To do that you need to do the following:</p> <pre><code>int *a; a = malloc (sizeof (int)); /* allocates a block of memory * of size of one integer */ *a = 10; printf ("%d", *a); free (a); /* You need to free it after you have used the memory * location back to the OS yourself. */ </code></pre> <p>In this case it is like below:</p> <p>After <code>*a = 10;</code> . The pointer variable is allocated in the stack. At this moment the <code>a</code> contains a garbage value. Then <code>a</code> points to an address with that garbage value.</p> <pre><code>this variable is this is the location stored in some with the address address on the 'garbage'. You do not stack have permissions to access this +-------+---------+ | name | value | +-------+---------+ +---------+ | a | garbage |----&gt;| ????? | +-------+---------+ +---------+ </code></pre> <p>After <code>a = (int *) malloc (sizeof (int));</code> . Let us assume that <code>malloc</code> returns you some address <code>0x1234abcd</code>, to be used. At this moment <code>a</code> will contain <code>0x1234abcd</code> then <code>a</code> points to a valid memory location which was allocated and reserved for you to be used. But note that the value inside <code>0x1234abcd</code> can be anything, ie. garbage. You can use <code>calloc</code> to set the contents of the memory locations you allocate to <code>0</code>.</p> <pre><code>this variable is this is the location stored in some 0x1234abcd , allocated address on the by malloc, and reserved stack for your program. You have access to this location. +-------+------------+ | name | value | +-------+------------+ +---------+ | a | 0x1234abcd |----&gt;| garbage| +-------+------------+ +---------+ </code></pre> <p>After <code>*a = 10;</code> , by <code>*a</code> you access the memory location <code>0x1234abcd</code> and store <code>10</code> into it.</p> <pre><code>this variable is this is the location stored in some 0x1234abcd , allocated address on the by malloc, and reserved stack for your program. You have access to this location. +-------+------------+ | name | value | +-------+------------+ +---------+ | a | 0x1234abcd |----&gt;| 10 | +-------+------------+ +---------+ </code></pre> <p>After <code>free (a)</code> , the contents of <code>a</code> ie. the memory address <code>0x1234abcd</code> will be freed, ie returned back to the operating system. Note that after freeing the <code>0x1234abcd</code> the contents of <code>a</code> is <em>still</em> <code>0x1234abcd</code> , but you can no more access it legally, because you just freed it. Accessing the contents pointed by the address stored in <code>a</code> will result in undefined behavior, most probably a segmentation fault or heap corruption, as it is freed and you do not have access rights.</p> <pre><code>this variable is this is the location stored in some 0x1234abcd , allocated address on the by malloc. You have freed it. stack Now you CANNOT access it legally +-------+------------+ | name | value | +-------+------------+ +---------+ | a | 0x1234abcd | | 10 | +-------+------------+ +---------+ the contents of a remains the same. </code></pre> <p><strong>EDIT1</strong></p> <p>Also note the difference between <code>printf ("%d", a);</code> and <code>printf ("%d", *a);</code> . When you refer to <code>a</code> , it simply prints the contents of <code>a</code> that is <code>0x1234abcd</code>. And when you refer <code>*a</code> then it uses <code>0x1234abcd</code> as an address , and then prints the contents of the address, which is <code>10</code> in this case.</p> <pre><code>this variable is this is the location stored in some 0x1234abcd , allocated address on the by malloc, and reserved stack for your program. You have access to this location. +-------+------------+ | name | value | +-------+------------+ +---------+ | a | 0x1234abcd |----&gt;| 10 | +-------+------------+ +---------+ ^ ^ | | | | (contents of 'a') (contents of the ) | (location, pointed ) printf ("%d", a); ( by 'a' ) | +----------------+ | printf ("%d", *a); </code></pre> <p><strong>EDIT2</strong></p> <p>Also note that <code>malloc</code> can fail to get you some valid memory location. You should always check if <code>malloc</code> returned you a valid memory location. If <code>malloc</code> cannot get you some memory location to be used then it will return you <code>NULL</code> so you should check if the returned value is <code>NULL</code> or not before use. So finally the code becomes:</p> <pre><code>int *a; a = malloc (sizeof (int)); /* allocates a block of memory * of size of one integer */ if (a == NULL) { printf ("\nCannot allocate memory. Terminating"); exit (1); } *a = 10; printf ("%d", *a); free (a); /* You need to free it after you have used the memory * location back to the OS yourself. */ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload