Allocating space for arrays in heap

Lets say i have this piece of code
1
2
3
4
5
6
7
8
9
10
11
12
class wat {
public:
   int data;
   wat::wat() {} //empty constructor for declaring arrays with new
   wat::wat(int a) { //regular constructor
      data = a;
   }
};
main method {
   wat* arrayofwat = new wat[5];
   arrayofwat[0] = wat(10); //line of interest
}


This works in this case but i recently discovered it is inefficient because on the line of interest it creates a new wat then copies its values over to the old wat already inside the array and then destroys the new wat (also causing potential issues like triggering the new wat's deconstructor which could for example delete data pointed to in the class resulting in the assignment action destrying the data)

So is there a way to allocate space for the array without using the new keyword while still it being in the right shape to be able to delete it with delete[] keyword? because as far as i know delete[] is only guaranteed to work on things declared with new[]. Also allocating array space with new[] seems like a bad idea to begin with because just allocating space has to be simpler than allocating space and building data structures in it
Last edited on
closed account (zb0S216C)
hazarada wrote:
(also causing potential issues like triggering the new wat's deconstructor which could for example delete data pointed to in the class resulting in the assignment action destrying the data)

I don't follow. Here's how I interpret lines 10 & 11:

First, an array of 5 ::wats is created dynamically. arrayofway now points to that memory. Then, the first element of arrayofwat is assigned to a temporary ::wat instance. Before the assignment begins, the temporary instance calls the wat::wat(int) constructor. Once fully constructed, the assignment is made. This assignment merely overwrites the data arrayofwat0 contains. The assignment doesn't affect the allocated memory in any way (except for modifying the data within).

hazarada wrote:
So is there a way to allocate space for the array without using the new keyword while still it being in the right shape to be able to delete it with delete[] keyword?

No, except for deleting constructors and operators (different context).

hazarada wrote:
because as far as i know delete[] is only guaranteed to work on things declared with new[].

That's true.

hazarada wrote:
Also allocating array space with new[] seems like a bad idea to begin with because just allocating space has to be simpler than allocating space and building data structures in it

So what's the alternative? The stack? That's an even worse idea. The stack size is limited, but so is the heap. The heap, however, is far bigger than the stack. Usually a stack is 1 megabyte large, whereas modern systems have a heap size of 1GB+. Also, it sounds like you don't like the fact that objects are implicitly created within the allocated memory. Am I right?

Wazzak
Last edited on
He is trying to directly initialize the objects in the array, instead of having the automatic items created and destroyed before he can assign the value he actually wants.

C++03 doesn't provide any easy way to do that.
C++11 makes it possible with improved initializers and move semantics.

If your class is properly designed, however, it should not cause any problem anyway. (A default-constructed class shouldn't be doing anything to lock resources or the like and should therefore be easily destructed.)

Hope this helps.
> Also allocating array space with new[] seems like a bad idea to begin with
> because just allocating space has to be simpler than allocating space and building data structures in it

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
34
35
36
37
38
39
40
41
42
43
44
45
#include <new>

struct wat
{
    wat( int aa, int bb ) : a(aa), b(bb) {}
    ~wat() {}
    int a ;
    int b ;
    // ...
};

int main()
{
    enum { N = 5, SZ = sizeof(wat) } ;

    {
        //////////////////////// C++11 ////////////////////////////

        wat* p = new wat[N] { {0,1}, {2,3}, {4,5}, {6,7}, {8,9} } ;

        // use objects

        delete[] p ;

    }

    {
        //////////////////////// C++98 ////////////////////////////

        // allocate memory
        char* pc = new char [ N * SZ ] ;

        // construct in-place with placement new
        wat* p = new(pc) wat(0,1) ;
        for( int i=1 ; i<N ; ++i ) new( pc + i*SZ ) wat( i*2, i*2 + 1 ) ;

        // use objects

        // destroy in-place without releasing memory
        for( int i=N-1 ; i>=0 ; --i ) p[i].~wat() ;

        // release memory
        delete[] pc ;
    }
}

Topic archived. No new replies allowed.