Archived

This topic is now archived and is closed to further replies.

Dynamic Arrays Problem

This topic is 5535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''ve just finished my engine, but only a little problem is preventing me from owning a completely Object Oriented Engine. I want to declarer dynamic arrays (which there size is going to filled based on my game meshes), like VB, IB, world matrices... I''ve a header, a class called: in the .h file: PowerXChar { LPVertexBuffer8* vb; } in the main .cpp engine file: PowerXChar:owerXChar(.....) { LPVertexBuffer8* vb = new LPVertexBuffer8[num_of_meshes_in 3d file]; ... } PowerXChar::Render{...) { Here I again use vb..., but it seems they have lost their values, why? } tell me what to do?

Share this post


Link to post
Share on other sites
In this line here:
LPVertexBuffer8* vb = new LPVertexBuffer8[num_of_meshes_in 3d file];

You''re making a new varible, not changing the vb member of PowerXChar.

Try charging it to :
vb = new LPVertexBuffer8[num_of_meshes_in 3d file];

or this->vb=...


Share this post


Link to post
Share on other sites
malloc and free are C
new and delete are C++. new and delete call constructors and destructors of classes. also you don''t need to do any sizeof crap. they''re easier to use in terms of less characters/thinking to type/do.

-me

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
well i am used to malloc and free and i prefer to do it that way
So you prefer to write:

  MyClass* p = static_cast<MyClass*>(malloc(sizeof(MyClass)));
new(static_cast<void*>(p)) MyClass;
// use p...

p->~MyClass();
free(p);

Instead of:

  MyClass* p = new MyClass;
// use p...

delete p;

I don''t think there is any advantage to using malloc/free, unless, of course, you need to do realloc.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Forgot error-checking:

  MyClass* p = static_cast<MyClass*>(malloc(sizeof(MyClass)));
if(!p) throw bad_alloc;
try {
new(static_cast<void*>(p)) MyClass;
} catch(...) {
free(p);
throw;
}
// use p...

p->~MyClass();
free(p);

Share this post


Link to post
Share on other sites
quote:
Original post by Basiror
why does nobody use malloc and free?


quote:
Original post by Basiror
well i am used to malloc and free and i prefer to do it that way


quote:
Original post by Basiror
i am used to it thats all


Then you are a C programmer, there is nothing wrong with that, so long as you keep to the C library and don''t ever try to use C++ features.

Pick one language or the other and please, do not advise other people to use a bastard mix of the two (as you seem to do).

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites