473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

unable allocating memory

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.

Jul 23 '05 #1
15 1448
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
Jul 23 '05 #2
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.

Jul 23 '05 #3
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
Jul 23 '05 #4
Because this classes is only part of the whole code, and using vector
only makes us more reliable on c++ library.

Jul 23 '05 #5
Mona schrieb:
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.
Jul 23 '05 #6
In message <11********************@f14g2000cwb.googlegroups.c om>, Mona
<pi*****@gmail.com> writes

[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.
So, I'm stuck in figuring out how to draw out the line.


--
Richard Herring
Jul 23 '05 #7
hm,....
depends on the task itself.
how large is the task i think, not really sure

Jul 23 '05 #8
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.

Jul 23 '05 #9
In message <11**********************@l41g2000cwc.googlegroups .com>, Mona
<pi*****@gmail.com> writes

[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.

--
Richard Herring
Jul 23 '05 #10
On Thu, 14 Apr 2005 02:21:58 -0700, Mona wrote:
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[i] and y[i] to assign the values to the array.

And delete them with delete [] x; etc.

Or use vector as others suggest... :)

- Jay

Jul 23 '05 #11
Jay Nabonne wrote:
Mona wrote:

[in a member function of myPoly]

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.


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++.

Jul 23 '05 #12
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[i]>>y[i];
}
for (int j = 0; j< n_count ; j++)
{
infile >>p[j];
}
infile.close();
}
but the infilex[i] and y[i] won't work, they keeps on giving me error.
I really dunno how to fix it anymore.
Jul 23 '05 #13
Lind schrieb:
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
Jul 23 '05 #14
Lind wrote:

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[i]>>y[i];
}
for (int j = 0; j< n_count ; j++)
{
infile >>p[j];
}
infile.close();
}
but the infilex[i] and y[i] 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."

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #15
On Thu, 14 Apr 2005 23:08:48 -0700, Lind wrote:
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...

Jul 23 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
11
by: Bob Karaban | last post by:
We ran into a problem using VirtualAllocEx and were wondering if anybody has a way around this. We have an executable that stores a hash table in a remote process. The VirtualAllocEx function...
4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
3
by: Erik S. Bartul | last post by:
lets say i want to fill up a multidimentional array, but i wish to allocate memory for it on the fly. i assume i declare, char **a; but how do i allocate memory for the pointers, so i can...
7
by: boss_bhat | last post by:
Hi all , I am beginner to C programming. I have a defined astructure like the following, and i am using aliases for the different data types in the structure, typedef struct _NAME_INFO {...
3
by: Tod Birdsall | last post by:
Hi All, The organization I am working for has created a new corporate website that used Microsoft's Pet Shop website as their coding model, and dynamically served up content, but cached each...
19
by: allanallansson | last post by:
Hi i would like some guidelines for a experiment of mine. I want to experiment with the swap and ctrl-z in linux. And for this i want to create a c program that allocates almost all the free memory...
3
by: Kane | last post by:
When you create node 1 you allocate memory and link it Again when you create node 2 you would allocate memory for that node in a different section of the code. Is there more efficient way where I...
10
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.