469,945 Members | 2,262 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

memory allocation using new operator

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 '07 #1
5 1934
ci********@gmail.com 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
Jan 4 '07 #2

Victor Bazarov wrote:
ci********@gmail.com 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 '07 #3
ci********@gmail.com 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
Jan 4 '07 #4
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

ci********@gmail.com 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 '07 #5
ci********@gmail.com 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

Jan 5 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Riadh Elloumi | last post: by
8 posts views Thread by Berk Birand | last post: by
5 posts views Thread by Lionel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.