unable allocating memory

M

Mona

class myPoly
{
private:
int nv;
double *x, *y;
int *p;

public:
myPoly()
{
x = new double (0);
y = new double (0);
p = new int (0);
nv = 0;
}

myPoly(int n) // number of vertices
{
nv = n;
}

~myPoly()
{
delete x;
delete y;
delete p;
}

void Draw()
{
Read("poly.dat");
for ( int k = 0; k<nv ; k++)
{
int p2 = abs(*p);
if (p2 == nv)
{
for (int l = 0; l<nv;l++)
{
if (*p>0)
{

glBegin(GL_LINES);

glVertex2i(*x,*y);
}
}
glEnd();
}
}
}

void Read(const char s[]) // name of the datafile
{
ifstream infile;
infile.open(s);

int n_count;
double n_x,n_y;

infile>>n_count; //how many vertices

for (int i = 0; i < n_count; i++)
{
myPoly(i);
infile>>n_x>>n_y;
x = &n_x;
y = &n_y;
}
for (int j = 0; j< n_count ; j++)
{
infile >>p[j];
}
infile.close();
}
}

this is my class, and I wonder how come my pointer doesn't point to the
allocated memory?
the nv determines the allocation of memory of each x,y obtained from
the file.
 
R

Richard Herring

Mona said:
class myPoly
{
private:
int nv;
double *x, *y;
int *p;

public:
myPoly()
{
x = new double (0);
y = new double (0);
p = new int (0);
nv = 0;
}

myPoly(int n) // number of vertices
{
nv = n;

Short answer: this constructor doesn't initialise x, y and p.

Long answer: there are other errors in your class design, which indicate
confusion about the diference between new/delete and new[]/delete[].
You'd be much better off using std::vector for this kind of thing.

[snip]
 
M

Mona

well, I'm force to use classes and pointers because vector will slowing
the entire process.
So, I'm stuck in figuring out how to draw out the line.
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Mona said:
well, I'm force to use classes and pointers because vector will slowing
the entire process.
How can you tell ?
Did you profile it ?

Stefan
 
M

Mona

Because this classes is only part of the whole code, and using vector
only makes us more reliable on c++ library.
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Mona said:
Because this classes is only part of the whole code, and using vector
only makes us more
reliable on c++ library.

(You mean 'dependant on', right ?)

And going through all the hassle of doing the memory management yourself
is better ?

S.
 
R

Richard Herring

In message <[email protected]>, Mona

[please quote some context when replying, so we know what you're talking
about]
well, I'm force to use classes and pointers because vector will slowing
the entire process.

What makes you think so? How do you know?

What you are doing is what vector-based code would be doing anyway, but
with errors. There's no reason why using vector should be any slower.
 
M

Mona

Well, I try to discuss it but he requires me to use classes than
vector. Eventhough vector will be the easiest way to solve this problem.
 
R

Richard Herring

In message <[email protected]>, Mona

[please quote some context when you reply]
Because this classes is only part of the whole code, and using vector
only makes us more reliable on c++ library.
Yes, the standard C++ library is reliable. Use it whenever you can.
 
J

Jay Nabonne

myPoly()
{
x = new double (0);
y = new double (0);
p = new int (0);
nv = 0;
}

Here you allocate a single double for x and y.
void Read(const char s[]) // name of the datafile
{
ifstream infile;
infile.open(s);

int n_count;
double n_x,n_y;

The above n_x and n_y are automatic variables. They cease to exist when
Read returns.
infile>>n_count; //how many vertices

for (int i = 0; i < n_count; i++)
{
myPoly(i);
infile>>n_x>>n_y;
x = &n_x;
y = &n_y;
}

The above lines assign the address of the local variables to your
pointers. Your pointers now point to memory that will no longer be valid
once Read returns. And all your points are being read into the same
variables.
for (int j = 0; j< n_count ; j++)
{
infile >>p[j];
}
infile.close();
}
}

It seems that what you want to do is allocate x and y once you know how
many points you actually need to read (e.g. x = new double[n_count]). Then
use x and y to assign the values to the array.

And delete them with delete [] x; etc.

Or use vector as others suggest... :)

- Jay
 
O

Old Wolf

Jay said:
Mona wrote:
[in a member function of myPoly]
The above lines assign the address of the local variables to
your pointers. Your pointers now point to memory that will no
longer be valid once Read returns. And all your points are being
read into the same variables.

Also, the line "myPoly(i);" does nothing. Actually, it creates
a temporary myPoly object and then immediately destroys it,
causing undefined behaviour because the myPoly(int) constructor
leaves x,y uninitialized, but the destructor tries to delete them.

The OP looks like they are trying to call the constructor
as if it is a function; however this is not possible in C++.
 
L

Lind

well, I tried to change my class into this :
class myPoly
{
private:
int nv;
double *x, *y;
int *p;

public:
myPoly()
{
x = NULL;
y = NULL;
p = NULL;
nv = 0;
}

myPoly(int n) // number of vertices
{
nv = n;
}

~myPoly()
{
delete [] x; // realease the previously allocated arrays
delete [] y;
delete [] p;
}
void Read(const char s[]) // name of the datafile
{
ifstream infile;
infile.open(s);

int n_count;
//double n_x,n_y;
x = new double [n_count];
y = new double [n_count];

infile>>n_count; //how many vertices

for (int i = 0; i < n_count; i++)
{
myPoly(i);
infile>>x>>y;
}
for (int j = 0; j< n_count ; j++)
{
infile >>p[j];
}
infile.close();
}
but the infilex and y won't work, they keeps on giving me error.
I really dunno how to fix it anymore.
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Lind said:
well, I tried to change my class into this :
class myPoly
{
private:
int nv;
double *x, *y;
int *p;

public:
myPoly()
{
x = NULL;
y = NULL;
p = NULL;
nv = 0;
}

myPoly(int n) // number of vertices
{
nv = n;
}

You don't initialize x, y, and p here!
(No the other c'tor does not get called automagically)
~myPoly()
{
delete [] x; // realease the previously allocated arrays
delete [] y;
delete [] p;
}

But you delete x,y,p here => undefined behaviour.
void Read(const char s[]) // name of the datafile
{
ifstream infile;
infile.open(s);

int n_count;
//double n_x,n_y;
x = new double [n_count];
y = new double [n_count];

infile>>n_count; //how many vertices

for (int i = 0; i < n_count; i++)
{
myPoly(i);

This does nothing. Really!
See the post of 'Old Wolf'.
I really dunno how to fix it anymore.
Again: USE A STD::VECTOR !!
I think you should get a good C++ book and start reading a little bit.

Stefan
 
K

Karl Heinz Buchegger

Lind said:
void Read(const char s[]) // name of the datafile
{
ifstream infile;
infile.open(s);

You should check that the open succeeds, but that's ok for now.
int n_count;
//double n_x,n_y;
x = new double [n_count];
y = new double [n_count];

Quick question:
What value does n_count have at this position?
The answer is: nobody knows.
You create n_count and don't initialize it to anything.
But yet you use n_count to create that many doubles.
infile>>n_count; //how many vertices

Ahh. Here n_count is going to get a value.
But that's too late. The allocations have already been done.
for (int i = 0; i < n_count; i++)
{
myPoly(i);

That does nothing.
Well. It creates a temporary myPoly object, which gets initialized with
i. But at the next moment this temporary object gets destroyed. So
eventually it is a complicated way to do nothing.
infile>>x>>y;
}
for (int j = 0; j< n_count ; j++)
{
infile >>p[j];
}
infile.close();
}
but the infilex and y won't work, they keeps on giving me error.


Never say: It doesn't work. It gives me error.

Say: "I keep ketting this error <insert compiler message from compiler here>."
Or say: "It crashes at runtime. My debugger shows me that somewhere here
<insert line number and put in a reference such that we can identify
the line in the code> the problem happens."
 
J

Jay Nabonne

well, I tried to change my class into this :
void Read(const char s[]) // name of the datafile
{
ifstream infile;
infile.open(s);

int n_count;
//double n_x,n_y;
x = new double [n_count];
y = new double [n_count];

infile>>n_count; //how many vertices

Try allocating x and y based on n_count *after* you've read n_count...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top