unable allocating memory

Discussion in 'C++' started by Mona, Apr 14, 2005.

  1. Mona

    Mona Guest

    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.
    Mona, Apr 14, 2005
    #1
    1. Advertising

  2. In message <>, Mona
    <> 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
    Richard Herring, Apr 14, 2005
    #2
    1. Advertising

  3. Mona

    Mona Guest

    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, Apr 14, 2005
    #3
  4. 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
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Apr 14, 2005
    #4
  5. Mona

    Mona Guest

    Because this classes is only part of the whole code, and using vector
    only makes us more reliable on c++ library.
    Mona, Apr 14, 2005
    #5
  6. 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.
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Apr 14, 2005
    #6
  7. In message <>, Mona
    <> 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
    Richard Herring, Apr 14, 2005
    #7
  8. Mona

    Mona Guest

    hm,....
    depends on the task itself.
    how large is the task i think, not really sure
    Mona, Apr 14, 2005
    #8
  9. Mona

    Mona Guest

    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.
    Mona, Apr 14, 2005
    #9
  10. In message <>, Mona
    <> 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
    Richard Herring, Apr 14, 2005
    #10
  11. Mona

    Jay Nabonne Guest

    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 and y to assign the values to the array.

    And delete them with delete [] x; etc.

    Or use vector as others suggest... :)

    - Jay
    Jay Nabonne, Apr 14, 2005
    #11
  12. Mona

    Old Wolf Guest

    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++.
    Old Wolf, Apr 14, 2005
    #12
  13. Mona

    Lind Guest

    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>>y;
    }
    for (int j = 0; j< n_count ; j++)
    {
    infile >>p[j];
    }
    infile.close();
    }
    but the infilex and y won't work, they keeps on giving me error.
    I really dunno how to fix it anymore.
    Lind, Apr 15, 2005
    #13
  14. 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
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Apr 15, 2005
    #14
  15. 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>>y;
    > }
    > for (int j = 0; j< n_count ; j++)
    > {
    > infile >>p[j];
    > }
    > infile.close();
    > }
    > but the infilex and y 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
    Karl Heinz Buchegger, Apr 15, 2005
    #15
  16. Mona

    Jay Nabonne Guest

    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...
    Jay Nabonne, Apr 15, 2005
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hall
    Replies:
    4
    Views:
    432
  2. soni29
    Replies:
    6
    Views:
    398
    Kevin Goodsell
    Sep 5, 2003
  3. Axel
    Replies:
    1
    Views:
    721
    stephan beal
    Oct 27, 2003
  4. Sameer
    Replies:
    2
    Views:
    277
    David White
    Nov 3, 2003
  5. Rakesh Kumar
    Replies:
    5
    Views:
    676
    James Kanze
    Dec 21, 2007
Loading...

Share This Page