How does new allocate memory for user defined types?

Say you have a pointer of a variable sized user defined type. (That is to say that it itself has a non const poiner data member, whose size can change).

How much memory is allocated for each instance of this type with new[]?

I was trying to implement a bigint class when it dawned on me that I aint gonna be able to use it as a pointer, or at least not in the same way I would with a int *. So say i declare a pointer of myint: myint * reallybignumbers;
and now I want to have 10 really big numbers: reallybignumbers = new myint[10];
Then I set each number to whatever value I want it to be, but since they're really big numbers, they might require more memory than what was allocated with new? How can I do this correctly?
There cannot be any classes with variable sizes. The size of a class instance is always a fixed value at compile-time, determinable with sizeof.
You can allocate more memory as a result of using new[] inside a member function, but of course, that has no effect on its structure size.
It sounds like you're already doing what Athar talks about (allocating memory using new) by your mention of a non-const pointer data member. The storage being pointed to is not actually part of the class, so resizing it does not affect the size of the object. The pointer itself has a fixed size.

But in addition to the size of the object, as returned by sizeof(), you are also allocating the block of memory you allocated using new. This will be located outside of the object.

So new myint[10] will allocate 10 x sizeof(bigint) + an extra bit of space due to struct packing

+ 10 other blocks, one for each of your non-const pointers (if all are allocated)

If you need to have lots of bit ints, you might consider using some kind of shared storage mechanism.
What is a shared storage mechanism? (Although I probably don't want to know. Sounds complex)

1
2
3
class myint{int * d; myint(){d = new int [1];} ~myint(){delete d;}};
myint * reallybignumbers = new myint[100];
//code that assigns each element of reallybignumber to a huge number 


The way I understand it new myint[100] is supposed to allocate sequential memory starting at the address of the first element, i.e. reallybignumber[0];

So what happens when reallybignumber[0].d grows to require more memory that was allocated for with new myint? It is stored somewhere else?

Sorry, you guys probably addressed this already in one of your posts, I just didn't quite understand.

What is a shared storage mechanism? ... Sounds complex


It could do. I was thinking about a naive way of handing out stretches of an array of int, for calculating series of factorials (that was what you wer calculating before?). But for general use, it could get rather involved (reclaiming spaces, etc.)

The way I understand it new myint[100] is supposed to allocate sequential memory...


Yes, but it allocates the memory for the myint class's actual members, which is the size of an int pointer. and that's it. The sequential memory only contains enough space for all your 'd' pointers. (is you real class that small?)

The memory allocated for the 'd' member pointers is elsewhere. Whether these are near each other or not depends on what blocks were free on the heap when you called new int[1].

If you replace them with new (bigger?) blocks, this new memory is allocated in yet another location.

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