initialize dynamic arrays in contructors??

This is a discussion on initialize dynamic arrays in contructors?? within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, I have general C++ question. When I am using dynamic arrays is it a good idea to just ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    initialize dynamic arrays in contructors??

    Hi Guys,
    I have general C++ question. When I am using dynamic arrays is it a good idea to just create the array with "new int[1]" for example in Constructor and then resize it where its actually being used? This will make it completely easy as then I will just do "delete []" on it in Destructor. The reason I am asking is I have a class where certain functions return a dynamic array and I will have to remember to do delete [] on them. If I go this path (new in Ctor) then these array will always be deallocated when I destroy my object. Are there any better approaches to this issue?

    Thanks in advance
    -sam

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    is it a good idea to just create the array with "new int[1]" for example in Constructor and then resize it where its actually being used?
    How do you resize it?

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    std::vector maybe?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    When I am using dynamic arrays is it a good idea to just create the array with "new int[1]"

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Using a vector would be easiest. Otherwise, if you aren't using the array, set the pointer to null in the constructor initialization list. Then when you want to use it, resize it as necessary. In the destructor, you can use delete [] to delete it. If the pointer is still null at that time, it will still work, since you are allowed to delete a null pointer with no problems.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Otherwise, if you aren't using the array, set the pointer to null in the constructor initialization list. Then when you want to use it, resize it as necessary..
    How are you able to resize an array?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    When I am using dynamic arrays is it a good idea to just create the array with "new int[1]" for example in Constructor and then resize it where its actually being used?
    Why do you feel restricted to creating the array in the constructor?

    The reason I am asking is I have a class where certain functions return a dynamic array and I will have to remember to do delete [] on them.
    If that's the approach you decide to take, I think you should create the array with the proper size in the function and assign it to a member variable. I don't think that's any different than your constructor approach, and it avoids having to resize.
    Last edited by 7stud; 05-03-2005 at 03:53 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    To resize a dynamic array, simply create a new array with the appropriate new size, copy the necessary data, if any, from the old array to the new temp array, delete the old array, then assign the temp array pointer to the member pointer variable.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Set the pointer to NULL initially. Deleting a NULL pointer is a perfectly safe null-op.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    *this
    Join Date
    Mar 2005
    Posts
    498
    Well you could instead get into storing dynamic data structures, and store your information in seperate nodes and link them together. That way, it will be adjusted to the correct size durring runtime, and if you need to, you could always delete a pointer and return its memory back to the heap.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That would be a linked list instead of a dynamic array, though, which has very different characteristics.

    The standard library provides both - I suspect this is an exercise.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    *this
    Join Date
    Mar 2005
    Posts
    498
    True, but it depends what is being stored in the array or list, I shoud have thought about it more before I posted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct types and dynamic arrays
    By simone.marras in forum C Programming
    Replies: 6
    Last Post: 03-14-2009, 10:56 AM
  2. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  3. From Python's Linked Lists to Dynamic Arrays
    By hexbox in forum C Programming
    Replies: 3
    Last Post: 01-26-2005, 02:14 PM
  4. Dynamic arrays
    By Barjor in forum C# Programming
    Replies: 1
    Last Post: 07-30-2002, 09:56 AM
  5. Dynamic Arrays
    By headexplodes in forum C++ Programming
    Replies: 18
    Last Post: 10-04-2001, 05:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21