Hello just curious, I know that i'm supposed to use new in c++ but decided to experiemnt with malloc.

Can you explain to me why the following code crashes and what is the reason. I was under the impression that the size of vector is fixed as it just stores a pointer to an dyanamic allocated array which holds the elements.

#include<iostream>
#include<vector>
using namespace std;

int main()
{


vector< string > * u = (vector< string >*) malloc(  sizeof(vector< string >)) ;
 *u =  vector< string >(10,"test");


system("pause");

}

My understanding is that malloc allocates memory to the size of a vector (which is 12 bytes) and then it is then deferenced and the constructor is called to fill that memory. I may be wrong.

Its probally something simple. Or not. I'm using the dev C++ compiler

Why would you do such experiment in the first place?
If it is research you would need to know why C++ use new instead of malloc

So why does C++ use new instead of malloc and why does malloc not work in this case?

new calls the constructor, malloc() doesn't.

But in the above code I’m calling the constructor after I have used malloc:

*u =  vector< string >(10,"test");

Is it because malloc does not initialise all the bits in the memory it allocates? But i would'nt know why that would make a difference if the contructor is being called.

So whats going on exactly?

I don't know. You are trying to work with an object which has not been constructed yet. Everything is possible...

vector< string >(10,"test");

Is one of the constructors of vector is it not? Can you tell me how its not being contructed?

Thanks

Hi thanks for the links. I understand the difference and the main one is that the constructor does not get called with malloc. I guess my question is whats the difference between this:

vector< string > * u = (vector< string >*) malloc(  sizeof(vector< string >)) ;
 *u =  vector< string >(10,"test");

Note I have called the constructor after malloc, which correct me if i'm wrong calls the constructor of vector and puts in 10 string elements all intialised to "test".

and This:

vector< string > *u =  new vector< string >(10,"test");

What does new do to the memory block other than call the constructor?

Thanks

Hi thanks for the links. I understand the difference and the main one is that the constructor does not get called with malloc. I guess my question is whats the difference between this:

vector< string > * u = (vector< string >*) malloc(  sizeof(vector< string >)) ;
 *u =  vector< string >(10,"test");

Note I have called the constructor after malloc, which correct me if i'm wrong calls the constructor of vector and puts in 10 string elements all intialised to "test".

and This:

vector< string > *u =  new vector< string >(10,"test");

What does new do to the memory block other than call the constructor?

Thanks

You are opening the whole can of worms. You have to find how each compiler implements the new operation and reinvent the wheel. I guess internally they use malloc/free
Here is related question:
http://stackoverflow.com/questions/1031301/can-i-implement-the-factory-method-pattern-in-c-without-using-new/1031375#1031375

Ah ok the line

vector< string > *u =  new vector< string >(10,"test");

is constructing a tempory object then using the assignment operator to assign to the allocated memory in which the object is uninitialized. Thanks for your answer.

If the problem is solved, mark it so!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.