• Advertisement
Sign in to follow this  

Loading Files. Memory Alloc Error

This topic is 2196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code gives me some sort of Memory Allocation Error when I compile it with VS2010 Express.


#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
struct Vector {float x; float y; float z;};
int main()
{
Vector *a;
int n;
ifstream file;
char data[50];
file.open("file.txt");
if(file.is_open())
{
while(!file.eof())
{
file >> data;
n++;
a = new Vector[n];
a->x = atof(data);
}
}
file.close();
return 0;
}

Share this post


Link to post
Share on other sites
Advertisement

This code gives me some sort of Memory Allocation Error when I compile it with VS2010 Express.


...
Vector *a;
int n;
ifstream file;
char data[50];
file.open("file.txt");
if(file.is_open())
{
while(!file.eof())
{
file >> data;


This will explode if the file happens to contain a string with more than 49 consecutive non-white-space characters. Use an std::string instead of a char array.



...
n++;

[/quote]
You have not initialised n. It's common for compilers to give variables default values of 0 in debug builds, but in optimized builds, they will contain garbage. IMHO, it's a very good habit to initialise absolutely everything.



a = new Vector[n];

[/quote]
You aren't delete[]-ing 'a' anywhere. You have one memory leak per loop iteration. Use std::vector<Vector>.



a->x = atof(data);

[/quote]
Why not use the stream to read in to Vector members directly, rather than going through an intermediate character buffer?

This is what I think you're trying to do:

// caveat: untested/uncompiled
#include <fstream>
#include <vector>

struct Vector
{
Vector() : x(0), y(0), z(0) { };
float x,y,z;
};

int main()
{
std::ifstream f("file.txt", std::ios::binary);
std::vector<Vector> a;

Vector v;
while ((f >> v.x >> v.y >> v.z))
a.push_back(v);

return 0;
}

Share this post


Link to post
Share on other sites
I fixed my code a little, but now it crashes when I run it. No Errors.


Vector *a;
int n = 0;
ifstream file;
char data[50];
file.open("file.txt");
if(file.is_open())
{
while(!file.eof())
{
file >> data;
n++;
a = new Vector[n];
a[n].x = atof(data);
}
}
file.close();
delete[] a;
return 0;

Share this post


Link to post
Share on other sites
You are still leaking the Vector* memory each iteration and just delete the last one allocated. You are still running into unhealthy undefined behavior when reading a line which contains more than 49 non-whitespace characters in a row. I suggest you take the advice edd² gave you to heart, he already offered advice on how to fix these problems and others.

Share this post


Link to post
Share on other sites
One problem in your current code is that you call delete[] a even if a has not been initialised, for example if the file is not found where you are expecting it. Another problem is that you access a[n] in a dynamic array of size n. For an array of N elements, the valid indices are between 0 and N - 1.

Is there any reason you chose to discard Edd[sup]2[/sup]'s advice?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement