I get SIGABORT when running this code.
I discovered that I am suppose to use square brackets but why the behavior is same.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double * dp = new double (5); // what do round brackets mean, and why the behvior
std::ifstream fid("testdata.txt");
fid >> dp[0] >> dp[1] >> dp[2] >> dp[3] >> dp[4];
fid.close();
}
Content of “testdata.txt” is:
4.0 5. 6. 6. 8. 7. 952.
This error happens only when there is 5 or more doubles in file.
Change
to
The first syntax creates one double on the heap and sets it to 5. The second syntax creates an array of five doubles.
Either way, it is a good practice to delete
dpwhen you’re done with it. In the first case, the correct syntax isdelete dp;and in the second,delete[] dp;.