@jaunchopanza's answer may be incorrect based on a technicality about English grammar--he uses the phrase "the only way", when there may be alternatives (but to be honest, I'm not at all sure there is any). Nonetheless, he outlines a technically correct method to do what the OP asked about. As such, while it might be open to a minor (bordering on trivial) improvement, I think it's quite a solid answer.
You answer is much more problematical. It uses malloc
to allocate raw memory. You rarely want to allocate raw memory in C++, but if you really need to do so, you nearly always want to do that with ::operator new
rather than malloc
.
It then attempts to assign the result from malloc
to a pointer to a Test
. This would work in C, but doesn't work (won't even compile) in C++. C allows an implicit conversion from void *
to T *
(for any arbitrary type T
). C++ requires that such a conversion be done explicitly, such as by using a static_cast
or C-style cast.
That is, this code:
// Bad code. If your C++ compiler accepts this, get a better compiler.
Test *t = malloc(10 * sizeof(Test));
...is what the standard calls "ill formed". The standard requires that a conforming implementation issue a "diagnostic" for this code. The usual implementation of that is that the compiler issues an error message, and your code won't compile. To make it compile, you need to add a cast:
// Bad code. Do not use
Test *t = (Test *)malloc(10 * sizeof(Test));
or:
// Still bad code. Don't use this either.
Test *t = static_cast<Test *>(malloc(10 * sizeof(Test));
or even:
// Minutely less bad code--but still don't use it.
Test *t = static_cast<Test *>(::operator new(10 * sizeof(Test)));
That allows the code to treat the block of raw memory as if it were objects of type Abc
--but that doesn't change the fact that what it actually contains is really just raw, uninitialized memory, not objects at all. This is not what the question asked about though. It asked specifically about creating objects: "Can we create an array of objects of the class [...]". Even assuming we add the cast necessary to let your code compile, that's still not what it does--it doesn't create objects, it just allocates raw memory, the tells the compiler a lie to say that raw memory is objects.
Will Rogers (I think he was the one, anyway) once told a joke that started with asking the audience: "If you call a horse's tail a leg, how many legs does a horse have?"
The audience came back with the obvious answer: "five".
Will then pointed out: "No. It doesn't matter what you call its tail, a horse still only has four legs."
In this case, it doesn't matter what you call that raw memory, it's still just raw memory.
Trying to use that raw memory as if it were objects (without invoking ctors to make it into objects) gives undefined behavior. That's the C++ standard's code phrase for saying: "what you've done isn't really C++".
As given, your code won't even compile with any compiler of which I'm aware. With a cast added, it'll compile but still gives undefined behavior. It's bad code.
If SO were to form a group of experts in C++, I think I'd have about as much right as anybody to be part of that group. I've taken what action I can to give my opinion of this answer; in addition to pointing out its flaws here, I've also added my down-vote and voted to delete the answer.
So should there be experts to review this kind of disputable situation?
yeah. But how can we identify those experts?... Let me think. Maybe using some sort of score? We could call it clout. Or perhaps even better, reputation. :)_x = 1_x = 2_x = 3_x = 4_x = 5177426_x = 3014739_x = 5177420_x = 4259907_x = 76_x = 5439573
. Different output from the same code means either UB or one of the compilers is broken. Even with all the problems MSVS has this is not a compiler bug. You have to understand the subtle language rules that you are breaking even though it compiles and it "works". Breaking those rules means all bets are off.Test
has no default constructor (since you gave it a non-default constructor); it therefore has "non-vacuous initialization". You did not call a constructor to initialize it, and therefore the lifetime of theTest
subobojects of the arrays has not begun. You access their value representation. And that's UB for an object who's lifetime has not started.