title: Invalid Pointer Error when using free()
body: <p>I am writing a Python Extension in C (on Linux (Ubuntu 14.04)) and ran into an issue with dynamic memory allocation. I searched through SO and found several posts on free() calls causing similar errors because free() tries to free memory that is not dynamically allocated. But I don't know if/how that is a problem in the code below:</p>
  
  <pre><code>#include &lt;Python.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  static PyObject* tirepy_process_data(PyObject* self, PyObject *args)
  {
      FILE* rawfile = NULL;
      char* rawfilename = (char *) malloc(128*sizeof(char));
      if(rawfilename == NULL)
           printf("malloc() failed.\n");
      memset(rawfilename, 0, 128);
      const int num_datapts = 0; /* Just  Some interger variable*/
  
      if (!PyArg_ParseTuple(args, "si", &amp;rawfilename, &amp;num_datapts)) {
            return NULL;
      }
  
      /* Here I am going top open the file, read the contents and close it again */
  
      printf("Raw file name is: %s \n", rawfilename);
      free(rawfilename);
      return Py_BuildValue("i", num_profiles);
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Raw file name is: \home\location_to_file\
  *** Error in `/usr/bin/python': free(): invalid pointer: 0xb7514244 ***
  </code></pre>