Results 1 to 15 of 15

Thread: memory allocation for object

  1. #1
    Junior Member
    Join Date
    Feb 2007
    Answers
    1

    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


  2. #2
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: memory allocation for object

    During compilation.

    -Kalayama

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  3. #3
    Junior Member
    Join Date
    Feb 2007
    Answers
    2

    Thumbs up 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


  4. #4

    Re: memory allocation for object

    at compile time


  5. #5
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    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
    Well Saravanan, if you want the memory to be allocated in run time, you should use commands liks malloc/callac or new.

    Otherwise, memory allocation will always be during compilation.

    -kalayama

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  6. #6
    Contributing Member
    Join Date
    Sep 2006
    Answers
    962

    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


  7. #7
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

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

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  8. #8
    Expert Member
    Join Date
    Nov 2006
    Answers
    518

    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,
    “Then what is runtime memory allocation?”

    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 malloc, calloc or new are used to allocate memory space at runtime as briefed by (kalayama). They are mostly used in the programming logics like data structures.

    Kalayama,

    Your comments on java are absolutely correct.

    Last edited by sutnarcha; 03-15-2007 at 05:21 AM.
    Lack of WILL POWER has caused more failure than
    lack of INTELLIGENCE or ABILITY.

    -sutnarcha-

  9. #9
    Contributing Member
    Join Date
    Mar 2007
    Answers
    34

    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


  10. #10
    Junior Member
    Join Date
    Mar 2007
    Answers
    2

    Re: memory allocation for object

    Quote Originally Posted by reess View Post
    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
    Memory is allocated at runtime


  11. #11
    Expert Member
    Join Date
    Oct 2005
    Answers
    383

    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!

    :)
    NEVER SAY DIE.

  12. #12
    Junior Member
    Join Date
    Dec 2008
    Answers
    1

    Re: memory allocation for object

    memory allocation would be at compile time ..


  13. #13
    Junior Member
    Join Date
    Dec 2008
    Answers
    2

    Re: memory allocation for object

    As it is local variable, memory is allocated at run time.


  14. #14
    Junior Member
    Join Date
    Dec 2008
    Answers
    10

    Thumbs up Re: memory allocation for object

    Quote Originally Posted by reess View Post
    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

    Ans:-- Compile time for the above question


  15. #15

    Re: memory allocation for object

    Its compile time..
    if you use new, then object will be created on runtime.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact