Reading new, filled array gives segfault
<p>I've been banging my head hard over this...I create a pointer in main(), which I pass on to another function. Inside that function, the pointer is used to create a new array (using the new operator), the array is filled, and the function ends. </p>

<p>If I then try to access elements in the new array in the caller, I get a segfault. Since the new operator was used, I expect the new array to be on the heap and thus not cleared by it going out of scope...I really don't get this. What am I overlooking? I also don't know precisely what to google for, so no luck there yet. </p>

<p>I can get it to work if I let the function return the pointer (instead of passing it), but I don't want to do that because eventually I'd like my function to create a few such newly created arrays. So what am I missing here? </p>

<p>Here is a minimal working example: </p>

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

bool getData(double *myData)
{
    myData = new double[2];
    if (!myData)
        return false;

    myData[0] = +4.53;
    myData[1] = -3.25;    
    return true;
}


int main()
{
    double *myData = NULL;
    if (!getData(myData))
        fprintf(stderr, "Could not get data.\n");

    std::cout &lt;&lt; myData[0] &lt;&lt; std::endl;
    std::cout &lt;&lt; myData[1] &lt;&lt; std::endl;

    delete [] myData;    
}
</code></pre>
