简体   繁体   中英

C++ Stack and Heap

I am often seeing these kinds of declarations:

class A
{
private:
    int a;
    int b;
public:
    // Ctor + Dtor + methods 
};

class B
{
private:
    int a;
    int b;
public:
    // Ctor + Dtor + methods
};

class C
{
private:
    A* aClass;
    B* bClass;
    // Other members.
public:
    // Ctor + Dtor + methods
};

aClass and bClass are dynamically allocated with the new operator.

Assumption

A lot of people tend to use the heap when there is apparently no reason to. Dynamically allocating A and B classes will decrease the size of the C object on the stack. However, the new operator is "costly" (execution time), and suppose we have getters and setters for all the variables contained in A and B , setting or getting a member of A and/or B will lead to some extra computation in order to access A or B (dereference A or B and then proceed to an operation).

Here are my questions:

  • Would it be better to write the following?

     class C { private: A aClass; B bClass; // .... } 
  • Can proceeding this way with large objects lead to a stack overflow?

  • Is the compiler able to proceed to some optimization if he does not know the "true type" of A* and B* ?

Edit

I forgot to mention that C is a composition, in both cases (thanks C.LECLERC). The first case is used to realize a composition.

Okay what if you made a Deck and a card class. Look Below

namespace game{

enum Rank{ACE=1,TWO,THREE,FOUR,FIVE,SIX,SEVEN,
               EIGHT,NINE,TEN,JACK,KING,QUEEN};

enum Suit{SPADE,DIAMOND,CLUB,HEART};

Class Card{
 public:
    Card(){
    suit = HEART;
    rank = ACE;
    }

    Card(int s, int r){
      suit = static_cast<Suit>(s);
      rank = static_cast<Rank>(r);
    }
 private:
    Rank rank;
    Suit suit;
};

Class Deck{
      public:
         Deck(int count){
              card = new Card[count];
        }
   // You would need a copy constructor of course but im lazy

       ~Deck() { delete [] card; } // Manually deallocate

         private:
          Card *card;
         };
     }

Now if we went over to main and made a new deck, this deck would have the amount of the cards the user typed inside it. This is just an example that I would use, and say we made 52 blank cards, lets have a function in the deck that generates a deck, using another method from card that makes random cards, or perhaps a standard deck.

The thing you are asking though is a bit odd, becuase you defined the A and B class so what is a "true type"

Of course some things run faster or slower than others, like using a linear or binary search on an array, one is obviously faster.

Study the relationships of the memory and such when you allocate these objects.

Also look at composition, aggregation and more relationships closely.

As for the stack overflow, are you working on an embedded system, or making one million objects, otherwise you should be fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM