无法分配内存 [英] unable allocating memory

查看:78
本文介绍了无法分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)//顶点数

{

nv = n;

}


~myPoly()

{

删除x;

删除y;

删除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读取(const char s [])//数据文件的名称

{

ifstream infile;

infile.open(s);


int n_count;

double n_x,n_y;


infile>> n_count; //多少个顶点


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();

}

}


这是我的班级,我想知道我的指针怎么没有指向

分配的内存?

nv确定分配每个x,y的内存从

获得的文件。

解决方案

在消息< 11 **********************@l41g2000cwc.googlegroups .com> ;, Mona

< pi ***** @ gmail。 COM>写道

class myPoly
{
私有:
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)//顶点数
{
nv = n;


简答:这个构造函数没有初始化x,y和p。


答案很长:你班上还有其他错误设计,表明

关于new / delete和new [] / delete []之间的差异的混淆。

使用std :: vector你会好得多对于这种事情。


[snip]}

这是我的班级,我想知道我的指针怎么没有指向<分配内存?
nv确定从文件中获取的每个x,y的内存分配。




-

Richard Herring


好吧,我强迫使用类和指针,因为矢量会减慢

整个过程。

所以,我不知道如何划清界限。


Mona schrieb:< blockquote class =post_quotes>好吧,我强迫使用类和指针,因为矢量会减慢整个过程。



你怎么知道?

你有没有把它描出来?


Stefan


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.

解决方案

In message <11**********************@l41g2000cwc.googlegroups .com>, Mona
<pi*****@gmail.com> writes

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] }
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.



--
Richard Herring


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.


Mona schrieb:

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


这篇关于无法分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆