Thread: dealing with array sizes

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    dealing with array sizes

    I've come across this:

    Code:
     
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Chair
    {
       public:
          vector<int> arrysize(int selectedArray[])
             {
                vector<int> result(2,0);        
                result[0] = sizeof(selectedArray);
                result[1] = sizeof(selectedArray[0]);
                return result;
              }
    };
    
    int main(int argc, char *argv[])
    {
       Chair seat;
       int sA[] = {2,4,7,3,7};
       cout<<"sizeof(sA): "<<sizeof(sA)<<endl;
       cout<<"sizeof(sA[0]): "<<sizeof(sA[0])<<endl;
       cout<<"sizeof(sA) using arrysize(): "<<seat.arrysize(sA)[0]<<endl;
       cout<<"sizeof(sA[0]) using arrysize(): "<<seat.arrysize(sA)[1]<<endl;
       system("PAUSE");   
       return EXIT_SUCCESS;
    }
    Now this pops out the following results:

    sizeof(sA): 20
    sizeof(sA)[0]): 4
    sizeof(sA) using arrysize(): 4
    sizeof(sA[0]) using arrysize(): 4


    My question is why is this giving different results when i get the size of the array through the function as opposed to getting the size of the array within the main loop?
    Last edited by Mooglepuff; 08-15-2005 at 10:05 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I might not do the best job explaining this in terms of a low level aspect, maybe somone could back me up, but when you take the sizeof the array in the main() it actually takes all 20 bytes (4 bytes per int) as the size of the array on the stack. In the main, the array is actually sitting on the stack, it is not a pointer to data elsewhere. When you call the function, all that happens however, is a pointer to the data on the stack in the main is push'ed. If you throw something like:

    Code:
    std::cout << (void*) &selectedArray;
    In your Chair member function, then you will see it spit out an address like: 0012FED4. Which is the address of the stack on XP SP2 for me. I think it's much higher on *nix. Um, yeah, I think that's about it.
    Last edited by Tonto; 08-15-2005 at 10:13 PM. Reason: Stuff

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yes. When arrays are passed to functions, it is actually a pointer to the array which is passed. Either pass the size as a parameter, or, preferably, use std::vector.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Quote Originally Posted by Tonto
    ...you will see it spit out an address like: 0012FED4. Which is the address of the stack on XP SP2 for me. I think it's much higher on *nix. Um, yeah, I think that's about it.
    For instance, on my RH9 system, the address is 0xBFFFF1B4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  2. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  3. Array sizes and arbitrary sized arrays
    By bennyandthejets in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2003, 11:24 PM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM