title: How does the malloc function work in C?
body: <p>My goal is to read a file and store it's contents in a char array given the offset and number of bytes to be read. I have written a function for doing the same and it works fine.</p>
  
  <p>Now this function is to be called from somewhere else. So I am trying to declare a variable <code>char * data</code> which will hold the contents returned by the above mentioned function. After declaring I tried to allocate it some memory. (I know how much, as I specify the number of bytes to be read). It goes as follows:</p>
  
  <pre><code>char * data;
  char * filename = "alphabet.txt";
  int data_size = 10;
  printf("data size: %d\n", data_size);
  data = (char*) malloc (data_size);
  printf("Size allocated to data: %d\n",sizeof(data));
  return 0;
  </code></pre>
  
  <p>This code gives the following output:</p>
  
  <pre><code>data size: 10
  Size allocated to data: 8
  </code></pre>
  
  <p>I don't understand this behavior. Can somebody please explain it to me.</p>
  
  <p>Thanks a lot</p>
  
  <p>shahensha</p>