0

So, I am new to pointers to in C.

I am facing a confusion. If I have,

int a;

Here, I dont allocate memory manually for a. It's done automatically by the compiler.

Now, if in a similar fashion, if I do,

char * a;

Do I need to allocate memory for the pointer?

Secondly, I made this code,

#include <stdio.h>
int main (void)
    {
        int *s=NULL;
        *s=100;
        printf("%d\n",*s);
        return 0;
    }

Why do I get a seg fault in this code? Is it because I havent allocated memory for the pointer? But as asked in the above question, I can simply declare it as well without manually allocating the memory.

PS: I am new to pointers and I am facing confusion in this. Spare me if it is a bad question. Thanks.

Edit: I read the post for malloc on SO.

http://stackoverflow.com/questions/1963780/when-should-i-use-malloc-in-c-and-when-dont-i

It doesnt really solve my doubt.

user3797829
  • 383
  • 3
  • 15
  • 4
    You don't need to allocate space for the pointer, but for the _pointee_. – mafso Jul 20 '14 at 08:13
  • `int a` is placed on the stack, but NOT by the compiler. This is done by the system. `char * a` creates a memory address that may or may not point to good memory. You __MUST__ malloc this memory to prepare it for reading/writing. Otherwise you are venturing into unedfined behavior land. your seg fault is caused by using memory before it is ready to be used. – Mike McMahon Jul 20 '14 at 08:13
  • One is allocating memory and the other is what does the variable associated with that memory point to. So, when I declared `char * a` , the system just allocates 4 bytes and names that memory as `a` but it is not initialised to any location. Am I right in saying this? – user3797829 Jul 20 '14 at 08:16
  • Read wikipage on [C dynamic memory allocation](http://en.wikipedia.org/wiki/C_dynamic_memory_allocation) and take several hours to read more about C programming. – Basile Starynkevitch Jul 20 '14 at 08:17

3 Answers3

6

You don't need to allocate memory for the pointer itself. That's automatic, like the int in your first code snippet.

What you do need to allocate is the memory that the pointer should point to, and you need to initialize the pointer to point to that.

Since you're not allocating any space, the *s= assignment is undefined behavior. s itself (the pointer) is allocated, and initialized to NULL. You can't dereference (*s - look at what the pointer points to) a null pointer.

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

int main (void)
{
    int *s = NULL; // s is created as a null pointer, doesn't point to any memory
    s = malloc(sizeof(int)); // allocate one int's worth of memory
    *s = 100;                // store the int value 100 in that allocated memory
    printf("%d\n",*s);       // read the memory back
    free(s);                 // release the memory
                             // (you can't dereference s after this without
                             //  making it point to valid memory first)
    return 0;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
  • Perfect. I got it. What I am trying to do is store the value `100` by dereferencing the `NULL` pointer which is actually wrong. Thanks a lot :). – user3797829 Jul 20 '14 at 08:19
  • thanks for answering such a basic question. I know it was a really bad doubt. Thanks again. :) – user3797829 Jul 20 '14 at 08:20
2

You have to allocate memory for the variable s you are declaring. Malloc is a nice way to do that. With int *s=NULL the pointer does not point to any address. And after that you trying to give a value to that address (*s=100;). If you do not want to allocate memory manualy (with malloc) you simply declare an int variable and then make s pointing to that variable.

With malloc:

  #include <stdio.h> int main (void)
     {
         int *s=NULL;
         s=(int *)malloc(sizeof(int));
         *s=100;
         printf("%d\n",*s);
         return 0;
     }

Without malloc:

#include <stdio.h>
int main (void)
    {
        int *s=NULL;
        int var;
        s=&var;
        *s=100;
        printf("%d\n",*s);//100
        printf("%d\n",var);//also 100
        return 0;
    }
csdinos
  • 197
  • 2
  • 6
1

As pointers are also special type of variables which stores the address of other variables,As the address is also some value(number), Hence memory is also needed to store this address,so compiler automatically allocates memory(exactly 4 bytes on 32-bit machine) to a pointer variable,when you are allocating the memory using malloc() or calloc()your are actually allocating the memory to the data which is to be stored,and simply assigning the starting address of this memory to the pointer.

In your example at the line

int *s=NULL; //this line

Compiler actually allocates memory of 4 bytes (if your machine is 32-bit)

see this small snippet

int main(void)
{
  int *s=NULL;
  printf("%d\n",sizeof(s)); //Will output 4 if you are using is 32-bit OS or else 2 if you are using using 16 bit OS.
  return 0;
}

And NULL is simply just another way of assigning Zero to a pointer.So technically your actually using 4 bytes to store zeros in that pointer. And don't be confused, assigning zero or NULL means that the pointer is not pointing to any data(as it doesn't hold a valid address,Zero is not valid address).

Mysterious Jack
  • 621
  • 6
  • 18