answersLogoWhite

0

How is memory allocated to objects in c plus plus?

Updated: 8/21/2019
User Avatar

Wiki User

7y ago

Best Answer

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.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How is memory allocated to objects in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How c plus plus objects are allocated memory?

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


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.


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 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....


Where the heap memory is allocated in c?

Main Memory (RAM).


Max memory allocated by malloc in c?

Memory is allocated by malloc from the heap.... so max mem = size of heap that is free...


What does malloc return in C and C plus plus?

Address of the allocated area, or NULL.


At what time the memory is allocated for variable in c and c?

Static memory allocation occurs at compile time where as dynamic memory allocation occurs at run time.


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 are the objects used in dev c plus plus?

Objects in Dev C++ are the same as objects in generic C++, insofar as an object is an instance of a class.


When you call new will it internally call delete in c plus plus?

No. Calling new returns a pointer to allocated memory. If you re-use a pointer to store the return value, then you must release the memory that it previously pointed at, either by deleting the pointer, or by maintaining a separate pointer to the original memory. Calling new will not release the current memory for you.


Explain instantiation of objects in c plus plus?

Instantiation of a class literally means creating an instance of a class. This is the process of allocating memory for an object that you can use in your program.