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_LINE S);
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. 15 1475
In message <11************ **********@l41g 2000cwc.googleg roups.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
Because this classes is only part of the whole code, and using vector
only makes us more reliable on c++ library.
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.
In message <11************ ********@f14g20 00cwb.googlegro ups.com>, 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
hm,....
depends on the task itself.
how large is the task i think, not really sure
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.
In message <11************ **********@l41g 2000cwc.googleg roups.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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()):
// first way
CBox x;
x.size();
// second way
|
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 fails on the 32665 item. Below
is a sample project to show it.
Program 1 (where the memory will be stored)
---------------------------------------------
#include "stdafx.h"
#include <windows.h>
|
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 " statement.
This happens for some inputs and not all.
What can be the reason for such fault ?
|
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 then allocate to the
pointers to which those pointers point?
essentially lets say i during runtime deturmine i must allocate space for 60
|
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 {
struct _NAME_INFO *Next;
ULONG LastId;
ULONG Id;
PVOID Value;
| |
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 page by content ID. The
site appears to be working well. It is hosted on a Windows 2003 server
with 2 Gigs of RAM. It was built using Visual Studio .NET 2005 and us
running under the .NET Framework 2.0 Beta
The Issue :
|
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 resources.
So far i have tried to use
#include <stdio.h>
int main() {
int *ip;
|
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 can allocate
memory once and use it to create new node in list.
-Thanks
-Kane
|
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 {
//
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |