Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>int * is a pointer, which can be dereferenced. int ** is a pointer to a pointer, which can be dereferenced twice. So what does this mean? Well, a pointer is nothing more than an integer. Memory addresses just start from zero to its max range. For 32-bit systems, its range of addressable memory is 0 to 2^32-1 (or 4294967295). Each of this address has a byte value. If you have an int *, then it will access the address 4-bytes at a time. Also, for simplicity, let's assume this is a virtual address, you can't just access all this memory, some will be protected (system), some are not valid (not committed). To gain more memory you can ask the system to allocate more from this range. sbrk in Linux, VirtualAlloc in Windows but you will be accessing them usually through C's malloc or calloc.</p> <p>Let's say, you have starting from 0x100: 0x100: 'h', 'e', 'l', 'l', 'o', '\0'</p> <p>So this string, occupies memory from 0x100 to 0x105 (including the null terminator). If you have a pointer:</p> <p>char *p = 0x100;</p> <p>Then you have:</p> <pre><code>p // 0x100 *p // 'h' p+1 // 0x101 *(p+1) // 'e' p += 2 // 0x102 *p // 'l' p = 0x200; p // now points to 0x200 in memory *p // contains whatever value is in 0x200 </code></pre> <p>If you have int pointers, then you are accessing memory 4-bytes at a time (or however big an int is on your system).</p> <p>So with all that background, when you run calloc, it returns the address of the block you've requested.</p> <pre><code>int *memoryBlock = calloc(count, sizeof(int)); // memoryBlock is an array of int, assuming sizeof(int) == 4, then // you have 40 bytes of memory starting from the address of what is // returned by calloc. memoryBlock++; // now memoryBlock is at base address + 4 *memoryBlock = 10; // now that address contains the value 10 (*memoryBlock)++; // now that address contains the value 11 memoryBlock++; // now memoryBlock is 4 bytes further </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