Calling constructor to allocate memory

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Calling constructor to allocate memory

ajksdf
"When calling constructor, compiler will request OS to allocate memory space to hold the new object. OS return the base address and then compiler will assign it 'this' segment."

When I come across above words in the book, some questions arise.

1. Does this process happen in every object-oriented language like Javascript, Java...
2. 'Compiler will request OS to allocate memory'. Does compiler request OS by translating the code into VM code? From my standpoint, compiler just do the translation stuff passively, like taking the input and providing output. Hard to imagine it is actively 'requesting' something.


Thank you very much in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Calling constructor to allocate memory

WBahn
Administrator
Lots of different ways to do it.

One way is that the compiler includes the memory management functions into the program being compiled. When the program is launched, it is given a location in memory and has so much memory. In the old days, this might be determined by the "memory model" that the compiler was set to use.

Today the actual OS is much more involved and an OS call can be made to allocate additional memory and, as far as the process that the program is running in can tell, it's memory space has simply grown by some amount. That is not done to allocate memory for each new object that is created -- it's done to get big chunks of new memory that the program's memory manager then deals with.
Reply | Threaded
Open this post in threaded view
|

Re: Calling constructor to allocate memory

ajksdf
The concept of program's memory manager is from a field in OS's memory class or a separate class in OS?
Reply | Threaded
Open this post in threaded view
|

Re: Calling constructor to allocate memory

WBahn
Administrator
Elements of both. Overall computer memory management is an OS-level task. But at that level the manager is concerned with just the total amount of memory given to a program/process as a whole. How that memory gets allocated/deallocated beyond that is up to the program/process.