Hi all,
when does the memory gets allocated to the function i.e. at compile time or at run time. If its at compile time the how is it?

Thanks
Tushar

All object memory is allocated at either load time or run time, there really is no compile time allocation (technically there is, but not in the way that you're thinking) from an implementation standpoint. Can you be more specific as to what objects you're talking about when you say "function"?

Suppose we have one class say A.
Class A
{
int I;
public:
int getI();
putI (int x);
};

Before creating the objects of above class, where the class is being stored?

And in the main i created two objects of the above class
int main()
{
A obj1, obj2;
}

So how does the memory gets allocated to the function related to each object?

>Before creating the objects of above class, where the class is being stored?
Technically, classes are just templates for objects. They take up no storage because they don't exist until you create an object. Realistically, classes are stored somewhere in the executable (such as the text segment) so that they can be used to create objects at run-time.

When studying these things, it can sometimes be instructive to turn on the assembler output in your compiler and see what is generated, OR step through your program in the debugger and turn on the assembler window to see what you are stepping though in assembler.

For VC 6, you can turn on assembler output listings by going into Project/Settings/C++/Listing Files and select a listing file type of "Assembly With Source Code". Other versions and other compilers will offer similar sorts of output.

One of the interesting results of this is you can see the difference between 'debug' output and 'release' output. debug output sometimes has extra checking code, and often proceeds in a straight-forward fasion. release mode often does not.

Just what specific incantations the compiler generates to achieve its goals are very dependent on the compiler and target chipset, of course. There is no ONE way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.