Dynamic Memory Array

This is a discussion on Dynamic Memory Array within the C++ Programming forums, part of the General Programming Boards category; Could somebody please clarify my thoughts on this. With arrays, you can either declare a constant value of them such ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Dynamic Memory Array

    Could somebody please clarify my thoughts on this.

    With arrays, you can either declare a constant value of them such as [10] which is hard coded into the program. If I wanted variable amount of memory at runtime, I would define the array as 'new int[n]' for example, and then define n at run time. Is that correct?

    Thanks.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    yes, you but you need to declare a variable of type int*

    like

    Code:
    int* myArray = NULL;
    int n = 10;
    
    myArray = new int[n];
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by rogster001 View Post
    yes, you but you need to declare a variable of type int*

    like

    Code:
    int* myArray = NULL;
    int n = 10;
    
    myArray = new int[n];
    But at run time, I could update the value of n before the array is created?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    You could do that. Also remember to use nullptr and not NULL.
    If you only really need dynamic memory (ie an array whose size will expand if you need to store more data in it), std::vector is fine.
    And if you don't use them, don't forget smart pointers!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    You could do that. Also remember to use nullptr and not NULL.
    If you only really need dynamic memory (ie an array whose size will expand if you need to store more data in it), std::vector is fine.
    And if you don't use them, don't forget smart pointers!
    Thanks. Yeah, I would definately almost always use vectors. I'm just practising memory management with new/delete.

    I've not got as far as smart pointers just yet. Do they perform similar tasks to what vectors do? Ie, knowing array size, etc to help with out of bounds?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    They release memory for you automatically.
    Code:
    {
        std::shared_ptr<int> p(new int);
    } // Memory is released
    No more memory leaks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    They release memory for you automatically.
    Code:
    {
        std::shared_ptr<int> p(new int);
    } // Memory is released
    No more memory leaks.
    Okay, thats handy. A little bit similar to garbage collection that i've read about in other languages?

    Should I always try to use smart pointers as above, in place of the standard type?

    Thanks.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    Somewhat. The fundamental difference is deterministic destruction. We know that the memory will be released when the last pointer to the memory is destroyed. Hence we will only be using as much memory as we need at any point in time. Garbage collection typically runs now and then and gathers unused memory and frees it. That means at any given point in time, your application may be using more memory than you need / should be using.

    Yes, you should always strive to use smart pointers with raw pointers. This becomes fundamentally important later when when you start using exceptions and multiple exit paths in your functions. When you use smart pointers, you can be sure that your memory will be freed, no matter how you exit your functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks. Will have a read up on them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic array allocation
    By Maulrus in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2010, 05:08 PM
  2. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. When the memory needed for an array increases...
    By Xzyx987X in forum C Programming
    Replies: 3
    Last Post: 04-03-2004, 01:19 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM

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