Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,427 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,902 people online right now. Registration is fast and FREE... Join Now!




Dynamic Memory

 
Reply to this topicStart new topic

Dynamic Memory, Dynamically Allocating Arrays problem ?

#include<wmx010>
26 Jan, 2008 - 09:32 AM
Post #1

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
CODE

#include <iostream>
using namespace std;

// class Point23 represents a point that may be two- or
// three-dimensional, depending on which constructor is used to create
// it. Coordinates are stored in a dynamically allocated array.

class Point23
{
public:
  // default class constructor (with no arguments):
  Point23();

  // class constructor that creates a 2-dimensional point with
  // coordinates xval and yval:
  Point23(int xval, int yval);

  // class constructor that creates a 3-dimensional point with
  // coordinates xval, yval, and zval:
  Point23(int xval, int yval, int zval);

  // class destructor:
  ~Point23();

  // member functions for getting values of x, y, and z (if it's present)
  int Get_X() const;
  int Get_Y() const;
  int Get_Z() const;

// private data members: size of the point and a pointer to the array of
// coordinates:
private:
  int Size;
  int *DataPtr;
};

// default class constructor creates a 2-dimensional point with
// coordinates x = 0, y = 0.
Point23::Point23()
{
  Size = 2;

  // Exercise 1: allocate memory for an array of two integers
  DataPtr = new int[Size];


  // Exercise 1: initialize the coordinates to 0s:

}

// class constructor creates a 2-dimensional point with
// coordinates xval, yval.

Point23::Point23(int xval, int yval)
{
  Size = 2;
  
  // Exercise 1: allocate memory for an array of two integers
  DataPtr = new int[Size];
  
  // Exercise 1: initialize the coordinates:
  xval = DataPtr[0];
  yval = DataPtr[1];
}

// class constructor creates a 3-dimensional point with
// coordinates xval, yval, and zval

Point23::Point23(int xval, int yval, int zval)
{
  Size = 3;
  
  // Exercise 1: allocate memory for an array of three integers
  DataPtr = new int[Size];

  
  // Exercise 1: initialize the coordinates:
  xval = DataPtr[0];
  yval = DataPtr[1];
  zval = DataPtr[2];
}

// class destructor deallocates all memory
// ..allocated for an object of the class
Point23::~Point23()
{
    // Exercise 2: deallocate memory for the array of coordinates
    delete [] DataPtr;
}

int Point23::Get_X() const
{
  // every point has an x coordinate
  return DataPtr[0];
}

int Point23::Get_Y() const
{
  // every point has a y coordinate
  return DataPtr[1];
}

int Point23::Get_Z() const
{
  // check if the point has a z coordinate:
  if (Size == 3)
     return DataPtr[2];
  else
  {
     cerr << "attempt to return a third coordinate of a 2D point" << endl;
     exit(1);
  }
}

// main: testing the class Point23

int main()
{
    // declaring points as variables (statically):  
   Point23 p1;               // default: 2D point with coordinates 0, 0
   Point23 p2(3, 4, 5);   // 3D point with coordinates 3, 4, 5

    // testing p1, p2:
   cout << "point p1: " << endl;
   cout << "x = " << p1.Get_X() << " y = " << p1.Get_Y() << endl;
  
   cout << "point p2: " << endl;
   cout << "x = " << p2.Get_X() << " y = " << p2.Get_Y() << " z = " << p2.Get_Z() << endl;

    // as an example of allocating memory for an instance of a
    // ..class, we declare a point dynamically:
   Point23 *Ptr1;               // declaring a pointer to a point
   Ptr1 = new Point23(7,8);     // ..allocating memory, initializing the point
                             // ..to a 2D point with coordinates 7, 8.

    // testing the dynamically allocated point:
   cout << "point pointed to by Ptr1 " << endl;
   cout << "x = " << Ptr1->Get_X() << " y = " << Ptr1->Get_Y() << endl;

    // Exercise 3: deleting the dynamically allocated point:
   delete [] Ptr1;


    // Note: there is no need to delete p1, p2, since these are local
    // variables which are deleted automatically when the program finishes.
    // Deleting p1, p2 would be an error, since they have not been allocated
    // by "new".

   return 0;
}


This is the first time I use memory allocation, anyway here's the runtime error I'm getting:

IPB Image

This post has been edited by #include<wmx010>: 26 Jan, 2008 - 09:39 AM
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: Dynamic Memory
26 Jan, 2008 - 02:17 PM
Post #2

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 231



Thanked: 3 times
My Contributions
You have the error because you are using "delete [] Ptr1".

when you use new[], you should use delete[] and when you use new, you should use delete.

They should match up. You were telling it to delete an array when there wasn't one.

Also, the DataPtr assignments in your constructors don't make sense, you have them backwards.

Per
User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Dynamic Memory
26 Jan, 2008 - 02:27 PM
Post #3

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
How could I've been so blind. ^^ Thanks 4 the help !
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: Dynamic Memory
26 Jan, 2008 - 02:31 PM
Post #4

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 231



Thanked: 3 times
My Contributions
No problem!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month