Dynamically create an array?

This is a discussion on Dynamically create an array? within the C++ Programming forums, part of the General Programming Boards category; I was given a programming assignment.. and part of it states: " 1. Read how many students are in the ...

  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
    21,390
    You should use a std::vector, or if that is not allowed, dynamically allocate arrays with new[] and delete[] them when you are done.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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? how to double an array size?

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    more likely
    Code:
    my_struct* my_array = new my_struct[x];
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    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++
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    dynamic array IS a pointer to the first element
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,390
    so in c++ you cant just create a dynamic array?
    Not in that way. Standard C++ does not support variable length arrays.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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?

Page 1 of 2 12 LastLast
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, 12:07 AM
  3. Replies: 5
    Last Post: 12-05-2002, 01:27 AM
  4. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 02:04 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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