Help with pointers/arrays.

This is a discussion on Help with pointers/arrays. within the C++ Programming forums, part of the General Programming Boards category; There's a particular function in this program that I'm having a hard time understanding. The confusing part has to do ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    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:

    Code:
    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 
    
    array?
        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?
                }
                break;
    
            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;
    
                break;
    
            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
    
                    myTapeSize+= myTapeChunk; 
    
                    // 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?  
        }
    }
    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.

    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.
    Last edited by Phil-; 09-11-2009 at 08:35 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. Fine.
    2. More better: creates a new array of size 3 called myOperator.
    3. Ditto.
    4, 5, 6. Fine.
    7. Index just walks through the myOperator and myOperand arrays.
    8, 9. Fine.
    11. This creates a new array, called ExpandedOperator, with more entries in it.
    12, 13, 14, 15, 16. Fine.
    17. myNumberofEntries is the size of the (smaller) arrays, whatever it happens to be (not always three). (Actually it's the number of entries that have been entered, but if we're in this section of the code it's also == the size of the array.)
    18. ++ has a higher precedence. However, it's postfix, meaning it returns the original value and therefore we use the original value for the * operator.
    19. Since we were using "dynamic" arrays, what we've done is deleted the short (too short, in this case) arrays. We need to move the arrays over to the old name, otherwise we would have to, in the middle of the code while the program is running, change all the names from myOperator to ExpandedOperator -- and then the next time use ExpandedExpandedOperator, and then ExpandedExpandedExpandedOperator, and so on. The point is that the current array is always known by that name, myOperator.
    20. Why in the name of anything would the code decide to "run" a statement that isn't there? If it isn't there, it doesn't happen.
    "21". Of course it runs again. Why wouldn't it run again? It will run every time the arrays are full. When the array has size 3, it will run when there are three elements. When the array has size 6, it will run when there are six elements. When the array has size 12, it will run when there are twelve elements.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    1. Fine.
    2. More better: creates a new array of size 3 called myOperator.
    3. Ditto.
    4, 5, 6. Fine.
    7. Index just walks through the myOperator and myOperand arrays.
    8, 9. Fine.
    11. This creates a new array, called ExpandedOperator, with more entries in it.
    12, 13, 14, 15, 16. Fine.
    17. myNumberofEntries is the size of the (smaller) arrays, whatever it happens to be (not always three). (Actually it's the number of entries that have been entered, but if we're in this section of the code it's also == the size of the array.)
    Gotcha. Regarding 17, I should have mentioned that I was referring to the three I'd entered but yes, I understand now.

    Quote Originally Posted by tabstop View Post
    18. ++ has a higher precedence. However, it's postfix, meaning it returns the original value and therefore we use the original value for the * operator.
    Ah okay. Must look over those precedences again - I completely understand what you're saying though; great explaination by the way.

    Quote Originally Posted by tabstop View Post
    19. Since we were using "dynamic" arrays, what we've done is deleted the short (too short, in this case) arrays. We need to move the arrays over to the old name, otherwise we would have to, in the middle of the code while the program is running, change all the names from myOperator to ExpandedOperator -- and then the next time use ExpandedExpandedOperator, and then ExpandedExpandedExpandedOperator, and so on. The point is that the current array is always known by that name, myOperator.
    Thank You! This was the main part I was confused by. I was thinking myOperator was being copied over to ExpandedOperator but I keep forgetting = sets both sides, so it goes back to being called myOperator. I hope I said that right but I'm confident I get it now (feel free to correct me though if I'm wrong).

    Quote Originally Posted by tabstop View Post
    20. Why in the name of anything would the code decide to "run" a statement that isn't there? If it isn't there, it doesn't happen.
    Figured. Stupid question on my part.

    Quote Originally Posted by tabstop View Post
    "21". Of course it runs again. Why wouldn't it run again? It will run every time the arrays are full. When the array has size 3, it will run when there are three elements. When the array has size 6, it will run when there are six elements. When the array has size 12, it will run when there are twelve elements.
    Completely understand now. Thank you very much, tabstop. I owe you big time

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't know what you mean by "= sets both sides". = sets the left side equal to the thing on the right; so in myOperator = ExpandedOperator, the thing on the left now is the same as the thing on the right.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    I don't know what you mean by "= sets both sides". = sets the left side equal to the thing on the right; so in myOperator = ExpandedOperator, the thing on the left now is the same as the thing on the right.
    Right, yes that's what I meant. Meaing now both sides are equal or the same which is why the current array is always know by myOperator. At least that's what I took away from your explanation to 19...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers/arrays
    By ikkin in forum C Programming
    Replies: 5
    Last Post: 12-09-2003, 07:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21