dynamically change the size of an array c++

Go To StackoverFlow.com

1

I have an array defined as;

static double Temp_data[TABLE_SIZE];

I want to change the size of the array according to the user input. Is this possible? Please help me. Thanks.

2012-04-04 04:50
by gishara


8

No. You probably want to use std::vector<double> Temp_data;

Then you can use its resize() member function to set the size as you see fit.

Edit: just to be clear, you generally want to avoid using new directly if you can (and in this case, you can very easily). Direct use of new is a constant source of bugs of quite a few types. std::vector handles quite a few corner cases that are difficult to get correct with new, ensures that the data gets cleaned up when it goes out of scope, etc.

I've said before, and I'll repeat here: at one time, you had little choice but to write code that used new. Now, you do have a choice -- and you should exercise it. Given a modern compiler and standard library, there's almost never any reason to allocate an array with new.

2012-04-04 04:52
by Jerry Coffin
But why do you have to specify size in vector - Rohit Vipin Mathews 2012-04-04 04:55
@Rohit: You don't. You can just start with an empty vector and use push_back to add as many items as you want (well, up to a limit, of course). If, however, the user is supplying a size, you can use resize to make it that size - Jerry Coffin 2012-04-04 04:58
Thanks @JerryCoffin. But I want to use either CArray or Arrays - gishara 2012-04-04 05:01
@Rohit 1. You may want to access the ith element, to do that, you need to resize the vector first. 2. For efficiency concern. If you know the size of the array, you can resize it, better than keep pushing back. (In this case, reserve() maybe a better choice though. - cxyzs7 2012-04-04 05:01
@gishu: CArray? You mean the MFC one, or something else? Why? I use std::vector in MFC programs without any problem at all. Anyway, given those choices, use CArray. It's not as clean as std::vector, but still a lot better than using new directly - Jerry Coffin 2012-04-04 05:02
Yeah, I meant CArray. I also think vector is much more convenient. But there is a function that only accept CArrays or arrays in my project. Thats why I have to use one of them. Thanks - gishara 2012-04-04 05:05
@gishu: You can use a std::vector with a function that requires an array -- when you need to call that function, just give it &myvector[0], or (with a newer compiler) myvector.data() - Jerry Coffin 2012-04-04 05:06
You have reserve and resizebut they aren't really specifying the array size in vector. you can just keep on using vector with out any concern of the size. reserve and resizeare for efficiency concerns - Rohit Vipin Mathews 2012-04-04 05:14
Thanks @JerryCoffin. I can't use myvector.data(). I used &myvector[0] and it is working fine. Btw How do I access all the elements in the vector - gishara 2012-04-04 05:21
@gishu: the usual ways are by index or iterator. I have a feeling I may not understand what you're asking though.. - Jerry Coffin 2012-04-04 05:25
I was asking that, because I need to pass the whole array once. Anyway I will check it. Thanks again for the help - gishara 2012-04-04 05:28
@gishu: you don't normally pass an entire array -- you pass a pointer to the beginning (typically along with something to tell how large it is). In fact, that's the only way to pass a normal array. When you try to specify a parameter of the type "array of X", the compiler will adjust it to "pointer to X" - Jerry Coffin 2012-04-04 05:34
ok, I got it. Thanks a lot. : - gishara 2012-04-04 05:39
@Rohit So you can do std::vector<int> a; a[10] = 10; without using resize - cxyzs7 2012-04-04 23:10
7 is the magic number used in most cases. So for an empty vector there will be memory allocated only until 7 or what ever magic number the algorithm follow - Rohit Vipin Mathews 2012-04-09 05:50


2

Use dynamic memory allocation.

int size;
cin>>size
int *ptr = new int[size];

http://www.cplusplus.com/doc/tutorial/dynamic/

2012-04-04 04:51
by Rohit Vipin Mathews
Not if you can avoid it - Jerry Coffin 2012-04-04 04:52
You have more control on array than vecto - Rohit Vipin Mathews 2012-04-04 05:30
What control would that be - Jerry Coffin 2012-04-04 05:33
Why do you say, You have more control on an array than a vector - Alok Save 2012-04-04 05:34
Sometimes, a vector is overkill. A vector does do everything an array does, but lets you add new elements beyond its current size, resize it, search through it, erase elements, et - Rohit Vipin Mathews 2012-04-04 05:36
@Rohit: So far, it sounds like you think a vector is the one that really gives you more control. In any case, it's at least partly true -- if you really need something that doesn't resize (for example), consider std::array instead. Searching (for one example) isn't part of vector though -- you'd use a standard algorithm, which an apply just as well to an array - Jerry Coffin 2012-04-04 05:42
And i think you prefer using array for performance reasons too - Rohit Vipin Mathews 2012-04-04 05:45
well in a debug build vectors can be slow, but in a release build the difference is almost nothing between vectors and arrays - Eric B 2012-11-19 16:55


2

T *pData

pData = new T[NUM_ELEMENTS];

Basically using the new operator. Read more about new from any C++ reference

2012-04-04 04:53
by AurA
Ads