C++ Allocate Memory Without Activating Constructors

Posted by schnozzinkobenstein on Stack Overflow See other posts from Stack Overflow or by schnozzinkobenstein
Published on 2011-01-01T23:31:56Z Indexed on 2011/01/01 23:53 UTC
Read the original article Hit count: 276

Filed under:
|

I'm reading in values from a file which I will store in memory as I read them in. I've read on here that the correct way to handle memory location in C++ is to always use new/delete, but if I do:

DataType* foo = new DataType[sizeof(DataType) * numDataTypes];

Then that's going to call the default constructor for each instance created, and I don't want that. I was going to do this:

DataType* foo;
char* tempBuffer=new char[sizeof(DataType) * numDataTypes];
foo=(DataType*) tempBuffer;

But I figured that would be something poo-poo'd for some kind of type-unsafeness. So what should I do?

And in researching for this question now I've seen that some people are saying arrays are bad and vectors are good. I was trying to use arrays more because I thought I was being a bad boy by filling my programs with (what I thought were) slower vectors. What should I be using???

© Stack Overflow or respective owner

Related posts about c++

Related posts about memory-allocation