Trying to create an infinite array

I have been trying to make an infinite array which I will add a random value (1-4), into it.
For some reason I keep getting weird values in the serial monitor. 0s, numbers in the 100s,
and negative numbers in the array when I use a for loop to check for all values in the array.
Is there a flaw to the code, I have been trying to find something wrong with it for a while.
If there is a much easier way to do this, how so?

int increment = 0 ;
int incremental;
int storage [1] = {1} ;
int temp [1];

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode (clockPinA, OUTPUT);
pinMode (dataPinA, OUTPUT);
pinMode (latchPinA, OUTPUT);

pinMode (clockPinB, OUTPUT);
pinMode (dataPinB, OUTPUT);
pinMode (latchPinB, OUTPUT);

pinMode (topL, INPUT);
pinMode (topR, INPUT);
pinMode (bottomR, INPUT);
pinMode (bottomL, INPUT);
randomSeed(analogRead(1));
storage [0] = random (1, 5);
}

//infinite array thing a ma jig
for (int v = 0; v < increment; v++) {
temp [v] = storage [v];
Serial.println (temp [v]);
}
increment ++;
int storage [increment];
incremental = (increment - 1 );
delay(1000);
for (int v = 0; v < incremental; v++)
{
storage [v] = temp [v];
}
int temp [incremental];
randomNum = random (1, 5);
storage [incremental] = randomNum;
//Serial.println(storage [incremental]);

for (int f = 0; f < incremental ; f++)
{
Serial.println(storage[f]);
delay(500);
}

I would show you how to do it but it would take forever

maninderlall:
I have been trying to make an infinite array which I will add a random value (1-4), into it.

If there is a much easier way to do this, how so?

Yes, you need an Arduino with an infinte amount of memory.

Always show us your current compete sketch. Use CTRL T to format the sketch. Please use code tags. Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

.

Storage has room for 1 element. Saying storage[2] does not magically make storage bigger.

Check out realloc(), though I strongly discourage it on the arduino.

You cannot make an array of infinite size on a device with a finite amount of memory.

You can - if you must - dynamically allocate memory with malloc/realloc/free - but this has some serious potential pitfalls, and is not for the novice programmer, which it sounds like you are. I try to allocate only arrays of fixed, known size, and bounds-check religiously...

What are you trying to do? There is probably a way to do it that doesn't involve an "infinite" array.

maninderlall:
I have been trying to make an infinite array...

I would first suggest getting the arduino to operate at an infinite speed.

which my bank account was infinite

check out how to use a circular buffer

I need you to explain how this:

int storage [1] = {1} ;
int temp [1];

is different from this:

int storage = 1;
int temp;

from a practical standpoint.

What everyone else said.

C++ is not a scripting language. In most scripting languages (javascript, ruby), arrays are simply dictionaries whose keys are numbers, and the language supports adding items to dictionaries "under the hood", so to speak. This is not the case on C++. In C++, an array is an allocated chunk of memory divided into N subchunks with a size.

To do what you want - dynamically resize an array - you will either have to use malloc/realloc (which is strongly discouraged for the novice programmer) or use a C++ library class (std::Vector, I think). Either way, if you keep adding elements to the array, eventually that will fail and you have to think about what happens when it does.

If you need to store a lot of data, use an SD card.

OP

. . .
Is there a flaw to the code, I have been trying to find something wrong with it for a while.
If there is a much easier way to do this, how so?

It is difficult to answer because it is not clear what you are trying to achieve. The code in the OP is incomplete and will certainly not compile. The setup() function appears to imply two shift registers and maybe some limit switches but appears unrelated to the remaining code which looks like it could be intended to go in the loop().

One thing is clear and has already been pointed out to you. There is no such thing in reality as an infinite array and from your code it appears you may mean an array with variable dimensions.

What is this project ?

What is infinity / infinity?

What is infinity - infinity?

OMG! What is infinity + infinity?

This makes my brain hurt.

krupski:
What is infinity / infinity?

What is infinity - infinity?

OMG! What is infinity + infinity?

This makes my brain hurt.

1
0
2 infinities

There...hope you're feeling better!

And why is the infinity of the real numbers > the infinity of the integers?

Calling Dr. Cantor! Calling Dr. Cantor!

The OP means sparse, not infinite.

"And why is the infinity of the real numbers > the infinity of the integers?"

Because they have data to the right of the decimal point :wink:

I believe the OP has boldly gone where no man has gone before.

.

larryd:
I believe the OP has boldly gone where no man has gone before.

.

Perhaps he has split his infinities

Meanwhile, the OP appears to have gone on vacation without responding once to his own thread....

UKHeliBob:
Perhaps he has split his infinities

Respect. 8) Boldly