• Content count

  • Joined

  • Last visited

Community Reputation

137 Neutral

About akira2000

  • Rank
  1. stack vs heap

    You can't manually "free" an object from the stack. Stack frames are pushed and popped for each "block" of code (basically every set of "{ ... }"). Whenever a "}" is reached, all the stack variables that were created in that block are destroyed. So there are never any holes.   For an example of where stack allocation isn't applicable, imagine a function that makes a new monster appear: Monster* SpawnMonster() {   Monster m;   return &m;//error! m is created with the same scope as this function. When the function returns, m is "deleted". }   Monster* SpawnMonster() {   Monster* m = new Monster();   return m;//correct, but someone has to delete this at some point, otherwise you've got a memory leak } shared_ptr<Monster> SpawnMonster() {   return shared_ptr<Monster>(new Monster());//also correct. This acts more like what you're used to in Java. When there are no more shared_ptr's that reference the Monster, it will automatically be deleted. }   I'm a little confused ;)  Can you please tell me, in the next example, mainCanon is in the heap or stack?   class Tank{ public:           Canon mainCanon;           ... } class Army{ public:           Tank tanks[20];            ... } void main(){           Army* army = new Army(); }   Thank You!
  2. stack vs heap

    About shared_ptr. I read now that that Bjarne consider this as bad idea and this is considered as bad c++ design pattern. Anyone can put some light on this. About unique_ptr. I do NOT want to write managed code. Is unique_ptr managed code ???
  3. stack vs heap

      0.o are you sure about this one? In java, for example, I do sometimes have objects of this size. When I work with images, pdfs... So I should create objects on stack only if their size < X ??? 
  4. stack vs heap

    OK   Now it's MUCH clearer.  As I see it now, I should use stack vars if and only if it's life span does NOT need to exceed the scope of it's creation {}.  Also, a "little" advantage of heap vars is that I can free them in the "middle" of the scope, thus the memory usage is a little smaller. Ok! I will use stack vars where I can ! Thank you all for the valuable answers :) 
  5. Hello.   I'm working as java programmer and just now starting to learn c++ and irrlicht (want to create games as a hobby ;) ). I am completely confused by stack vs heap issue. What is the advantage of using heap? Isn't it MUCH faster and more efficient to create variales on stack and pass them by reference. What I mean is:   Object obj = new Object();   VERSUS   Object obj; method1(&obj)   I am also confused by memory managment in c++. When stack object is freed is there a memory "hall" in stack.     Thanks