473,799 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 23 '05
15 1476
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
4129
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
11
2888
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>
4
745
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 ?
3
2181
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
7
2100
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;
3
5049
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 :
19
3817
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;
3
447
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
10
1770
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
9686
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, 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...
0
9540
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,...
0
10475
Oralloy
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...
0
10250
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 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...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2938
bsmnconsultancy
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...

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.