Advertisement

why is it that you access DX's interface via pointers..,

Started by May 11, 2002 10:22 PM
21 comments, last by mickey 22 years, 6 months ago
davidsporn:

isn''t it supposed to be the other way around?

ie,

Class myclass ;

would result putting that on the stack and the other one would be on the heap,



http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Um ya, if you allocate it dynamically it comes from the heap, otherwise it''s on the stack.
Advertisement
The other big advantage of pointers is it lets you take advantage of some of the polymorphic properties of objects. If you had (really bad) example class like CAnimal, a variable of that type could be initialized as any one of CAnimals decendents. Hence if you had an array called zoo, you could fill it with different CAnimal''s, each one using its own version for CAnimals methods (each animal could have its own Roar method for instance).
dx could not work without pointers. the reason for the pointers and dynamically allocating the class is so that you dont need to have any statically linked fucntion calls. this allows the dx object to be updated, but you will always recieve the same interface that your app uses. read up on polymorphism.

static varibles are not stored on the stack.

to save yourself from memory leaks, you delete anything you new. that simple, heh.

the previous examples explain why pretty clearly. i think you need to do some research to understand the difference between statically linked code vs dynamicly linked code. create some classes that use polymorphism.

remeber, a statically allocated object requires all the code to be present. a dynamically allocated object only requires the defination of the parent class. you can create many different classes which are all derived from a single base class (ie like a image loader which has a different class for each image file type). the app does not need to care about how the child classes work, only how the base class works. you can even change how the child class works, and not need to change anything in the app, this assumes that you dont change the base class, and the changes you make are to virtual functons that have been inherieted.

i think the thing your missing, is how powerfully polymorphism is when used with dynamically allocated objects when you are only dealing with the base class. its more then anough reason to deal with the memory mangement issues.

EDIT: also, objects that are not dynamically created require you to staticly link the class bloating your exe. furthermore since its statically linked, you cant recieve benefits of updates to dx.



[edited by - a person on May 15, 2002 3:11:48 AM]
quote: Original post by davidsporn
I think that another fact is that :



Writing this : Class myclass ;




The code is compiled so that memory is allocated from the heap space (a limited chunk of memory that is used to hold variables)
Thus if your object is larger than the reamining heap size, your app will crash.

Using pointer will allow you to use all the available memory space.



----
David Sporn AKA Sporniket

[edited by - davidsporn on May 13, 2002 7:49:38 AM]



Replace "heap space" with "stack space"


Forever trusting who we are
And nothing else matters
- Metallica
Forever trusting who we areAnd nothing else matters - Metallica
get use to pointer, they can make miracle things! mickey
Nothing to do, try complimenting someone, it will make you feel better!
Advertisement
alrighty...,

but anyway last thing, how much do we have in heap space? wait a minute.., so pointers, when created will be placed on the heap space right? and this is limited??? so we can''t have a lot of dynamically allocated objects alive in one time? so even though my system has a 512 megs of ram and including virtual memory that windows provides, then it still doesn''t matter because pointers used heap space not system memory?

thanks!
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Heap space is a part of system memory. So is the stack space, but the stack stays the same size for the life of the program and is used by functions for parameter passing and calls. The heap space OTOH grows to meet your requirements. This means you can have a lot of dynamically allocated objects alive at once. If you need more heap space, some system memory is allocated to the heap.

So your computer has the potential to use 512MB of heap space, but if you did so, your program might be too big.




University is a fountain of knowledge, and students go there to drink.
when allocating dynamic objects you have theoretically 4gig ram thanks to virtual memory and 32bit addressing. in reality i would try not allocating too much more then half your physical ram if its for a realtime app. dont worry about it, you can allocate 4gig (if you have the harddrive space), also some of the virtual memory address space is mapped and reserved for io devices like the video card, etc. nothing that will concern you either. the OS handles it.
Another reason why pointers are sometimes used is that some objects have to be passed to other funtions or member functions. If you pass an entire object to a function, you get really bad performance because you have to pass *every* single variable in that class to the function. For large objects, like those in DirectX, this effect could really cause problems. Passing the pointer is much more efficient - you don''t need to pass all of the baggage to the function..

This topic is closed to new replies.

Advertisement