simple memory allocation question

Discussion in 'C++' started by jason, Jun 23, 2005.

  1. jason

    jason Guest

    i'm a little new to VC++, so i'm curious how to appropriate perform the
    following task.

    there is a pointer which i wish you point to a buffer of frequently
    changing size. i'm wondering what is the proper way to initially
    allocate, and then grow that allocation?

    some sample code that might illustrate my intentions:

    DWORD CMyClass::MyEvent(void *ptr, char *stufftoappend)
    {
    if (ptr == NULL)
    {
    //initial allocation here
    ptr = new char[] // like so?
    }
    //grow the allocation based on the size of stufftoappend here?
    }

    thanks in advance for any help,

    jason
    jason, Jun 23, 2005
    #1
    1. Advertising

  2. jason

    Peter Ammon Guest

    jason wrote:
    > i'm a little new to VC++, so i'm curious how to appropriate perform the
    > following task.
    >
    > there is a pointer which i wish you point to a buffer of frequently
    > changing size. i'm wondering what is the proper way to initially
    > allocate, and then grow that allocation?
    >
    > some sample code that might illustrate my intentions:
    >
    > DWORD CMyClass::MyEvent(void *ptr, char *stufftoappend)
    > {
    > if (ptr == NULL)
    > {
    > //initial allocation here
    > ptr = new char[] // like so?
    > }
    > //grow the allocation based on the size of stufftoappend here?
    > }
    >
    > thanks in advance for any help,
    >
    > jason


    The normal approach here is to use a data structure like std::vector,
    which handles details of growing for you.

    If you really want to roll your own, you can use malloc() to create the
    buffer and realloc() whenever you need to grow it. new doesn't have an
    equivalent to realloc().

    -Peter

    --
    Pull out a splinter to reply.
    Peter Ammon, Jun 23, 2005
    #2
    1. Advertising

  3. jason

    jason Guest

    ok, thanks for clarifying that Peter. yeah, i would love to use a
    vector, but the pointer is a little picky about what it can hold, and
    isn't something i can change. however, the malloc/realloc info is
    exactly what i was missing. thanks for stopping me from barking up the
    new/delete tree.

    jason
    jason, Jun 23, 2005
    #3
  4. jason wrote:
    > i'm a little new to VC++, so i'm curious how to appropriate perform the
    > following task.


    If your question is VC++-specific (like language extensions, compiler-
    specific or 'implementation-specific' behaviours, limitations, bugs, etc.)
    then you better post to microsoft.public.vc.language. If your question is
    about C++ in general, as a language, then comp.lang.c++ is just as good
    (or even better in some cases).

    > there is a pointer which i wish you point to a buffer of frequently
    > changing size. i'm wondering what is the proper way to initially
    > allocate, and then grow that allocation?
    >
    > some sample code that might illustrate my intentions:
    >
    > DWORD CMyClass::MyEvent(void *ptr, char *stufftoappend)
    > {
    > if (ptr == NULL)
    > {
    > //initial allocation here
    > ptr = new char[] // like so?


    No, definitely not like so. You pass the pointer by value, there is no
    way you will retain any changes to it. You need to pass it by a reference
    to a pointer. And if you need an array of 'char', why is your pointer
    declared 'void*'? Shouldn't it be 'char*'?

    > }
    > //grow the allocation based on the size of stufftoappend here?


    The C++ language doesn't have any "grow the allocation" mechanism, you
    need to "roll your own":

    else {
    char *newptr = new char[oldsize + howmuchtoadd];
    std::copy(ptr, ptr + oldsize, newptr);
    std::copy(stufftoappend, stufftoappend + howmuchtoadd,
    newptr + oldsize);
    delete[] ptr;
    ptr = newptr;
    }

    > }


    That all said, you probably should use 'std::string' or 'std::vector' for
    your containment needs instead of managing dynamic memory yourself (given
    that you're a beginner, that is).


    V
    Victor Bazarov, Jun 23, 2005
    #4
  5. jason

    jason Guest

    much appreciated. especially the code sample at the bottom.

    yeah, i see now the value/reference mistake in my sample.

    i have some experience with std::vector, not much with string. if the
    pointer won't complain about that type, i will definitely use it.
    thanks again.
    jason, Jun 23, 2005
    #5
  6. jason wrote:
    > i have some experience with std::vector, not much with string. if the
    > pointer won't complain about that type, i will definitely use it.
    > thanks again.


    Maybe you need std::string::data() or std::string::c_str()?

    Or is it complaining due to constness?

    --Phil.
    Phil Endecott, Jun 23, 2005
    #6
  7. jason

    Lionel B Guest

    jason wrote:
    > ok, thanks for clarifying that Peter. yeah, i would love to use a
    > vector, but the pointer is a little picky about what it can hold, and
    > isn't something i can change.


    What exactly is the problem with using std::vector (or std::string, since you seem to be allocating chars)? It would
    make your life much easier to use existing containers that handle memory for you - thus it may well be worth your while
    to resolve type mismatch issues... post some code and perhaps we can help.

    --
    Lionel B
    Lionel B, Jun 24, 2005
    #7
  8. jason

    ben Guest


    > The normal approach here is to use a data structure like std::vector,
    > which handles details of growing for you.


    is std::deque better?

    ben

    >
    > If you really want to roll your own, you can use malloc() to create the
    > buffer and realloc() whenever you need to grow it. new doesn't have an
    > equivalent to realloc().
    >
    > -Peter
    >
    > --
    > Pull out a splinter to reply.
    ben, Jun 24, 2005
    #8
  9. jason

    jason Guest

    thanks for the offer! however i think in this case i'll just use a char
    array. i'm having to do enough pointer math, that managing all of that
    through iterators would be more difficult for me than something that i
    can guarantee is contiguous.
    jason, Jun 24, 2005
    #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. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    701
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    605
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,864
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    989
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,187
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page