Dynamic Arrays Problem

Started by
13 comments, last by vcGamer 21 years, 5 months ago
quote:Original post by Basiror
i didn t advice anybody i just asked why nobody uses it
thats all



malloc != new
free != delete

and thats the point, new and delete call constructors. And if you think constructors arent important, then expect all of your classes to crash.

--Fireking

Owner/Leader
Genetics 3rd Dimension Development
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
Advertisement
why would they crash?
when they don t have a constructor?
http://www.8ung.at/basiror/theironcross.html
All classes have constructors. If you don''t creat one it gets a default one.
The point is that new/delete "know" about your contructor/destructor code. Meaning you get the chance to initalize/cleanup anything you want. Using malloc/free does not give you a chance to do this.

When I have to use strict C I always end up writing functions that do initialization like this:

Example *eg = AllocateExample();

Example* AllocateExample() {
Example *eg = malloc( sizeof(Example) );

eg->variable1 = 0;
eg->variable2 = 0;
eg->variable3 = 0;
.
.
.
}

so that I don''t forget to inialize something. Especially if you have member variables that are pointers...if you forget to initialize them to NULL and the you do a check against NULL somewhere else in your program then you are going to crash when you try to access that pointer.

If you use "new/delete" then you are given 1 place to do all of your initialization.
thats how i am doing it
http://www.8ung.at/basiror/theironcross.html

This topic is closed to new replies.

Advertisement