954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

STL Vector and Malloc

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

wazzer225
Newbie Poster
9 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

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

wazzer225
Newbie Poster
9 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

Caligulaminus
Junior Poster
106 posts since Feb 2011
Reputation Points: 72
Solved Threads: 30
 

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.

wazzer225
Newbie Poster
9 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

No, you're not.

Caligulaminus
Junior Poster
106 posts since Feb 2011
Reputation Points: 72
Solved Threads: 30
 

So whats going on exactly?

wazzer225
Newbie Poster
9 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

Caligulaminus
Junior Poster
106 posts since Feb 2011
Reputation Points: 72
Solved Threads: 30
 
vector< string >(10,"test");


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

Thanks

wazzer225
Newbie Poster
9 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

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

wazzer225
Newbie Poster
9 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

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.

wazzer225
Newbie Poster
9 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

If the problem is solved, mark it so!

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: