Dynamic Memory??

Hi, I was hoping if someone could explain this to me. When is dynamic memory suppose to be use? When is "new" and "delete" suppose to be used? Why is the new operator used in both constructors? If "new" is used in the constructors why not in the destructor and in class myRectnagle where it was declared?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

class myRectangle {
  int *width, *height;
  public:
         myRectangle ();
         myRectangle (int,int);
         ~myRectangle ();
         int sayArea () {return (*width * *height);}
         };
         
         myRectangle::~myRectangle()
         { delete width; delete height;}
         
         myRectangle::myRectangle () 
         {height = new int; width = new int; 
         *height = 4; *width = 4;}
         
         myRectangle::myRectangle (int a, int b)
         {height = new int; width = new int;
          *width = a; *height = b;}
         

int main () {
    int a, b;
    myRectangle rect(3,4);
    myRectangle rectb;
    cout << "Rect's area: " << rect.sayArea() << endl
         << "Rectb's area: " << rectb.sayArea() <<endl;
         system("PAUSE");
         return 0;
         }
Ok,

the new operator is used to initialize a chunk of memory on the heap. If you choose to allocate heap memory using new it will automatically allocate the space not only for the class or structure, but for its inward parts (functions, data members). If you are unfamiliar with the difference between the Stack and the Heap you may want to brush up on the subject.

That being said, your example is redundant to say the least. Object lifespan is really what you
need to consider. If you need to use a user defined type, or an STL object ask yourself, do I need
to use this in more than one function? Do I need to maintain the state of this object throughout
the rest of the application?

Scope is also an issue. For example you may want to send a variable to another function by reference vs. value. In this case you may be able to send the memory address of the local variable via pointers:

1
2
3
4
void some_func(int *i) { *i += 1; }

int i = 45;    // i => 45
some_func(&i); // i => 46 


Destructors are implicitly called by the delete operator. In the above example, you would not need to express delete or new for primitive types such as int. If you had say, a vector<T*> where T is any type of class object then you would explicitly express delete for each element.

The most important thing to realize is that you are 100% responsible for memory management in C++. Developing good habits from the beginning will help you avoid memory leaks in the future and optimize efficiency.
Last edited on
closed account (zb0S216C)
When you use the new[1] keyword, a block of memory is allocated from the main memory, also known as the heap. Unlike new, malloc( ), and calloc( ) don't call the types constructor. The size of the block allocated, is determined how you used the new keyword. For example:

1
2
3
4
5
// Allocate a single 4-byte integer, then initialize it to 10.
int *block_a( new int( 10 ) );

// Allocate 3 4-byte integers. 
int *block_b( new int[ 3 ] );


Every block of memory allocated by new must be released. This can be done by using the delete[1] keyword. However, based on the type of block you allocated, your call to delete may vary. For example:

1
2
3
4
5
6
7
8
9
// Allocate a single 4-byte integer, then initialize it to 10.
int *block_a( new int( 10 ) );

delete block_a;

// Allocate 3 4-byte integers. 
int *block_b( new int[ 3 ] );

delete [ ] block_b;


If a pointer allocates multiple objects of the same type, or, in the form of an array( like block_b in the examples ), you would call delete [ ] < POINTER_IDENTIFIER >. If a pointer allocates only one object( like block_a in the examples ), then you would call delete < POINTER_IDENTIFIER >

If you plan on allocating memory with the same pointer after releasing previously allocated memory, make sure you set the pointer to NULL prior to allocation.

Note that new doesn't return NULL on an allocation failure. Instead, new throws a bad_alloc exception[2]. To force new to return NULL on allocation failure, you would explicitly tell it to, like this:

 
int *block( new ( std::nothrow ) int( 10 ) );


However, telling new to not throw exceptions will mean if new fails to allocate a block of memory, it's basically won't tell you. You will know if the allocation fails, however( the will program crash ).

References:
[1]http://www.cplusplus.com/reference/std/new/
[2]http://www.cplusplus.com/doc/tutorial/exceptions/


Wazzak
Last edited on
Topic archived. No new replies allowed.