memory allocation using new operator

Discussion in 'C++' started by cikkamikka@gmail.com, Jan 4, 2007.

  1. Guest

    Hi friends,
    Sorry for such basic question. but I wanted to know where does new
    operator or malloc operator allocate memory? in actualy physical Main
    memory or virtual memory?
    , Jan 4, 2007
    #1
    1. Advertising

  2. wrote:
    > Sorry for such basic question. but I wanted to know where does new
    > operator or malloc operator allocate memory? in actualy physical Main
    > memory or virtual memory?


    'malloc' is not an operator. It's a function.

    Both 'new' and 'malloc' allocate memory in "free store". What it is
    depends on your operating system, your compiler (library), and your
    code. You can have your own implementation of 'new' for any class
    you define, which can get its memory anywhere you want.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 4, 2007
    #2
    1. Advertising

  3. Guest

    Victor Bazarov wrote:
    > wrote:
    > > Sorry for such basic question. but I wanted to know where does new
    > > operator or malloc operator allocate memory? in actualy physical Main
    > > memory or virtual memory?

    >
    > 'malloc' is not an operator. It's a function.
    >
    > Both 'new' and 'malloc' allocate memory in "free store". What it is
    > depends on your operating system, your compiler (library), and your
    > code. You can have your own implementation of 'new' for any class
    > you define, which can get its memory anywhere you want.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Correct. There are two things
    1) I get virtual address space
    2) I get virtual memory : here I guess when my Main memory exceeds then
    OS gives me more memory from disk which is used as a page file.

    So I meant to know that when I request for the memory from where the OS
    gives me memory by reserving the space from actual Main memory or from
    page file?
    , Jan 4, 2007
    #3
  4. wrote:
    > [..] There are two things
    > 1) I get virtual address space


    Right. Always. It may or may not be the same as physical space.

    > 2) I get virtual memory : here I guess when my Main memory exceeds
    > then OS gives me more memory from disk which is used as a page file.


    What's the difference between 1 and 2?

    > So I meant to know that when I request for the memory from where the
    > OS gives me memory by reserving the space from actual Main memory or
    > from page file?


    Hold on... You meant to know where the OS gives you memory? How
    should we know? It's a perfect question for the newsgroup dedicated
    to your OS, don't you think?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 4, 2007
    #4
  5. Guest

    Assuming that you using VM platform, on application level you never
    deal with physical memory, it will allways be VM. If you are curious
    where your data will physically reside, it is hard to tell it can be at
    different places at different times (and sometimes even at the same
    time):

    CPU Register
    Internal cash,
    L2 cash,
    DRAM,
    page file

    Hope it helps

    wrote:
    > Hi friends,
    > Sorry for such basic question. but I wanted to know where does new
    > operator or malloc operator allocate memory? in actualy physical Main
    > memory or virtual memory?
    , Jan 4, 2007
    #5
  6. Taran Guest

    wrote:
    > Hi friends,
    > Sorry for such basic question. but I wanted to know where does new
    > operator or malloc operator allocate memory? in actualy physical Main
    > memory or virtual memory?


    When you call new or malloc, the memory is allocated from a free-memory
    pool called 'Heap'. But this does not force you to use this. You can as
    well have your implementation of new/malloc which would allocate
    memory from some other place, say your own array kept seperate for
    memory allocation. How feasible it is, my answer is that's why there's
    new/malloc.

    Virtual Memory as the name itself says is virtual. For a given address
    in the virtual memory you cannot go any physical memory and say this it
    it.
    The underlying operating sytem provides a mapping from this virutal
    memory to the pysical memory. Since a virtual memory is huge as
    compared to the physical memory the operting system does something
    called paging, where it takes some disk space and dumps the pages of
    the process which is not currently executing if the executing process
    needs more physical memory space. The virtual memory helps the
    operating system do this without worrying about the process getting
    screwed up because the address have changed. When a process is paged in
    it might as well be located at a different physical memory than where
    it was before being paged out. The mapping maintained by the underlying
    operating system gaurantees that when the process access some variable
    it will take the right value/variable whether or not it is always in
    the same physical memory.

    HTH
    --
    Taran
    Taran, Jan 5, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lionel
    Replies:
    5
    Views:
    534
    Stephen M. Webb
    Apr 6, 2005
  2. Gaurav Jain

    Memory allocation using New

    Gaurav Jain, Jun 10, 2005, in forum: C++
    Replies:
    7
    Views:
    567
    Peter Koch Larsen
    Jun 11, 2005
  3. Ken
    Replies:
    24
    Views:
    3,828
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    969
    chris
    Oct 28, 2005
  5. Replies:
    8
    Views:
    318
    Neil Cerutti
    Dec 22, 2005
Loading...

Share This Page