Thread: function to copy an array->something going wrong with sizeof()

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    function to copy an array->something going wrong with sizeof()

    Whilest implementing merge sort, i had to create a function that copies an array into another one. Something went wrong, though. At first my code looked like this:

    Code:
    void printArray(int *a)
    {
        cout << "sizeof(testArray) in f-call: " << sizeof(a) << endl;
        int lengte = sizeof(a) / 4;
        cout << lengte;
        for (int i = 0; i < lengte; ++i)
            cout << a[i] << " ";
        cout << endl;
    }
    
    int main(int argc, char *argv[])
    {
        int testArray[4] = {2, 1, 2, 3};
        cout << "sizeof(testArray) before f-call: " << sizeof(testArray) << endl;
        printArray(testArray);
        //mergeSort(testArray);
        return 0;
    }
    I noticed that the second cout printed 4. I figured this could be because it was acctually a pointer, so I changed the paramater to int a[], but still the same result. So I figure int a[] is still a pointer (obv, value parameter).

    But now my question is, how can I get the correct sizeof() result?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You cannot use sizeof in a function to determine the size of an array. You must pass the number of elements in the array. To the function, your array is nothing but a pointer.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can only get the correct sizeof result if you pass the array by reference.

    Code:
    void printArray(int (&a)[4])
    {
        cout << "sizeof(testArray) in f-call: " << sizeof(a) << endl;
        int lengte = sizeof(a) / sizeof(int);
        cout << lengte;
        for (int i = 0; i < lengte; ++i)
            cout << a[i] << " ";
        cout << endl;
    }
    Of course, this makes using sizeof rather pointless, because this function can only accept arrays of 4 integers and nothing else.

    You could make that into a template:

    Code:
    template <std::size_t N>
    void printArray(int (&a)[N])
    {
        cout << "sizeof(testArray) in f-call: " << sizeof(a) << endl;
        int lengte = sizeof(a) / sizeof(int);
        cout << lengte << '\n';
        for (int i = 0; i < lengte; ++i)
            cout << a[i] << " ";
        cout << endl;
    }
    which would still make sizeof pointless, because now you'd already know how many items there are.

    Or you could use a real array class (to be added to the C++ standard library).

    Code:
    #include <iostream>
    #include <boost/array.hpp>
    using namespace std;
    
    template <class Container>
    void printArray(const Container& c)
    {
        for (int i = 0; i < c.size(); ++i)
            cout << c[i] << " ";
        cout << endl;
    }
    
    int main(int argc, char *argv[])
    {
        boost::array<int, 4> testArray = {2, 1, 2, 3};
        printArray(testArray);
        //mergeSort(testArray);
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::array is available in C++0x, though. Just use the newest version of Visual Studio Express or GCC.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    std::array is available in C++0x, though.
    boxden, in case you did not know, C++0x is a name for the next version of the C++ standard, so Elysia is merely confirming what anon mentioned that this "real array class" (template) is "to be added to the C++ standard library". As of now, you may be able to use std::tr1::array, which should be the same as the would-be std::array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM