Memory allocation with new

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Avinash

    Memory allocation with new

    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

    #2
    Re: Memory allocation with new

    "Avinash" <Avinash.Kalman i@infineon.com> wrote in message
    news:cfbcd41e.0 401160701.3ecb9 e6f@posting.goo gle.com...[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...howeve r 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


    Comment

    • gabriel

      #3
      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

      Comment

      • Dario

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

        Comment

        • Tim Slattery

          #5
          Re: Memory allocation with new

          "lallous" <lallous@lgwm.o rg> 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

          Comment

          • Rolf Magnus

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

            Comment

            • Avinash

              #7
              Re: Memory allocation with new

              Rolf Magnus <ramagnus@t-online.de> wrote in message news:<bub5ai$tg q$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

              Comment

              • DNA

                #8
                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$tg q$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]

                Comment

                • Karl Heinz Buchegger

                  #9
                  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

                  Comment

                  • Rolf Magnus

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

                    Comment

                    • Kevin Goodsell

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

                      Comment

                      Working...