How to re define array length during program execution?

Hi!

As title suggest, I want to define array length during program execution.

First of all let me explane. I was practicing programs examples and exercise, when I came across a question which was about showing a number in digit form. Ever solution I can think of, contain predefined length of array. For example I declare an array length of 20. User enter 30 digit number or 60 digit number. Their is always limitation.

I tried different techniques like initializing array length with a variable. But it did not work. Another technique is to declare dynamic array, which I don't think will work.

Is their any way to define an array after program initialization?

IS now their anyway way to accomplish it? Define length of array during program execution?

Thank's for your time.
Faizan
Another technique is to declare dynamic array, which I don't think will work.

Is their any way to define an array after program initialization?

Dynamic memory allocation is the answer.

For example I declare an array length of 20. User enter 30 digit number or 60 digit number. Their is always limitation.

There is fixed limit for static array. The question is, can you decide a limit that is feasible and works for most inputs? For example, fixed size of 100 would handle all those example inputs. However, automatic variables are within stack memory and that being limited prevents the use of absurdly large statically allocated arrays.

Dynamic allocation is the answer. There are two ways to do it. One is manual memory management. The one that you have probably heard of.

The other way is to let someone else to do the hard chores on your behalf. Someone else, like std::vector.
automatic variables are within stack memory and that being limited prevents the use of absurdly large statically allocated arrays.

I can't understand. Can you be more precise?
The "stack" is the memory space where newly created variables, arrays, class objects etc.. are kept while your program is running. When you create an array and tell the computer that the array has 1000 doubles in it, well, the computer allocates a space on the stack large enough to hold that array as if it were full. Not a big deal for our small practice programs, but it is a huge concern in industry where arrays could have millions of elements.

The cure is dynamically created arrays or vectors. Both are easier to resize as the number of elements change in the program.
Topic archived. No new replies allowed.