Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory structure of a function-only object?

Tags:

c++

Let's say we have a class that looks like this:

class A 
{ 
    public:
        int FuncA( int x );
        int FuncB( int y );

        int a;
        int b;
};

Now, I know that objects of this class will be laid out in memory with just the two ints. That is, if I make a vector of instances of class A, there will be two ints for one instance, then followed by two ints for the second instance etc. The objects are POD.

BUT let's say the class looks like this:

class B
{ 
    public:
        int FuncA( int x );
        int FuncB( int y );
};

What do objects of this class look like in memory? If I fill a vector with instances of B... what's in the vector? I've been told that non-virtual member functions are in the end compiled as free functions somewhere completely unrelated to the instances of the class in which they're declared (virtual function are too, but the objects store a vtable with function pointers). That the access restrictions are merely at the semantic, "human" level. Only the data members of a class (and the vtable etc.) actually make up the memory structure of objects.

So again, what do objects of class B look like in memory? Is it some kind of placeholder value? Something has to be there, I can take the object's address. It has to point to something. Whatever it is, is the compiler allowed to inline/optimize out these objects and treat the method calls as just normal free function calls? If I create a vector of these and call the same method on every object, can the compiler eliminate the vector and replace it with just a bunch of normal calls?

I'm just curious.

like image 693
Lucas Avatar asked Jul 30 '10 15:07

Lucas


People also ask

How is a function stored in memory?

Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller's environment, such as some of the machine registers, are saved on the stack.

What is the memory structure of an object in C++?

There are two parts of memory in which an object can be stored: stack – Memory from the stack is used by all the members which are declared inside blocks/functions. Note that the main is also a function. heap – This memory is unused and can be used to dynamically allocate the memory at runtime.

When a function is called which part of the memory is used?

Locals 101: When a function is called, memory is allocated for all of its locals. In other words, when the flow of control hits the starting { for the function, all of its locals are allocated memory. Parameters such as number and local variables such as result in the above example both count as locals.

Are functions stored in stack or heap?

Functions are objects. Therefore, the function's identifier is in the stack, and the function's value is stored in the heap.


2 Answers

All objects in C++ are guaranteed to have a sizeof >= 1 so that each object will have a unique address.

I haven't tried it, but I would guess that in your example, the compiler would allocate but not initialize 1 byte for each function object in the array/vector.

like image 153
Ferruccio Avatar answered Oct 05 '22 23:10

Ferruccio


As Ferruccio said, All objects in C++ are guaranteed to have a size of at least 1. Mostly likely, it's 1 byte, but fills out the size of the alignment, but whatever.

However, when used as a base class, it does not need to fill any space, so that:

class A  {} a;    // a is 1 byte.
class B  {} b;    // b is 1 byte.
class C  { A a; B b;} c; // c is 2 bytes.
class D : public A, B { } d;  // d is 1 byte.
class E : public A, B { char ee; } e;  // e is only 1 byte
like image 30
James Curran Avatar answered Oct 05 '22 23:10

James Curran