new vs malloc Q: [RESOLVED]
It seems that while writing a template dynamic array class..I attemp to use malloc to initialize the array and when I free it I always get a block error.
It is as simple as:
//BLOCK ERROR
T *m_Pool;
m_Pool = (T*)malloc(uiSize);
free(m_Pool);
//WORKS FINE
T* m_Pool;
m_Pool = new T[uiSize];
delete [] m_Pool;
Edit: I am not multiplying the uiSize by the size in bytes -- malloc needs that. My fault.
Re: new vs malloc Q: [RESOLVED]
Template class + malloc = bad..
If your templated type has a constructor, malloc won't call it, so your objects don't get constructed properly. Use new in C++, malloc in C.
Re: new vs malloc Q: [RESOLVED]
malloc(uiSize);
allocates uiSize bytes. Does nothing else.
new T[uiSize]
allocates sizeof(T)*uiSize bytes. Calls the default constructor on each object.