| maynard 2006-01-24, 7:07 pm |
| Victor Bazarov wrote:
> MyClass* arrayCreator(size_t howmany, int argument) {
> char *storage = new char[howmany * sizeof(MyClass)];
> for (int i = 0; i < howmany; ++i)
> new (storage + sizeof(MyClass) * i) MyClass(argument);
Victor, of course, it's never as easy as it seems. I've overlooked
something...MyClass has dynamically allocated members (dynamic arrays
of doubles). The MyClass constructor used above is intended to
allocate all of these arrays with *size* "argument", not simply to
initialize some value to "argument". I see how this will corrupt the
memory allocated using the above bit of code, but I can't quite figure
out how to resolve this. I think my main problem is that I do not know
the size (sequentially) of an object of MyClass until it is
instantiated. Therefore, the above approach wouldn't quite work (since
one would need to know how much to increment the "new placement" for
each allocated object of MyClass, right?).
Am I making any sense?
|