BumbleBee's blog

By BumbleBee, history, 2 years ago, ,

Somwtimes we use different user deffined data structures in C/C++. For example,

struct data1{
string name;
int id;
double marks;
}


We can also use operator overloading and user defined functions in structers.

struct data2{
string name;
int id;
double marks;

bool operator < (const data &a) const{
return id<a.id;
}

{
marks+=x;
}
}


Using operator overloading or user defined functions makes the use of these structures easy.

Now my question is, when I declare an array or vector of a structure, will it allocate more memory if the structure contains some user defined functions in it?

For example, if I declare two vectors of the structures declared above ( data1 and data2 ) like given below, will they allocate same amount of memory?

vector <data1> v1(1000);
vector <data2> v2(1000);


Is the amount of memory allocated increases if the structures contains some functions in it?

• 0

 » 2 years ago, # |   +1 For vector of structure program will allocate memory for the data of the object uniquely, while the functions are allocated memory not inside of every structure object you create. So memory usage will be slightly more with more functions declared but all the object of that structure uses the same functions from memory, it is referred globally by objects of the type.
•  » » 2 years ago, # ^ |   0 That means, each object won't allocate memory for functions separately, rather all the object will use the same function memory and function memory will be global to all objects...am I right?
•  » » » 2 years ago, # ^ |   +11 That's what Object Oriented methodology (OOP) methodology follows, functions are allocate memory such that every object who invokes a member function passes implicitly its reference as "this" pointer as the first argument to the invoked member function.