CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Dynamic arrays

  1. #1
    Join Date
    Nov 2007
    Posts
    51

    Dynamic arrays

    Hi!

    Is it possible to give values to a dynamic array once you create it?

    Code:
    int * numbers = new int[10];
    numbers[0] = 3;
    numbers[1] = 6;
    numbers[2] = 7;
    numbers[3] = 8;
    numbers[4] = 11;
    numbers[5] = 12;
    numbers[6] = 45;
    numbers[7] = 77;
    numbers[8] = 87;
    numbers[9] = 99;
    Come on, there must be an easier way to do this.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Dynamic arrays

    Is it possible to give values to a dynamic array once you create it?
    Yes, and you have pretty much done it

    Okay, I suspect you are looking for a dynamic array version of array initialisation:
    Code:
    int numbers[10] = {3, 6, 7, 8, 11, 12, 45, 77, 87, 99};
    I do not think it is possible, at least for now. If you really wanted the convenience of an initialiser list, you could initialise a temporary array and then use say, std::copy to copy over the values. With a std::vector, the constructor that takes two iterators could be used.

    Speaking of std::vector, I hope you normally use that (or some other container) instead of a raw dynamically allocated array.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2007
    Posts
    811

    Re: Dynamic arrays

    If using stl and boost:

    Code:
    #include <boost/assign/std/vector.hpp>
    using namespace std;
    using namespace boost::assign;
    
    vector<int> numbers;  
    numbers += 3, 6, 7, 8, 11, 12, 45, 77, 87, 99;

  4. #4
    Join Date
    Nov 2007
    Posts
    51

    Re: Dynamic arrays

    Thank you both for answering!

    I thought that it wouldn't be possible, and now i don't worry about it anymore.

    Thank you laserlight for giving this temporary array idea. It would save lot of time. Good would be to use a function to copy the values of temporary array to
    the new one, then after executing the function the temporary array would be deleted, right?

  5. #5
    Join Date
    May 2007
    Posts
    811

    Re: Dynamic arrays

    I would also add, that anytime you are using new and delete, you should ask yourself, do I need to manage memory manually? The more new and delete you have in your program, more potential for bugs.

  6. #6
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Dynamic arrays

    Stroustrup and Reis made a proposal to the standardization committe for initializer lists, that do what you are trying to do. Thus with the next C++ standard, you will be able to more easily initialize other containers than arrays.

    Read here for more info: http://www.open-std.org/jtc1/sc22/wg...2007/n2220.pdf
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  7. #7
    Join Date
    Nov 2007
    Posts
    51

    Re: Dynamic arrays

    I would also add, that anytime you are using new and delete, you should ask yourself, do I need to manage memory manually? The more new and delete you have in your program, more potential for bugs.
    Yeah, i know.
    But i am not using these for particular program, initializing 10 integers to pointer really doesn't need memory managing.
    I am still learning C++ and reading one book about it. There is an exercise where reader is asked to make a program that uses new to initialize an array of structures and give them values (pain in the ***).
    I just hoped that there is an easier way than initializing them separately.
    Now i know that in the moment there isn't, but thanks to treuss I also know that it may come.

    Anyway, thanks everyone for helping me!

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