Click to See Complete Forum and Search --> : Dynamic arrays
Jpsarapuu
February 7th, 2008, 11:20 PM
Hi!
Is it possible to give values to a dynamic array once you create it?
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.
laserlight
February 7th, 2008, 11:51 PM
Is it possible to give values to a dynamic array once you create it?
Yes, and you have pretty much done it :p
Okay, I suspect you are looking for a dynamic array version of array initialisation:
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.
STLDude
February 8th, 2008, 01:14 AM
If using stl and boost:
#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;
Jpsarapuu
February 8th, 2008, 06:47 AM
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?
STLDude
February 8th, 2008, 09:57 AM
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.
treuss
February 8th, 2008, 10:08 AM
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/wg21/docs/papers/2007/n2220.pdf
Jpsarapuu
February 8th, 2008, 02:30 PM
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!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.