-3

Whenever we create an object from a class, it is created on the heap occupying more space as compared to a a struct variable which occupies its memory on stack. If i create a class of Person and a struct P, having same attributes, then it should justify what I just said. Please examine the following 2 fragments of code:

#include <iostream.h>
#include <conio.h>
#include <string>
using namespace std;

class Person{

      int age;
      string hair_color;
      float height;

      public:
      Person::Person(int n)
      {
       age = n;
      }

      int Person::getAge()
      {
        return age;    
      }



      };

struct P{

       int age;


       };      

main()
{

     Person person(45);

     //Person *person = new Person(45);


     P myPerson;

     cout<<sizeof(person)<<endl;
     cout<<sizeof(myPerson)<<endl;

     //cout<<"Age: "<<person->getAge(); 
    getch();  
}

And when i write this code:

#include <iostream.h>
#include <conio.h>
#include <string>
using namespace std;

class Person{

      int age;
      string hair_color;
      float height;

      public:
      Person::Person(int n)
      {
       age = n;
      }

      int Person::getAge()
      {
        return age;    
      }



      };

struct P{

       int age;


       };      

main()
{

    // Person person(45);

     Person *person = new Person(45);


     P myPerson;

     cout<<sizeof(person)<<endl;
     cout<<sizeof(myPerson)<<endl;


    getch();  
}

Please correct me if I am wrong here about objects and refernces. I want to know from my code what occupies more space: Object or Struct?

Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124
  • 12
    "Whenever we create an object from a class, it is created on the heap " - wrong. "occupying more space as compared to a a struct variable" - wrong. "which occupies its memory on stack" - wrong. So wrong. You clearly don't understand objects at all. Learn some more C++ and don't worry about memory usage yet. –  Oct 26 '13 at 16:49

4 Answers4

5

In some languages (C#), struct is used to define types that can be stack-allocated, while class instances have to be allocated on the heap.

In C++, there is no such difference and it is up to the instantiator to determine whether to allocate the object on the heap or the stack. For example

Person* person = new Person(45);

allocates the Person instance on the heap, while

Person person(45);

allocates a similar instance on the stack.

Additionally, there is nothing in general to support the statement "created on the heap occupying more space as compared to a a struct variable which occupies its memory on stack". It is often true that heap allocation comes with some overhead (memory-wise and in processing time), but stack space is often much more limited (usually a fixed stack size per thread).

There are lots of documentation on when to use what, and even dicussions here on SO. In short, the stack is to be used for small, short-lived objects (temporary objects within the scope of a function or similar).

Community
  • 1
  • 1
Krumelur
  • 31,081
  • 7
  • 77
  • 119
5

In C++, a struct and a class are exactly the same thing with regards to how they are allocated and how much space they occupy - the main difference is that the default for a class is that data is private, and you need to add public: for any group of members that you want to expose to the outside of the class, where a struct has public members by default, and if you want to have private members, you need to add private:.

Whether something is allocated on the heap, the stack or in global memory is entirely up to how the code using the class/struct is written (e.g. call to new will allocate on the stack, not calling new will allocate either global space or on the stack) and it makes absolutely no difference if the object is a struct or a class - they take up the same size if they have the same content.

It is OFTEN the convention that struct is used when the data in the structure is "plain data" and there is no constructor or member functions, where class is used when there are member functions and/or constructor involved. This is however a convention, and there is absolutely no TECHNICAL difference between:

 struct Coord3D
 {
    float x, y, z;
 };

and

 class Coord3D
 {
    public:
      float x, y, z;
 };

or between

struct Person
{
  private:
     int age; 
     std::string name;
  public:
     int getAge() { return age; }
     std::string getName() { return name; }
};

and

class Person
{
  // no need to have private:, but most people add it just to be perfectly clear
     int age; 
     std::string name;
  public:
     int getAge() { return age; }
     std::string getName() { return name; }
};

(Compiling code that uses either of the two alternative forms, and it would be completely impossible to tell from the binary code generated whether it is the first or second for)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

Please keep in mind that in C++ a struct and a class are not different kinds of things. They are the same thing, with a struct being syntactic sugar for creating a class with public members and methods, and defaulting to public inheritance.

Specifically:

Section 10.2.8 of the book "The C++ Programming Language", Bjarne Stroustrup states:

By definition a struct is a class in which members are public by default; that is

struct s { ...  

is simply shorthand for

class s { public:...

And the C++ Standard states (Section 11.2, Item 2):

In the absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.

Escualo
  • 40,844
  • 23
  • 87
  • 135
1

In addition to the other answers, a struct and class are stencils telling the compiler how something is organized. A struct and class do not take up any room in the executable since they are only used during compilation.

An object is commonly referred to as an instance of a class or struct. The object occupies memory. The amount of memory occupied depends of the definition of the struct or class that it was instantiated from.

So in answering the title of your question, Which occupies more space: Object or Struct?, the object does since a struct is only a stencil and the object is the instantiation.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154