Help with pointers/arrays.
There's a particular function in this program that I'm having a hard time understanding. The confusing part has to do with pointers pointing to arrays.
I have some questions but mostly I want to know if my thinking (i.e. understanding of what's happening) is right or not.
Here's the code then my questions and thoughts on what I think is happening. I've included my thoughts and questions in bold as comments in the code - note there are also regular comments that aren't bolded, these are from my book instructions that I copied for reference for myself when I look at the code:
I've numbered my comments in case anyone just wants to address a particular one. I've racked my head over this now for two days now so any help or clarification I can get would be much appreciated.
void Tape(const char theOperator,const int theOperand)
static const int myTapeChunk = 3; // 1.Declares myTapeChunk as a static const int and sets its value to three.
static char *myOperator = new char[myTapeChunk]; // 2.Declares myOperator as a pointer to char array. Basically it sets myoperator = to element 0 of new myTapeChunk char
static int *myOperand = new int[myTapeChunk]; // 3.Same here except with operand.
static int myTapeSize = myTapeChunk; 4.Declares myTapeSize = to myTapeChunk or in this case 3.
static int myNumberOfEntries = 0; 5.creates static int and sets it to 0
switch (theOperator) // 6.the switch starts. Cases being the operators ?, . and default
case '?': // Displays the tape
for (int Index = 0; Index < myNumberOfEntries; Index++)
cout << myOperator[Index] << "," << myOperand[Index] << endl; // 7.Not so sure about this. I'm thinking the pointer myOperator is holding the address of the
first element of index?
case '.': // The program is stopping, delete arrays // 8.sounds simple enough. Entering '.' deletes and frees back up the space taken by myoperator and operand?
delete  myOperator;
delete  myOperand;
default: // Add to the tape and expand if needed // 9.Default kicks in, which is basically anytime the case isn't '?' or '.'
if (myNumberOfEntries == myTapeSize) // Expand //10. See last comment on confusion about this
// Create a destination for the expansion
char *ExpandedOperator = new char[myNumberOfEntries + myTapeChunk]; //11.This creates the new pointer named ExpandedOperator and it's equal to (or holds) the
first address (i.e. where the first element is) or the new char array which now has 6 element slots?
int *ExpandedOperand = new int[myNumberOfEntries + myTapeChunk]; // 12. same here except with the Operand
// We use pointers to do the array copy,
// starting at the array start position.
char *FromOperator = myOperator; // 13.pointer FromOperator is created and now holds the address of the first element of myOperator?
int *FromOperand = myOperand; //14. Same as above except with operand
char *ToOperator = ExpandedOperator; //15. Pointer ToOperator is created and now holds the address of the first element of ExpandedOperator?
int *ToOperand = ExpandedOperand; //16. Same as above except with operand
// Copy the old arrays to the new - this is supposed to be faster
// than copying indexes but it is dangerous
for (int Index = 0; Index < myNumberOfEntries; Index++) //17.myNumberofEntries is equal to three (or four? see below) so the loop will go 0,1,2 or the three
numbers that I've entered in their elements?
*ToOperator++ = *FromOperator++; //18.The way I understand this is since * has a higher precedence, ++ doesn't happen until each of these are copied. So it's
taking what's in the first element of myOperator (since fromoperator points to it's first element) and copying it to the first element pointed to by ToOperator (which is
ExpandedOperator)? Then they both move on to copy the next element (the ++ part). Same below
*ToOperand++ = *FromOperand++;
// Delete old arrays
delete  myOperator;
delete  myOperand;
// Replace the old pointers with the new
myOperator = ExpandedOperator; // 19.This is something I'm confused by. Didn't I just delete myOperator? So how can we now set it to ExpandedOperator if it's
just been deleted? Also, why do I even need to do that? Didn't I take care of that with all the above transfers that I just did?
myOperand = ExpandedOperand;
// Record how big the array is now
// Now it's safe to add a new entry
myOperator[myNumberOfEntries] = theOperator;
myOperand[myNumberOfEntries] = theOperand;
myNumberOfEntries++; // 20.I'm a bit confused about this being down here. If I understand correctly, this runs if the case is default. But lets say I've
already entered two things (two operators and two operands each being held in there 0 and 1 element slot) and I've just entered a third. Does the program wait until the next
time around to run the if statement right below default (since 3 now == to 3) or does it run it as soon as it gets down here and I've entered a third number and operator?
Thanks in advance.
EDIT: I forgot one question. Say the default switch statement runs and hence the if statement runs (i.e. 3 ==3), what happens the next time it has to run again? After it runs for the first time myTapeSize is increased to 6, and if myNumberOfEntries get to six, does it run again? Basically, how can it even run again? In my comment number 13, FromOperator is being set to myOperator, which I erased... Also, regarding the code next to comment 19. - myOperator=ExpandedOperator - didn't I already do that? Shouldn't it need a new pointer to be created and set to?
It obviously doesn't though because the program runs fine past six, so I guess I'm just not getting it.