473,385 Members | 1,630 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,385 software developers and data experts.

confusion..

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();
}
}
}
}
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
7 1143
Mona wrote:

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();
}
}
}
}
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.
Well, nothing in your code gives a hint of what you actually want to do.
From the part you posted, everything seems to suggest that this
class wants to hold only *one* x/y coordinate.

.... x = new double (0); ....
This allocates *1* double, and initializes it with 0.0

.... delete x; ....
This deletes *1* double

.... glVertex2i(*x,*y); ....
This puts *1* point coordinate into OpenGL. Since neither x nor y get altered anywhere
you do this a number of times (dependend on the exact values of nv and *p

.... Read("poly.dat");

....
You don't show the implementation of that, but I guess that it does some memory allocations.
So when asking the question: Why your pointer doesn't point to the allocated memory, you
should look in this function, because that function will have to allocate the memory
when the file gets read.

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

public:
myPoly()
{
x = NULL;
y = NULL;
p = NULL;
nv = 0;
}
void myPoly(int n) // number of vertices
{
delete [] x; // realease the previously allocated arrays
delete [] y;
delete [] p;

// and allocate new ones, which reflect the required number of vertices
nv = n;

x = new double [ nv ];
y = new double [ nv ];
p = new int [ nv ];

// better initialize the arrays here with some valid values. What that values
// are is up to you.
}

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

void Draw()
{
// I could not figure out, what exactly your original draw function wants do
// to. So I wrote a new one which most likely does what I think you want to do
//
// Also: Why a function "Draw" has to read some data from a file is not understandable
//
// Read("poly.dat");

glBegin(GL_LINES);
for ( int k = 0; k<nv ; k++)
{
glVertex2i( x[k], y[k] );
}
glEnd();
}
};
From the posted snippets it seems that your experience with dynamic allocation is
very weak. So why don't you use std::vector for all of that and safe yourself the
hassle of dynamic allocations until your C++ knowlege gets better and you actually
have no other choice then to go through all of this.

Also note:
There are literally a gazillion things that can go wrong when doing graphics.
So trying to learn C++ and doing graphics at the same time isn't a very good idea.
May I suggest a step-by-step plan.
You first step is: read the polygon and output the numbers you read from the file
in plain text through std::cout. In this way you can easily check if the read code
works as expected, if your memory management is correct and things like that.
Only after that works perfectly switch to OpenGL. In this way you know that
when you don't see anything in the graphics output that it was not because of
you not reading the file correctly or that something is wrong in the memory management
and so on. Then you can concentrate on the graphics aspect of your program: Is
OpenGL initialized correctly? Do I have a valid drawing space? Is the coordinate
system setup correctly? Do I draw outside the drawing space? Do I draw with black
ink on a black piece of paper? Things like that.

You need to learn to walk before you can learn to run. Doing graphics is not
only running, but is running the New York marathon.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
Karl Heinz Buchegger wrote:

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

public:
myPoly()
{
x = NULL;
y = NULL;
p = NULL;
nv = 0;
}
void myPoly(int n) // number of vertices
{
delete [] x; // realease the previously allocated arrays
delete [] y;
delete [] p;


<snip>

Karl, usually I like your posts, but this one, I simply don't
understand. Why delete members in a *constructor*?

--
Peter
Jul 23 '05 #3
Peter Kragh wrote:
Karl Heinz Buchegger wrote:

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

public:
myPoly()
{
x = NULL;
y = NULL;
p = NULL;
nv = 0;
}
void myPoly(int n) // number of vertices
{
delete [] x; // realease the previously allocated arrays
delete [] y;
delete [] p;

<snip>

Karl, usually I like your posts, but this one, I simply don't
understand. Why delete members in a *constructor*?


With all due respect to both o'yous, it's not a constructor. Constructors
don't have return value types...

I wonder what Karl wanted to demonstrate (and whether it actually *was*
Karl posting that "code"). The use of assignment instead of initialiser
lists is another indicator of a rather strange behavior of *our* Karl,
which might suggest something's wrong there...

V
Jul 23 '05 #4
Victor Bazarov wrote:

With all due respect to both o'yous, it's not a constructor. Constructors
don't have return value types...


Missed that one. However, it made me wonder. Is it legal to name a
member function the same name as the class? I searched the standard, but
couldn't find any limitations.

I tried to compile it, and both my compilers choked on it. Comments?

--
Peter
Jul 23 '05 #5
Peter Kragh wrote:
Victor Bazarov wrote:

With all due respect to both o'yous, it's not a constructor.
Constructors
don't have return value types...

Missed that one. However, it made me wonder. Is it legal to name a
member function the same name as the class? I searched the standard, but
couldn't find any limitations.

I tried to compile it, and both my compilers choked on it. Comments?


A member function that has the same name as the class is a constructor
(by inversion of the definition of a constructor). Constructors shall not
have return value types.

V
Jul 23 '05 #6
Peter Kragh wrote:

Karl Heinz Buchegger wrote:

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

public:
myPoly()
{
x = NULL;
y = NULL;
p = NULL;
nv = 0;
}
void myPoly(int n) // number of vertices
{
delete [] x; // realease the previously allocated arrays
delete [] y;
delete [] p;
<snip>

Karl, usually I like your posts, but this one, I simply don't
understand. Why delete members in a *constructor*?


Because I didn't notice that it was a constructor. That's why
I also added the return type of void which of course is nonsense
for a constructor. I was a little bit in a hurry. Sorry for that.

--
Peter

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7
Victor Bazarov wrote:

Peter Kragh wrote:
Karl Heinz Buchegger wrote:

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

public:
myPoly()
{
x = NULL;
y = NULL;
p = NULL;
nv = 0;
}
void myPoly(int n) // number of vertices
{
delete [] x; // realease the previously allocated arrays
delete [] y;
delete [] p;

<snip>

Karl, usually I like your posts, but this one, I simply don't
understand. Why delete members in a *constructor*?


With all due respect to both o'yous, it's not a constructor. Constructors
don't have return value types...

I wonder what Karl wanted to demonstrate (and whether it actually *was*
Karl posting that "code").


Yes it was me.
But it wasn't my best day. I had a little bit of stress in my own
project. Usually I answer posting during compiler runs but this time
I didn't pay close attention to the details. Actually I was thinking what
all those loops in the Draw() function could be intended for and did the
rest mechanically.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #8

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

Similar topics

1
by: Doug Farrell | last post by:
Hi all, I'm trying to do the following from within a code module: import re # text to match text = "Good morning x something /x, how are you today x something else /x"
1
by: Mathias Mamsch | last post by:
Hi, I have some confusion concerning the weakref module. I am trying to save a weak reference to a bound member function of a class instance for using it as a callback function. But I always...
38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
10
by: Kamilche | last post by:
I'm trying to pack two characters into a single byte, and the shifting in Python has me confused. Essentially, it should be possible to use a 'packed string' format in Python, where as long as...
4
by: JMCN | last post by:
object invalid or no longer set - confusion of the recordset in access 2003. i am currently converting from access 97 to access 2003. majority of the codes converted over perfectly fine, though...
0
by: i_have_control | last post by:
I'd be grateful for any input on this one: I have three web domains. The destinations of two are set to folders on the first, though that fact is transparent to the user (i.e: it does not...
13
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
2
by: Riaaaa | last post by:
Hello, We are doing the project in VB.Net. We had a great confusion for ASP.Net and VB.Net. Is VB.Net project performed in Microsoft Visual Studio 2005 ?? We have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.