Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)? [closed]

I'm trying to make a program which

  1. Takes the user input (let's say all of it is int)
  2. Store it in an array without a starting size (i.e. not -> array[5];); and then
  3. Use the information stored in the array for whatever sinister purposes.

I'm asking for help so that I can learn how to do this on my own if needed.

  • How do I make a dynamic array without a set size?
  • How can I use/access/reach the elements in the above array?

Reading just didn't explain enough for me.

I know it's a very noobish question, and yes, I am a noob, but to change that I need some help.

like image 431
hilchev Avatar asked Nov 17 '12 14:11

hilchev


People also ask

How do you make an array dynamic size?

In dynamic arrays, the size is determined during runtime. Dynamic arrays in C++ are declared using the new keyword. We use square brackets to specify the number of items to be stored in the dynamic array. Once done with the array, we can free up the memory using the delete operator.

Can you change array size dynamically?

Dynamic arrays are very useful data structures. They can be initialized with variable size at runtime. This size can be modified later in the program to expand (or) shrink the array. Unlike fixed-size arrays and Variable Length Arrays, Dynamically sized arrays are allocated in a heap.

How do you create an array with dynamic size in Java?

Creating a Dynamic Array in JavaArrayList<data-type> arr=new ArrayList<>(); Using this, we can create an ArrayList of that specific data type whose initial capacity is 10. Explanation: We have created a dynamic array i.e., an ArrayList of integers arr and then checked its size.


1 Answers

For C++:

If you just need a container just use std:vector. It will take care all the memory allocations necessary for you. However if you want to develop your own dynamic container (whatever reasons you have) you have to take care off the memory allocations yourself. That is, when your array grows you have to allocate new memory chunk, copy present array values to the new memory location and add new values to the newly allocated memory. Usually one wraps this kind of logic inside a separate class e.g. GrowingArray(like standard provided vector class)

EDIT

To elaborate more on my answer (given that you are using this for learning purpose):

store it in an array without a starting size (i.e. not -> array[5];)

Here you want to use something like this: int * myDynamicArray; When a user inputs some values you allocate memory chunk where those values are going to be stored: myDynamicArray = new int[5]; with the size of your initial input. I would as well recommend to save the size of the array in some variable: int arraySize = 5; If later on you want to append new values to your myDynamicArray first of all you have to allocate new memory chunk for grown array (current array elements + new array elements). Lets say you have 10 new values coming. Then you would do: int* grownArray = new int[arraySize+10]; this allocates new memory chunk for grown array. Then you want to copy items from old memory chunk to the new memory chunk and add user appended values (I take it you are using this for learning purposes thus I provided you simple for cycle for copying elemts. You could use std:copy or c like memcopy as well):

int i = 0;
for (; i < arraySize; ++i)
   {
   grownArray[i] = myDynamicArray [i];
   }
// enlarge newly allocated array:
arraySize+= 10;
for (; i < arraySize; ++i)
   {
   grownArray[i] = newValues from somewhere
   }
// release old memory
delete[] myDynamicArray;
// reassign myDynamicArray pointer to point to expanded array
myDynamicArray = gronwArray;
like image 143
Robertas Avatar answered Oct 24 '22 21:10

Robertas