CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Sep 2005
    Posts
    15

    Question realloc() and new

    Hello!
    Maybe my question was already discussed.

    When we use malloc()/free() functions we have nice routine to reallocate memory that called realloc().
    How to increase memory size when new/delete are used?

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: realloc() and new

    You would rather use std:vector, or another appropriate STL Container.

    This is a STL Implementation of a Dynamic Array, the size of which can be changed at run-time. You wont have to worry about memory allocations, de-allocations, copying, expansions, etc.

    In addition, std::vector <T> comes with many utility member functions, and can also be used with STL Algorithms to give the programmer access to available, tested and reliable functionality.

    Here are references that will take you forward:




    Last edited by Siddhartha; September 10th, 2005 at 08:00 AM.

  3. #3
    Join Date
    Sep 2005
    Posts
    15

    Re: realloc() and new

    Thanks!
    And how about effectiveness of using vector?
    Is vector fast and good organized?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: realloc() and new

    Quote Originally Posted by Funt
    Thanks!
    And how about effectiveness of using vector?
    Is vector fast
    Yes. It won't be any slower than if you tried to do everything with new and delete along with trying to call realloc.
    and good organized?
    The vector class is the ANSI standard C++ class, so I would think that a lot of thought went into it when it was organized.

    Also, the reason why there is no "realloc" for new and delete is because in C++, you have to recreate and copy the entire set of objects. You can't just do a raw memory copy and move for these types of objects. In other words, to do a realloc safely in C++ you have to:

    1) Create a new area of memory with new[].

    2) Copy the data from the old memory area to the new memory area using "=" in a loop (not memcpy(), since there is no guarantee that the type can use memcpy() to copy the object)

    3) Delete the old memory area.

    But in general, unless you are not using a standard ANSI C++ compiler, there really is no need for new[]/delete[] for the purpose of creating a dynamic array. The std::vector class takes care of these things, including all memory management, and the "realloc" for vector (which is actually called resize()) is safe and comes for free without you having to code anything.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 10th, 2005 at 09:54 AM.

  5. #5
    Join Date
    Sep 2005
    Posts
    15

    Re: realloc() and new

    Thanks a lot!

    I already have implementation of resizing with new[]/delete[] in way you have described.
    In my program I should resize memory only one or few times.
    How do you think, should I rewrite my code with "vector" or leave all is?

  6. #6
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: realloc() and new

    Quote Originally Posted by Funt
    And how about effectiveness of using vector?
    Is vector fast and good organized?
    A vector can be used as a normal array as well. It is as fast as the programmer can make it...

    The first link in my post above (this), and the article, and this one, and many others on this forum and the MSDN links will tell you how to use a vector.

  7. #7
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: realloc() and new

    Quote Originally Posted by Funt
    How do you think, should I rewrite my code with "vector" or leave all is?
    Using vector (and re-writing) is worth it.

    Do so, and when you face problems get back to us...

  8. #8
    Join Date
    Jan 2001
    Posts
    145

    Re: realloc() and new

    Quote Originally Posted by Siddhartha
    You would rather use std:vector, or another appropriate STL Container.
    Why the vector or container is recommended? The question does not show the situation for a list of variables/objects or a centralized management to be created. It could be very possible in a general discrete situation.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: realloc() and new

    Quote Originally Posted by Funt
    Thanks a lot!

    I already have implementation of resizing with new[]/delete[] in way you have described.
    In my program I should resize memory only one or few times.
    How do you think, should I rewrite my code with "vector" or leave all is?
    The issue is not that you are just doing this. The issue is where you are doing this code.

    The problem with using new[]/delete[] is that you leave yourself open for memory leaks. What if you forget the delete[] call? This can happen without you realizing it. Look at this code:
    Code:
    void foo()
    {
        int *p = new int [10];
        if ( something_is_true )
        {
            perform_some_function();
            delete [] p;
        }
    }
    See the problem? The delete is conditional, and there you have a memory leak. What if the code is more complex than this? If you have any code that looks like this, with multiple delete[] calls just to make sure you covered all the exit paths, then this is ripe for mismanagement of memory.

    Also, if your pointer is a member of a class, this is also another place where memory is mismanaged if you don't code a copy constructor and assignment operator that handles these things.

    A vector alleviates all of these problems. There is no need to create a headache on purpose by using new[] and delete[].

    As to the resize, it is very simple. Instead of this (assume T is any basic type of type with default constructor):
    Code:
    void foo()
    {
        T *p = new T [10];
    
        // resize it to 20;
        T *pTemp = new T [20];
        for (int i = 0; i < 10; ++i )
          pTemp[i] = p[i];
       delete [] p;
       p = pTemp;
      //...
    }
    You do this:
    Code:
    #include <vector>
    void foo()
    {
        std::vector<T> p(10);
    
        // resize it to 20;
        p.resize(20);
    }
    Now which is easier to code and maintain? Not only that, you still have to delete [] p at some point, while with vector, it will delete itself when it goes out of scope.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: realloc() and new

    Quote Originally Posted by dunakl
    Why the vector or container is recommended? The question does not show the situation for a list of variables/objects or a centralized management to be created. It could be very possible in a general discrete situation.
    It is recommended to you for STL gives you a ready-made implementation of a dynamic array.

    To get a specific answer, you need to ask a specific question...

  11. #11
    Join Date
    Jan 2001
    Posts
    145

    Re: realloc() and new

    Quote Originally Posted by Siddhartha
    It is recommended to you for STL gives you a ready-made implementation of a dynamic array.

    To get a specific answer, you need to ask a specific question...
    The question is the thread starter did not want a dynamic array, but very possible in a general discrete situation.

  12. #12
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: realloc() and new

    Quote Originally Posted by dunakl
    The question is the thread starter did not want a dynamic array, but very possible in a general discrete situation.
    You need to explain what exactly you mean... In simple words, that is.

    As far as the thread starter not wanting a dynamic array or a container goes - again, please explain how you arrived at this conclusion...

  13. #13
    Join Date
    Jan 2001
    Posts
    145

    Re: realloc() and new

    Quote Originally Posted by Siddhartha
    You need to explain what exactly you mean... In simple words, that is.

    As far as the thread starter not wanting a dynamic array or a container goes - again, please explain how you arrived at this conclusion...
    Quote Originally Posted by Funt
    Hello!
    Maybe my question was already discussed.

    When we use malloc()/free() functions we have nice routine to reallocate memory that called realloc().
    How to increase memory size when new/delete are used?
    This is a quite general question and does not say he want to create a list of variables, which means that it will be very possible in a general use, not specifically in a situation for handling a list of variable like an array.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: realloc() and new

    Quote Originally Posted by dunakl
    The question is the thread starter did not want a dynamic array
    Using new[]/realloc/delete[] is a dynamic array, even if the OP didn't call these sequence of operations a "dynamic array".

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: realloc() and new

    Quote Originally Posted by dunakl
    This is a quite general question and does not say he want to create a list of variables,
    But what they do want to do is equivalent to what vector does. There really is no difference.
    which means that it will be very possible in a general use, not specifically in a situation for handling a list of variable like an array.
    Using new[] automatically implies you are using the pointer in an array-like manner.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured