Connecting Tech Pros Worldwide Forums | Help | Site Map

Memory allocation with new

Avinash
Guest
 
Posts: n/a
#1: Jul 22 '05
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

lallous
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Memory allocation with new


"Avinash" <Avinash.Kalmani@infineon.com> wrote in message
news:cfbcd41e.0401160701.3ecb9e6f@posting.google.c om...[color=blue]
> 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[/color]

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

--
Elias


gabriel
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Memory allocation with new


Avinash wrote:
[color=blue]
> process is exited. Then I need to free it only if there are more
> memory allocations required by my application.[/color]

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.

--
gabriel
Dario
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Memory allocation with new


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

Yes.
[color=blue]
> If it is allocated from process heap then it will be freed when the
> process is exited.[/color]

Yes.
[color=blue]
> Then I need to free it only if there are more
> memory allocations required by my application.[/color]

Yes.

Tim Slattery
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Memory allocation with new


"lallous" <lallous@lgwm.org> wrote:
[color=blue]
>AFAIK, new[] would call VirtualAlloc() internally...[/color]

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

--
Tim Slattery
Slattery_T@bls.gov
Rolf Magnus
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Memory allocation with new


Avinash wrote:
[color=blue]
> 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?[/color]

It is allocated from the free store, which is often also referred to as
'heap', yes.
[color=blue]
> If it is allocated from process heap then it will be freed when the
> process is exited.[/color]

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.
[color=blue]
> Then I need to free it only if there are more memory allocations
> required by my application.[/color]

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

Avinash
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Memory allocation with new


Rolf Magnus <ramagnus@t-online.de> wrote in message news:<bub5ai$tgq$03$1@news.t-online.com>...[color=blue]
> Avinash wrote:
>[color=green]
> > 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?[/color]
>
> It is allocated from the free store, which is often also referred to as
> 'heap', yes.
>[color=green]
> > If it is allocated from process heap then it will be freed when the
> > process is exited.[/color]
>
> 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.
>[color=green]
> > Then I need to free it only if there are more memory allocations
> > required by my application.[/color]
>
> 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[].[/color]



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
DNA
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Memory allocation with new


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 <ramagnus@t-online.de> wrote in message news:<bub5ai$tgq$03$1@news.t-online.com>...[color=blue]
> Avinash wrote:
>[color=green]
> > 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?[/color]
>
> It is allocated from the free store, which is often also referred to as
> 'heap', yes.
>[color=green]
> > If it is allocated from process heap then it will be freed when the
> > process is exited.[/color]
>
> 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.
>[color=green]
> > Then I need to free it only if there are more memory allocations
> > required by my application.[/color]
>
> 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[].[/color]
Karl Heinz Buchegger
Guest
 
Posts: n/a
#9: Jul 22 '05

re: Memory allocation with new


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.
[color=blue]
>
> Why does microsoft clutter minds of programmers by allowing automatic
> release of allocated memory on process heap?[/color]

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.
[color=blue]
>
> Why do they encourage programmers to not to be careful with memory
> allocations?[/color]

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).
[color=blue]
>
> Why did they provide FreeSysString function for AllocSysString?[/color]

To properly release the resource :-)
[color=blue]
> Anyway
> it is getting released once the process exits.[/color]

Exactly.


--
Karl Heinz Buchegger
kbuchegg@gascad.at
Rolf Magnus
Guest
 
Posts: n/a
#10: Jul 22 '05

re: Memory allocation with new


DNA wrote:
[color=blue]
> Why does microsoft clutter minds of programmers by allowing automatic
> release of allocated memory on process heap?[/color]

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.
[color=blue]
> Why do they encourage programmers to not to be careful with memory
> allocations?[/color]

Do they?
[color=blue]
> Why did they provide FreeSysString function for AllocSysString? Anyway
> it is getting released once the process exits.[/color]

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.
[color=blue]
> Avinash, I suggest you don't follow microsoft s**t.[/color]

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.
[color=blue]
> Just do it plain old C++ way of releasing them yourself.[/color]

Of course.

Kevin Goodsell
Guest
 
Posts: n/a
#11: Jul 22 '05

re: Memory allocation with new


Avinash wrote:
[color=blue]
>
> If the memory is allocated in the process heap, which is freed when
> the application exits.[/color]

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.
[color=blue]
> Where is the memory allocated when we share the
> memory between 2 processes[/color]

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.
[color=blue]
> and still do we need to free this ? Or its
> freed when both the processes exit?[/color]

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

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Closed Thread


Similar C / C++ bytes