answersLogoWhite

0


Best Answer

Never. A class is a type definition that only exists in the source code, so no storage is ever allocated to it. This can be proved by the simple fact that you can never take the address of a class.

When you instantiate an object from a class, the object and its members have identity thus you can take the address of that object and its members. Similarly, as soon as you use the static members of a class, you may take the address of those members. However the class itself does not exist; its sole purpose is to assist the compiler in generating the code that allows you to call static member functions and to instantiate objects of the class. Once that code is compiled, the class definition is entirely redundant.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Memory is always allocated at the point of instantiation. In the case of all functions, whether members of a class or not, instantiation occurs at compile time.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When is memory allocated for a function of a class in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between a class and a union in c plus plus?

The data members of a class are each allocated separate memory addresses. A union's members are all mapped to the same address. If you change the value of one union member, you change them all.


When is memory allocated when declaring an array in C plus plus?

It depends how the array is declared (fixed size or variable size) and where it is declared (global scope or local scope). If the array is declared in global scope (outside a function) and is fixed size, it will be allocated in static memory. If it is variable size, the pointer is stored in static memory while the array itself is allocated on the heap. The pointer in static memory points to the start address of the array in heap memory. If the array is declared in local scope (inside a function) and is fixed size, it will be allocated on the stack in whichever thread the function was called. If it is variable size, the local pointer is stored on the stack while the array is allocated on the heap. The pointer will fall from scope when the function returns so the array must not be allowed to outlive the function in which the pointer is declared. If the array must outlive the function that allocates the array, the pointer must be declared at a higher scope in the call stack and must be passed by reference to or returned by value from the function that allocates the array. If you provide your own memory manager, however, an array may be allocated wherever the memory manager's memory pool is allocated, be it in static memory, the stack or the heap. A memory manager essentially allocates an array of bytes which you can then utilise as you see fit (the array of bytes will be allocated as per the previous description for arrays in general).


How is a c plus plus program stored in the memory?

C++ programs are not stored in memory (RAM) they are stored on mass storage devices (usually disk drives). When compiled, they produce machine code programs which contain machine instructions and their operands. These are also stored on mass storage devices, but when loaded into memory the machine instructions are executed by the CPU.


What is member fusion in c plus plus?

If you are asking about member functions. When we declare a function inside a class then that function becomes member function of that class and this function can access the whole class


How do you calculate the size of a class in memory in C plus plus?

Use sizeof( ).

Related questions

What happens when class libraries leak memory in c plus plus?

A memory leak is when allocated memory that is no longer needed is not deallocated. Eventually, the memory pool is unable to satisfy an allocation request, and the program fails. A memory leak is a programming bug. When class libraries leak memory, they need to be fixed, just like any other piece of code that has bugs. If they came from a vendor, then that vendor needs to fix them.


What is the difference between a class and a union in c plus plus?

The data members of a class are each allocated separate memory addresses. A union's members are all mapped to the same address. If you change the value of one union member, you change them all.


When is memory allocated when declaring an array in C plus plus?

It depends how the array is declared (fixed size or variable size) and where it is declared (global scope or local scope). If the array is declared in global scope (outside a function) and is fixed size, it will be allocated in static memory. If it is variable size, the pointer is stored in static memory while the array itself is allocated on the heap. The pointer in static memory points to the start address of the array in heap memory. If the array is declared in local scope (inside a function) and is fixed size, it will be allocated on the stack in whichever thread the function was called. If it is variable size, the local pointer is stored on the stack while the array is allocated on the heap. The pointer will fall from scope when the function returns so the array must not be allowed to outlive the function in which the pointer is declared. If the array must outlive the function that allocates the array, the pointer must be declared at a higher scope in the call stack and must be passed by reference to or returned by value from the function that allocates the array. If you provide your own memory manager, however, an array may be allocated wherever the memory manager's memory pool is allocated, be it in static memory, the stack or the heap. A memory manager essentially allocates an array of bytes which you can then utilise as you see fit (the array of bytes will be allocated as per the previous description for arrays in general).


How c plus plus objects are allocated memory?

With the new operator.myclass myclasspointer = new myclass;...use the classdelete myclasspointer;


How is a c plus plus program stored in the memory?

C++ programs are not stored in memory (RAM) they are stored on mass storage devices (usually disk drives). When compiled, they produce machine code programs which contain machine instructions and their operands. These are also stored on mass storage devices, but when loaded into memory the machine instructions are executed by the CPU.


What is the memory management operator in c plus plus?

There is no memory management operator in C++ -- it is an unmanaged language. You use the C++ new operator to allocate memory, and use the C++ delete operator to release previously allocated memory.


What is member fusion in c plus plus?

If you are asking about member functions. When we declare a function inside a class then that function becomes member function of that class and this function can access the whole class


C plus plus uses dynamic memory management?

No, C++ does not use dynamic memory management. The programmer is entirely responsible for releasing dynamic memory when it is no longer required. When static objects fall from scope, their destructors are called automatically, but there is no automatic garbage collection for dynamic objects. Allocated memory remains allocated until the programmer manually releases it, or the thread that owns the memory is terminated.


How do you calculate the size of a class in memory in C plus plus?

Use sizeof( ).


What are new and delete operators in c plus plus?

New and Delete are the memory management operators in c++,like c language we use malloc() and calloc() functions to allocate memory and free() functiong to release the memory similarily we use new to allocate memory in C++ and Delete to release the allocated memory....


How is memory allocated to objects in c plus plus?

In C++ we can allocated objects in static memory, on the stack, or on the heap. It's all the same memory (RAM), the only difference is in when it is allocated. Static memory is allocated at load time and contains all constant variables, static variables and global variables. Static objects are initialised at compile time which creates a bitwise image of each object's initial representation. The image is embedded in the executable and is copied to the allocated memory at load time. If a static object constructor body contains any additional initialisation code that cannot be optimised away by the compiler, that code is execute at runtime. It is vital that static object constructors do not throw.Local objects and function arguments are allocated on the stack at runtime. Every thread of execution has its own stack and the memory is allocated when each thread is instantiated. Stacks are fixed size (determined at compile time) and extend downwards from the highest address while a stack pointer keeps track of the top of the stack (the highest available address). When a function is invoked by a thread, the stack pointer is adjusted, creating a stack frame to store the function's return address, its arguments and its local objects. The size of each stack frame can be determined at compile time.All other objects are allocated on the heap at runtime. We often refer to these objects as dynamic objects. A dynamic object is constructed via operator new which allocates sufficient memory from the free store to accommodate the object's representation. If successful, the object constructor then initialises the memory. If memory cannot be allocated, operator new will either throw a std::bad_alloc exception or will return a nullptr, depending on which overload was invoked. Every class of object has implicit new and delete operators which can be overridden. The default operator new implementations will request memory from the system on an as-required basis while the delete operator releases that memory back to the system. If we wish to provide our own memory management facility, we can allocate uninitialised ("raw") memory at runtime using the global operator new and then construct objects within that memory. This can improve performance by reducing the number of allocation requests we need to make from the system. Once raw memory is allocated we can freely use it as we see fit, however we must manually ensure no two objects occupy the same address space and we must ensure the memory is released as soon as we are finished with it.


What is a distructor in c plus plus?

A destructor destroys an instance of a class to free up memory.