Confusion with malloc through function argument
<p>Today I was trying to code something and i found my self at some point that i need to allocate a block of memory needed for a pointer. The program was OK and checked with <code>valgrind</code> was also OK.</p>

<p>Some how after I started to review the whole code i realized that what i did, was that the memory was allocated for <code>int a</code> which is not a pointer.</p>

<p>I'm a person which learn <code>C</code> just for fun, I read some books and hundreds of Tutorials, but i never saw any mention about something like this so I really need an Explanation here.</p>

<p>I tried to make a small program to explain the context:</p>

<pre><code>#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;

void createMe(int *a);
void freeMe(int *b);

int main(void){
    int a;
    createMe(&amp;a);
}

void createMe(int *ptr){
    ptr = malloc(256);
    *ptr = 10;
    printf("A = %d\n",*ptr);
    freeMe(ptr);
}

void freeMe(int *ptr){
    free(ptr);
}
</code></pre>

<p>Here i have <code>int a</code> and i passed its address in <code>createMe(&amp;a);</code></p>

<p>The create function takes a pointer as argument so i had to use <code>&amp;a</code>.
Now comes the strange part for me:</p>

<p>inside <code>createMe();</code> i call <code>malloc</code> on that pointer <code>ptr</code> which is the argument of the function.
Where exactly it is that memory block added/used in the pointer which is the argument of <code>create()</code> function or it sent to <code>a</code> inside main?</p>

<p>As far as I read/learn until now:</p>

<p>1) <code>a</code> doesn't get that memory block because <code>a</code> is an <code>int</code> and not an <code>int*</code>.</p>

<p>2) does the function argument <code>ptr</code> get that memory block`?, if so, how comes that i never read something like that.</p>

<p>I used <code>Linux mint 17.3</code> with <code>GCC-4.8.5</code> and <code>GCC-5.2.0</code></p>

<p>Here is the result of <code>Valgrind</code>:</p>

<pre><code>==6793== Memcheck, a memory error detector
==6793== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==6793== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==6793== Command: ./program
==6793== 
A = 10
==6793== 
==6793== HEAP SUMMARY:
==6793==     in use at exit: 0 bytes in 0 blocks
==6793==   total heap usage: 1 allocs, 1 frees, 256 bytes allocated
==6793== 
==6793== All heap blocks were freed -- no leaks are possible
==6793== 
==6793== For counts of detected and suppressed errors, rerun with: -v
==6793== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
</code></pre>

<p>As you can see the memory allocation takes place and <code>free</code> takes place too.</p>

<p>And the <code>GCC Flags</code> are:</p>

<pre><code>-Wall -Wextra -Werror -Wstrict-prototypes -Wconversion -Wmissing-prototypes -Wold-style-definition -std=c11 -O0 -g
</code></pre>