Re: How to renew a memory in C++?

Discussion in 'C++' started by SG, Sep 8, 2009.

  1. SG

    SG Guest

    On 8 Sep., 08:32, Water Lin <> wrote:
    > I can use malloc to alloc a memory block and use realloc to resize the
    > block in C. In C++, I use new to alloc a memory block dynamicly. But is
    > there any way to realloc the memory block like realloc in C++?


    No. That's what vector::resize is for.

    > BTW: If I use realloc to alloc the memory block, can I use delete [] to
    > delete them? Or I can just use free()?


    No.

    Just use google on "realloc" and "c++" and you'll find plenty of
    discussions about it.

    Cheers!
    SG
     
    SG, Sep 8, 2009
    #1
    1. Advertising

  2. SG

    Jorgen Grahn Guest

    On Mon, 7 Sep 2009 23:42:01 -0700 (PDT), SG <> wrote:
    > On 8 Sep., 08:32, Water Lin <> wrote:
    >> I can use malloc to alloc a memory block and use realloc to resize the
    >> block in C. In C++, I use new to alloc a memory block dynamicly. But is
    >> there any way to realloc the memory block like realloc in C++?

    >
    > No. That's what vector::resize is for.


    And the more detailed version:

    What problem are you trying to solve? Most things which you use
    realloc() for in C have much better solutions in C++, using the
    standard containers (including but not limited to std::vector).

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Sep 8, 2009
    #2
    1. Advertising

  3. SG

    Jerry Coffin Guest

    In article <>, says...

    [ ... ]

    > If I use alloc() or realloc() to create a memory block, can I use delete
    > [] to free this memory block? Will there be any memory leak?


    Presumably for 'alloc' you meant 'malloc' or (possibly) 'calloc'.

    In any case, if you didn't obtain the block of memory via new[],
    invoking delete[] on it causes undefined behavior. In reality, the
    result will often be corrupting the entire heap rather than just
    leaking the memory.

    --
    Later,
    Jerry.
     
    Jerry Coffin, Sep 9, 2009
    #3
  4. SG

    SG Guest

    On 9 Sep., 08:21, Water Lin <> wrote:
    >
    > So I think I can't use realloc to alloc memory and then use delete []
    > to release memory block.


    Right.

    > So I still prefer to use "new" to alloc memory block. But is there any
    > way to renew the memory block like realloc() when I feel it is not big
    > enough?


    No, there is NO way. You really should make yourself familiar with
    http://www.cplusplus.com/reference/stl/vector/

    Cheers,
    SG
     
    SG, Sep 9, 2009
    #4
  5. In message <>, Water Lin <>
    writes
    >Jerry Coffin <> writes:
    >
    >> In article <>, says...
    >>
    >> [ ... ]
    >>
    >>> If I use alloc() or realloc() to create a memory block, can I use delete
    >>> [] to free this memory block? Will there be any memory leak?

    >>
    >> Presumably for 'alloc' you meant 'malloc' or (possibly) 'calloc'.
    >>
    >> In any case, if you didn't obtain the block of memory via new[],
    >> invoking delete[] on it causes undefined behavior. In reality, the
    >> result will often be corrupting the entire heap rather than just
    >> leaking the memory.

    >
    >Yes, I mean malloc(). Sorry for that.
    >
    >So I think I can't use realloc to alloc memory and then use delete []
    >to release memory block.
    >
    >So I still prefer to use "new" to alloc memory block.


    That's not what new does.

    new and new[] don't (just) allocate a memory block, they construct
    objects. Similarly delete and delete[] don't (just) deallocate memory,
    they destroy objects. This is an important difference which will cause
    all kinds of trouble if you don't understand it.


    > But is there any
    >way to renew the memory block like realloc() when I feel it is not big
    >enough?


    Use std::vector instead of new[]. There is rarely any reason for using
    new[] or delete[], unless you are writing low-level library code.

    --
    Richard Herring
     
    Richard Herring, Sep 9, 2009
    #5
  6. SG

    peter koch Guest

    On 9 Sep., 10:53, Richard Herring <junk@[127.0.0.1]> wrote:
    > In message <>, Water Lin <>
    > writes
    >
    >
    >
    >
    >
    > >Jerry Coffin <> writes:

    >
    > >> In article <>, says...

    >
    > >> [ ... ]

    >
    > >>> If I use alloc() or realloc() to create a memory block, can I use delete
    > >>> [] to free this memory block? Will there be any memory leak?

    >
    > >> Presumably for 'alloc' you meant 'malloc' or (possibly) 'calloc'.

    >
    > >> In any case, if you didn't obtain the block of memory via new[],
    > >> invoking delete[] on it causes undefined behavior. In reality, the
    > >> result will often be corrupting the entire heap rather than just
    > >> leaking the memory.

    >
    > >Yes, I mean malloc(). Sorry for that.

    >
    > >So I think I can't use realloc to alloc memory and then use delete []
    > >to release memory block.

    >
    > >So I still prefer to use "new" to alloc memory block.

    >
    > That's not what new does.
    >
    > new and new[] don't (just) allocate a memory block, they construct
    > objects. Similarly delete and delete[] don't (just) deallocate memory,
    > they destroy objects. This is an important difference which will cause
    > all kinds of trouble if you don't understand it.
    >
    > > But is there any
    > >way to renew the memory block like realloc() when I feel it is not big
    > >enough?

    >
    > Use std::vector instead of new[]. There is rarely any reason for using
    > new[] or delete[], unless you are writing low-level library code.
    >

    For low-level code, you'd normally use operator new. operator new[] is
    now largely useless for all normal code. You'd use std::vector instead
    (which will not call new[]).

    /Peter
    > --
    > Richard Herring
     
    peter koch, Sep 9, 2009
    #6
  7. SG

    Jerry Coffin Guest

    In article <>, junk@[127.0.0.1]
    says...

    [ ... ]
    > Use std::vector instead of new[]. There is rarely any reason for
    > using new[] or delete[], unless you are writing low-level library
    > code.


    Even _in_ low-level library code, there's rarely a reason to use new
    [] or delete[]. In fact, I'd be a lot more likely to use malloc!

    For low-level library code (e.g. implementing something similar to
    std::vector) you normally want to allocate blocks of raw memory, then
    use placement new to create objects in that memory. For that purpose,
    malloc works perfectly well -- but unfortunately, realloc does NOT,
    because it does a bitwise copy of the memory contents when/if it
    needs to move a block, and that can wreak havoc with anything other
    than POD objects.

    --
    Later,
    Jerry.
     
    Jerry Coffin, Sep 9, 2009
    #7
  8. SG

    James Kanze Guest

    On Sep 9, 5:39 pm, Jerry Coffin <> wrote:
    > In article <>, junk@[127.0.0.1]
    > says...


    > [ ... ]


    > > Use std::vector instead of new[]. There is rarely any reason
    > > for using new[] or delete[], unless you are writing
    > > low-level library code.


    > Even _in_ low-level library code, there's rarely a reason to
    > use new [] or delete[]. In fact, I'd be a lot more likely to
    > use malloc!


    Or the operator new() function. I tend to avoid malloc and free
    except in code implementing operator new() and operator
    delete().

    > For low-level library code (e.g. implementing something
    > similar to std::vector) you normally want to allocate blocks
    > of raw memory, then use placement new to create objects in
    > that memory. For that purpose, malloc works perfectly well --


    Sort of. It's a lot more awkward with regards to handling out
    of memory errors, and if the program installs a global operator
    new/operator delete (e.g. to track memory usage), malloc/free
    side steps it.

    --
    James Kanze
     
    James Kanze, Sep 10, 2009
    #8
  9. SG

    Jerry Coffin Guest

    In article <3a6b5833-3894-4248-ac3a-15ce9b041459
    @o41g2000yqb.googlegroups.com>, says...
    >
    > On Sep 9, 5:39 pm, Jerry Coffin <> wrote:
    > > In article <>, junk@[127.0.0.1]
    > > says...

    >
    > > [ ... ]

    >
    > > > Use std::vector instead of new[]. There is rarely any reason
    > > > for using new[] or delete[], unless you are writing
    > > > low-level library code.

    >
    > > Even _in_ low-level library code, there's rarely a reason to
    > > use new [] or delete[]. In fact, I'd be a lot more likely to
    > > use malloc!

    >
    > Or the operator new() function. I tend to avoid malloc and free
    > except in code implementing operator new() and operator
    > delete().


    Oh, quite true -- I'm not saying I'd normally use malloc, only that I
    really would NOT normally use new[]. For implementing something like
    vector, I'd probably rank things (from most frequent to least
    frequent) something like this:

    1) operator new()
    2) something system-specific (sbrk, VirtualAlloc, etc.)
    3) malloc (probably in conjunction with realloc)
    4) new[]

    Some might wonder why that ranking. The global operator new function
    is most common simply because it's the default way to allocate blocks
    of memory in general. Lacking some specific reason to do otherwise,
    it's what the user will normally expect.

    Something system-specific comes next, because if you aren't going to
    use the global operator new function, you probably need some other
    specific behavior, and going directly to the system will usually be
    about the only way you can get what you need.

    The next possibility is malloc because it does provide _one_
    capability that the global operator new function doesn't -- the
    possibility of expanding a block in place. Therefore, if you have
    something like a vector of POD objects, you can use it for the memory
    management and have some chance of improving efficiency.

    new[] ends up last, because I can't think of any situation in which
    it provides an advantage over the other possibilities. Some early
    books on C++ (e.g. one by Scott Meyers, IIRC) did use 'new char
    [size]' to allocate a block of "raw" memory. IIRC, in one of his
    later books he repudiated that though. Virtually all the code I've
    written that used this predates (widespread availability of) the
    standard containers.

    --
    Later,
    Jerry.
     
    Jerry Coffin, Sep 11, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A Traveler

    Renew IIS session with an image request?

    A Traveler, Oct 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    577
    George Hester
    Oct 12, 2004
  2. TheDude5B

    renew the sessionID

    TheDude5B, Mar 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    469
    Alvin Bruney - ASP.NET MVP
    Mar 9, 2006
  3. Anonymous Sender

    gnarlodious--did you try to renew?

    Anonymous Sender, May 26, 2004, in forum: HTML
    Replies:
    1
    Views:
    308
    Gnarlodious
    May 26, 2004
  4. Tarapia Tapioco

    gnarlodious--did you try to renew?

    Tarapia Tapioco, May 26, 2004, in forum: HTML
    Replies:
    0
    Views:
    334
    Tarapia Tapioco
    May 26, 2004
  5. pradeep

    renew() an array?

    pradeep, Apr 25, 2008, in forum: C Programming
    Replies:
    6
    Views:
    893
    Keith Thompson
    Apr 26, 2008
Loading...

Share This Page