memory allocation using new operator

C

cikkamikka

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?
 
V

Victor Bazarov

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
 
C

cikkamikka

Victor said:
'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

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?
 
V

Victor Bazarov

[..] 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
 
G

gpriv

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
 
T

Taran

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top