Memory allocation with new

A

Avinash

Hi,
I have written an application in VC 6.0. I have allocated some
memory using new operator.

1.When i use new operator is the memory allocated from process heap?
If it is allocated from process heap then it will be freed when the
process is exited. Then I need to free it only if there are more
memory allocations required by my application.

Any details, comments and links are welcome.

Regards,
Avinash
 
L

lallous

Avinash said:
Hi,
I have written an application in VC 6.0. I have allocated some
memory using new operator.

1.When i use new operator is the memory allocated from process heap?
If it is allocated from process heap then it will be freed when the
process is exited. Then I need to free it only if there are more
memory allocations required by my application.

Any details, comments and links are welcome.

Regards,
Avinash

Hello,

AFAIK, new[] would call VirtualAlloc() internally...
Yes the memory will be freed when your program exists...however it is a bad
practice to leave allocated memory unallocated when not needed anymore
because you will make the whole system slow not to mention problems created
from memory leaks...
 
G

gabriel

Avinash said:
process is exited. Then I need to free it only if there are more
memory allocations required by my application.

Creating memory leaks is always a bad idea. For many reasons.

If you would prefer to not be careful about memory usage, then C++ is the
wrong language.
 
D

Dario

1.When i use new operator is the memory allocated from process heap?

Yes.
If it is allocated from process heap then it will be freed when the
process is exited.
Yes.

Then I need to free it only if there are more
memory allocations required by my application.

Yes.
 
T

Tim Slattery

lallous said:
AFAIK, new[] would call VirtualAlloc() internally...

VirtualAlloc is a Windows API, so if this statement is true, it's true
only for Windows platforms.
 
R

Rolf Magnus

Avinash said:
Hi,
I have written an application in VC 6.0. I have allocated some
memory using new operator.

1.When i use new operator is the memory allocated from process heap?

It is allocated from the free store, which is often also referred to as
'heap', yes.
If it is allocated from process heap then it will be freed when the
process is exited.

Usually, yes. But the objects are not properly destroyed, i.e. if they
are class instances that have a destructor that needs to do important
things like closing a log file, that won't be done.
Then I need to free it only if there are more memory allocations
required by my application.

You should do it the Right Way (tm) from the beginning and not let bad
habits grow up. Use delete on every object you got from new and
delete[] on every array you got from new[].
 
A

Avinash

Rolf Magnus said:
Avinash said:
Hi,
I have written an application in VC 6.0. I have allocated some
memory using new operator.

1.When i use new operator is the memory allocated from process heap?

It is allocated from the free store, which is often also referred to as
'heap', yes.
If it is allocated from process heap then it will be freed when the
process is exited.

Usually, yes. But the objects are not properly destroyed, i.e. if they
are class instances that have a destructor that needs to do important
things like closing a log file, that won't be done.
Then I need to free it only if there are more memory allocations
required by my application.

You should do it the Right Way (tm) from the beginning and not let bad
habits grow up. Use delete on every object you got from new and
delete[] on every array you got from new[].



Hi There,
Thank you for answering my queries. But I know that good programming
practice is to free memory when the application exits. But now i have
few more queries

If the memory is allocated in the process heap, which is freed when
the application exits. Where is the memory allocated when we share the
memory between 2 processes and still do we need to free this ? Or its
freed when both the processes exit?


Thanks and Regards,
Avinash
 
D

DNA

Why does microsoft clutter minds of programmers by allowing automatic
release of allocated memory on process heap?

Why do they encourage programmers to not to be careful with memory
allocations?

Why did they provide FreeSysString function for AllocSysString? Anyway
it is getting released once the process exits.

Avinash, I suggest you don't follow microsoft s**t. Just do it plain
old C++ way of releasing them yourself.



Rolf Magnus said:
Avinash said:
Hi,
I have written an application in VC 6.0. I have allocated some
memory using new operator.

1.When i use new operator is the memory allocated from process heap?

It is allocated from the free store, which is often also referred to as
'heap', yes.
If it is allocated from process heap then it will be freed when the
process is exited.

Usually, yes. But the objects are not properly destroyed, i.e. if they
are class instances that have a destructor that needs to do important
things like closing a log file, that won't be done.
Then I need to free it only if there are more memory allocations
required by my application.

You should do it the Right Way (tm) from the beginning and not let bad
habits grow up. Use delete on every object you got from new and
delete[] on every array you got from new[].
 
K

Karl Heinz Buchegger

DNA wrote:

Please don't top post.
Put your reply beneath the text you are replying to and snip
what you no longer need in the reply.
Why does microsoft clutter minds of programmers by allowing automatic
release of allocated memory on process heap?

What are you talking about?
It's quite common for an operating system, to simply free the
allocated memory after a program has exited. It is a sort of
defense strategy against programming errors.
Why do they encourage programmers to not to be careful with memory
allocations?

Because it is very simple to miss a few deallocations. From the point
of view of the operating system, this is unacceptable. Thus it does
the simplest strategy available: If a program terminates, release all
resources allocated to that program (if possible).
Why did they provide FreeSysString function for AllocSysString?

To properly release the resource :)
Anyway
it is getting released once the process exits.

Exactly.
 
R

Rolf Magnus

DNA said:
Why does microsoft clutter minds of programmers by allowing automatic
release of allocated memory on process heap?

Probably because they haven't gone completely insane (though I'm still
not sure about that). A basic concept of modern operating systems is
that a process can't take the whole system down. If a badly written
program could just leak memory that the system cannot get back even
after that program terminated, that system would be pure crap and
useless when run for longer than a few hours.
Why do they encourage programmers to not to be careful with memory
allocations?

Do they?
Why did they provide FreeSysString function for AllocSysString? Anyway
it is getting released once the process exits.

Not everyone wants _all_ the program's memory stay allocated until that
program finishes. And in C++, you might want your objects properly
destroyed and not just vanishing whithout the destructor being called.
Avinash, I suggest you don't follow microsoft s**t.

I don't. I'm using Windows once every few months or so. But any other OS
that I know also release your program's memory and closes its files and
all that stuff when it exits, or when it crashes.
Just do it plain old C++ way of releasing them yourself.

Of course.
 
K

Kevin Goodsell

Avinash said:
If the memory is allocated in the process heap, which is freed when
the application exits.

Neither of these is required by the C++ standard. If you want to talk
about such things, you should do so in a group that discusses your C++
implementation.
Where is the memory allocated when we share the
memory between 2 processes

There are no separate processes in C++. If you want to talk about such
things, you should do so in a group that discusses your C++ implementation.
and still do we need to free this ? Or its
freed when both the processes exit?

These are not meaningful questions in this group, since they assume
things that are not true in standard C++.

-Kevin
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top