Question about 'new'...

I know that if I use "new" to allocate space within an object, then I am supposed to de-allocate the space in the destructor with "delete". How do you handle this if you use "new" in main() or within a code block within main, such as below?

1
2
3
4
5
6
7
8
int main() {
  Object[10] obj_arr = 0;
  Object* obj = 0;
  for (int i = 0; i < 10; i++) {
    obj = new Object;
    obj_arr[i] = obj;
  }
}
You must call delete before the block ends.
That code is technically invalid. You should have an array of object*. Then you set obj_arr[i] = new Object.
So you would delete the values immediately after line six.
Line 2 is invalid. Maybe you meant Object obj_arr[10];. Let me guess. Your last language was Java.

If you declare obj_arr as I did above, you don't need to use new, and consequently you don't need to use delete.
Exactly so. But I believe the OP is looking for a proof of concept more than an actual program.
Java's array and class declarations are so much weirder than C++, and I swear I haven't figured out an advantage yet.
@davermont
yeah line 2 is valid in java but not in c++.. identifier should come first before square brackets.. i don't really know what you want to do but here's some info.

the default constructor is called if you use new..
the destructor is called when you use delete or it's scope is reach..

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
// example on constructors and destructors
#include <iostream>
using namespace std;

class CRectangle {
    int *width, *height;
  public:
    CRectangle (int,int);
    ~CRectangle ();
    int area () {return (*width * *height);}
};

CRectangle::CRectangle (int a, int b) {
  width = new int;
  height = new int;
  *width = a;
  *height = b;
}

CRectangle::~CRectangle () {
  delete width;
  delete height;
  cout << "deleted\n";
}

int main () {
  CRectangle rect (3,4), rectb (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
}


@tummychow
well in java everything is an object so you could use the length property of an array to get it's length, i guess that's an advantage.. also garbage collection automatically occurs when you assign null to an array or object.. or when there already no reference to an object..
well in java everything is an object
Not everything.
sorry.. i don't really mean everything.. i'm just too exaggerated.. yeah almost everything in java is inside a class.. there are also interface and enum.. and abstract classes..

please tell me what are the things you know that are not object in java.. thanks.. ^^
blackcoder: I will answer your question.... I think the integral types, such as int, are not classes, although they have a class equivalent (Integer). On the other hand, enums are some weird stuff, they can have methods defined inside and behave most likely like a class defining a constant list. Anyway, that's a C++ forum, not a Java one. Maybe we should port this discussion to another forum ;)
Last edited on
@vichor
of course primitive types are not included.. LOL ^^

yeah you're right we shouldn't discuss it here.. but it always happen..
I don't see how line 2 can be valid in java... I think it was just a poor example.

for instance, lets say we have a cat object you can't say:
1
2
3
4
5
6
7
8
9
10
11
Cat[10] cat = 0;

// this needs to be

Cat[] cat = new cat[10];

// or

Cat[] cat;

cat = new Cat[10] { /* different cat objects */ };

this would only work with type int, short, long, or perhaps float. String, double, and all objects would be an error.

as far as standard arrays in java goes, I feel they are much more functional. For instance using arrays is much more useful in java than c, because in c you need to have dodgy workarounds to deal with unknown array sizes. while in java simply array.length gives you your size/length.
There's other useful things about them to, such as copyrange(int,int), toString(), search(), sort(), List<Obj>... etc

However this is C++ forum so you can ignore the above :P
Last edited on
@gcampton
sorry i am careless.. actually the code above is wrong in C++ either in Java.. here's the correct way..
1
2
3
4
5
6
7
8
//this are correct
Cat a[] = new Cat[10];
Cat[] b = new Cat[10];
Cat c[];
Cat[] d;

//this is wrong..
Cat [10] cat;


~hope that helps
while Cat a[] = new Cat[]; is valid and will compile it is not standard. It was standard in the old 1.4... Now types must have the array identifier as to be gramitcally correct, eg. Object Cat array, named "cat". whereas the old standard it was Object Cat, named "cat array". The latter made no sense.
Last edited on
Looks like I did myself a disservice by writing the example in my browser and not running it through a compiler before posting it. Whomever suggested I came from Java is correct; my syntax is mixed and not 100% correct in either language. I didn't mean to set off a tangent.

Anyway, I have a follow-up question and this time I did put my code into VS to validate it's syntax. I have a situation like the one below, where I have an array of object pointers and I want to have the pointers in the array point to both stack and heap objects. Assume that the "Object" class is properly defined and included.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {
	Object** obj_arr = new Object*[3];
	for (int i = 0; i < 3; i++) {
		obj_arr[i] = 0;
	}

	Object obj;
	obj_arr[0] = &obj;
	for (int i = 1; i < 3; i++) {
		obj_arr[i] = new Object;
	}

	system("pause");
	return EXIT_SUCCESS;
}


I assume that I have to track which objects are created on the heap and which ones are created on the stack and only delete the heap objects later. Is this right? Also, is this just a bad practice to mix heap and stack objects in an array (or in, say, a vector)? What happens to my stack object if I delete the (heap) array containing the pointer that points to it? I assume the stack object will still have its memory reclaimed when the program terminates even though the pointer to it has been deleted.
I assume that I have to track which objects are created on the heap and which ones are created on the stack and only delete the heap objects later.
That's impossible without adding more information to the object.

It's never a good idea to mix stack and heap pointers in the same data structure. There are four ways to improve your example there.
Only stack:
Object obj_arr[3];
Heap pointers in a stack array:
1
2
3
4
5
6
Object *obj_arr[3];
for (int a=0;a<3;a++)
    obj_arr[a]=new Object;
//...
for (int a=0;a<3;a++)
    delete obj_arr[a];

Heap array of objects:
1
2
3
Object *obj_arr=new Object[3];
//...
delete[] obj_arr

Heap pointers in a heap array:
1
2
3
4
5
6
7
Object **obj_arr=new Object*[3];
for (int a=0;a<3;a++)
    obj_arr[a]=new Object;
//...
for (int a=0;a<3;a++)
    delete obj_arr[a];
delete[] obj_arr;

Depending on the situation, you'll have to use any of these four.
Last edited on
Topic archived. No new replies allowed.