'How much memory' is given by
sizeof operator[
^].
The following,
compiler-friendly version of your program
class A
{
public :
void display()
{
int v = 10;
std::cout<<"\nv:"<<v;
}
};
int main()
{
A a1; A *aptr; std::cout << "sizeof(a1)= " << sizeof(a1) << std::endl;
std::cout << "sizeof(aptr)= " << sizeof(aptr) << std::endl;
}
produces the output below:
sizeof(a1)= 1
sizeof(aptr)= 4
We get
sizeof(a1)=1
because the class is empty (i.e. has no data), but, anyway a
C++
object must have
size > 0
(must have an identity, see
Stroustrup's "Why is the size of an empty class not zero?"[
^]).
We get
sizeof(aptr)=4
because the program doesn't allocate an object, it allocates just '
a pointer to an object' (pointing to garbage) and pointers are
4
-bytes wide on my
32
-bits system.
Both allocations happens on the stack.
:)