Thread: Dynamically create an array?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    25

    Dynamically create an array?

    I was given a programming assignment.. and part of it states:
    "
    1. Read how many students are in the file and dynamically create an array of structure to store the student information.
    "

    I'm not sure what is meant by dynamically create an array?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should use a std::vector, or if that is not allowed, dynamically allocate arrays with new[] and delete[] them when you are done.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    25
    ok thanks (:
    the new[] and delete[] I believe is what my professor was referring to

  4. #4

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Dynamically allocating memory just means you're storing data without knowing (necessarily) how much data you need to store. It also gives the memory indeterminate scope.

    Here's how you create a regular array: (You're probably very familiar with this)
    Code:
    int my_array[5]; // array of 5 integers
    Here's how you dynamically create an array:
    Code:
    int* my_array = new int[5];
    Note that the number in brackets (the 5 in this case) can be a variable.

    When you're done using that memory:
    Code:
    delete [] my_array;
    The reason you'd want dynamic allocation here is because the number of students in the file is variable.
    Last edited by rudyman; 04-25-2008 at 12:24 PM.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Same teacher? http://cboard.cprogramming.com/showthread.php?t=102265

    I think there are more efficient ways of doing this, but I am certain none of them are what your teacher was searching for (bar none, using the stl vector class is the best all-around solution).

    In either event, refer the your classmate's(?) link... Though next time it wouldn't hurt to read other threads before posting. His post has never been more than a couple posts lower than yours on the forums.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    25
    so I'm trying to dynamically create this array of structures...

    should it look something like:

    Code:
    struct* my_array = new my_struct[x];
    where x is the number of students
    ?? or what?? .. I don't really know -_-''

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    more likely
    Code:
    my_struct* my_array = new my_struct[x];
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Click the link I posted above. This person is trying to accomplish the same thing.

    Code:
     struct my_struct* my_array = new my_struct[x];
    ^ That one will even compile too

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by master5001 View Post
    Click the link I posted above. This person is trying to accomplish the same thing.
    struct is optional in C++
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    ^^^^ nobe question but is that not creating a pointer to an array?

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    dynamic array IS a pointer to the first element
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    so in c++ you cant just create a dynamic array?

    int myAry[x];

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    so in c++ you cant just create a dynamic array?
    Not in that way. Standard C++ does not support variable length arrays.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by vart View Post
    struct is optional in C++
    Indeed, however I think its a good idea to illustrate why the OP wasn't totally off.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Ok another question..........

    Sorry dont want to be annoying but i find it so much easier to code when i understand how things work.

    What is the difference that creating a pointed to the array allows you to make a dynamic array?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. How to init an array dynamically on the heap?
    By karantsch in forum C Programming
    Replies: 6
    Last Post: 01-25-2003, 01:07 AM
  3. Replies: 5
    Last Post: 12-05-2002, 02:27 AM
  4. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 03:04 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM