Question: What happens here in terms of allocation ? is it faulty situation to use pointers like this?

```
void f(int p[])
{ 
  p = (int*)malloc(sizeof(int));
  *p = 0;
}

int main()
{
  int *q = 0;
  q = (int*)malloc(sizeof(int));
  *q = 1;
  f(q);
  return 0;
}

```

Answer: 
The short answer is that `p` and `q` are independent variables. So first `p` will be assigned the same value as `q` and then `p` gets a new value due to the `malloc`. `q` is not changed by the function call. However, there is a memory leak due to `p` (and `q`) not being freed.

You can see this using a few prints.

```
#include <stdio.h>
#include <stdlib.h>

void f(int p[]) 
{
    printf("--------------------\n");
    printf("p is now %p\n", (void*)p);
    printf("p points to the value %d\n", p[0]);
    p = (int*)malloc(sizeof(int)); 
    *p = 0; 
    printf("--------------------\n");
    printf("p is now %p\n", (void*)p);
    printf("p points to the value %d\n", p[0]);
    printf("--------------------\n");
}

int main(){
    int *q = 0;
    q = (int*)malloc(sizeof(int));
    *q = 1;
    printf("q is now %p\n", (void*)q);
    printf("q points to the value %d\n", q[0]);
    f(q);
    printf("q is now %p\n", (void*)q);
    printf("q points to the value %d\n", q[0]);
    return 0;
}

```

The output (with a few comments to explain):

```
q is now 0x1912010        // In main q is initialized
q points to the value 1   // In main the location q points to is initialized
--------------------
p is now 0x1912010        // In function p first has the same value as q
p points to the value 1   // so it also points to the same as q
--------------------
p is now 0x1913040        // In function p gets a new value due to malloc
p points to the value 0   // and the pointed to memory gets a new value
--------------------
q is now 0x1912010        // Back in main q is the same as before the function call
q points to the value 1   // and the pointed to memory is unchanged as well

```
whenever you ask for allocation on heap using malloc for example- you have to free it, if you don't you get a memory leak- so for 2 mallocs here you must use 2 frees.

And you need to keep in mind that when you send q to the function you send it BY VALUE
so if you check \*q in main it will still stay 1. 

if you want to change the value q is pointing to in the function you can send f(int \*\*). 

in your example a way to go if you want to change a where a pointer is pointing and to avoid memory leak is:

```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void f(int** p)
{
    free(*p); /*first free the initial main allocated memory on heap */
    *p = malloc(sizeof(int)); /*now the pointer is pointing to the new allocation */
    if(NULL == *p)
    {
       return;
    }
    **p = 0;
}

int main(){
    int *q = NULL;
    q = malloc(sizeof(int)); /*ask for memory allocation */
    if(NULL != q)
    {
       *q = 1;
       f(&q); 
       printf("%d\n", *q); /*now pointer is pointing to the allocationasked for by f */
       free(q); /*free the allocated place on heap */
     }
    return 0;
   }

```