memory allocation for object
hi,
please clear me in this basic issue.
when the memory will be allocated to an object?
is it at compile time or runtime?
eg:
class one
{
public:
int i;
private:
one();
onemore();
}
int main()
{
one ob1;
retun 0;
}
so fir this case when will be the memory allocated for this object ob1?
thanks n regards
rees
Re: memory allocation for object
During compilation.
-Kalayama
Re: memory allocation for object
when object is created the memory will be allocated only at runtime and it cleared after terminating frm the output
Re: memory allocation for object
Re: memory allocation for object
[QUOTE]when object is created the memory will be allocated only at runtime and it cleared after terminating frm the output[/QUOTE]
Well Saravanan, if you want the memory to be allocated in run time, you should use commands liks [B]malloc/callac[/B] or [B]new[/B].
Otherwise, memory allocation will always be during compilation.
-kalayama
Re: memory allocation for object
In C++, Memory allocation at compilation time. In java, Memory allocation at runtime. This is the main reason in java they are not using multiple inheritance.
-------------------------
suresh
Re: memory allocation for object
[QUOTE]"In C++, Memory allocation at compilation time. In java, Memory allocation at runtime. This is the main reason in java they are not using multiple inheritance."[/QUOTE]
Well that's not entirely a true statement. In C++, memory allocation can be both runtime as well as compile time. One can use the new operator for doing runtime allocation.
When it comes to java, it is not a compiled language, it is an interpreted language. That means you need the javaruntime/virtual machine to run the java codes.
Correct me if I am wrong. (Specially my Java knowledge is limited)
-Shiv
Re: memory allocation for object
Compilation is the process of converting the source program into an object code (.obj). So when a C/C++ source code is compiled, it is checked for the syntax and semantic errors in it. If not found the .obj file is created.
This .obj file is later linked to the library files (.lib) in the process of linking to make an executable file (.exe).
Now, on a C/C++ source program editor s/w like TurboC, when a program is run, it first compiles it to make .obj, then links it to the library to make .exe and later runs the program. After the process of compilation and linking and before executing the first executable statement in the program, the system allocates memory for the variables and objects as required.
reess,
in your example code, the memory is allocated for ‘ob1’ as is described above. This memory is cleared when the program is terminated as briefed by (saravanan.ct)
After all this explanations, there may be questions like,
[B]“Then what is runtime memory allocation?”[/B]
The answer is that, it is very much possible that the memory allocated as above is not sufficient and there would be a need for more memory space during the course of program execution for the every changing and ever increasing business world that we are in. So there will be need for more memory space at runtime of the program. Then the options like [B]malloc, calloc or new[/B] are used to allocate memory space at runtime as briefed by (kalayama). They are mostly used in the programming logics like [B]data structures.[/B]
Kalayama,
Your comments on [B]java[/B] are absolutely correct.:cool:
Re: memory allocation for object
the memory allocation in c & c++ is for run tyime by using malloc or calloc or in java using a new operator. what i think as iam studying oopss:o
Re: memory allocation for object
[QUOTE=reess;8181]hi,
please clear me in this basic issue.
when the memory will be allocated to an object?
is it at compile time or runtime?
eg:
class one
{
public:
int i;
private:
one();
onemore();
}
int main()
{
one ob1;
retun 0;
}
so fir this case when will be the memory allocated for this object ob1?
thanks n regards
rees[/QUOTE]
Memory is allocated at runtime
Re: memory allocation for object
in java memeory is allocated to object at run time;
if u are familiar with java, then you must have come across the keyword 'new'
which creates the objects and allocates memeory to it!
See objects are allocated memory at runtime!
Re: memory allocation for object
memory allocation would be at compile time ..
Re: memory allocation for object
As it is local variable, memory is allocated at run time.
Re: memory allocation for object
[QUOTE=reess;8181]hi,
please clear me in this basic issue.
when the memory will be allocated to an object?
is it at compile time or runtime?
eg:
class one
{
public:
int i;
private:
one();
onemore();
}
int main()
{
one ob1;
retun 0;
}
so fir this case when will be the memory allocated for this object ob1?
thanks n regards
rees[/QUOTE]
Ans:-- Compile time for the above question
Re: memory allocation for object
Its compile time..
if you use new, then object will be created on runtime.