|
-
Mar 4th, 2005, 07:34 PM
#1
Thread Starter
PowerPoster
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.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Mar 5th, 2005, 07:28 AM
#2
Fanatic Member
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.
-
Mar 5th, 2005, 11:58 AM
#3
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|