Being argument, array a[2] isn't copied when calling function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Being argument, array a[2] isn't copied when calling function

  1. #1
    Join Date
    Feb 2013
    Posts
    2

    Being argument, array a[2] isn't copied when calling function

    Hi,
    I cannot understand why the following happens, please read to the end.
    The code below outputs this:
    a[]= 00
    a[]= 10
    a[]= 10
    a[]= 10
    a[]= 11
    a[]= 11
    0.

    But I was expecting this:
    a[]= 00
    a[]= 10
    a[]= 10
    a[]= 00
    a[]= 01
    0.

    This describes how the process is running in machine:
    1. Defining a[2]{0,0}; ii=0; aj=0
    2. Calling function func(a,ii,aj) |func({0,0},0,0)|
    3. func({0,0},0,0) defining w=0; static aa=0
    4. func({0,0},0,0) if(0) returns aa=1
    5. func({0,0},0,0) for j=0
    6. func({0,0},0,0) for Outputing "00", because a[2]={0,0}, look (1).
    7. func({0,0},0,0) for if(!0) | because a[0]=0| returns w+=func(a,ii+1,j) |func({0,0},0+1,0)| and calls func({0,0},1,0)
    8. func({0,0},0,0) for if func({0,0},1,0) defining w=0
    9. func({0,0},0,0) for if func({1,0},1,0) if(1) returns a[0]=1, because of static aa=1, см 4.
    10. func({0,0},0,0) for if func({1,0},1,0) for j=0
    11. func({0,0},0,0) for if func({1,0},1,0) for Outputing "10", because of a[2]={1,0}, look row #9
    12. func({0,0},0,0) for if func({1,0},1,0) for if(!1) |because a[0]=1|
    13. func({0,0},0,0) for if func({1,0},1,0) for j=1
    14. func({0,0},0,0) for if func({1,0},1,0) for Outputing "10"
    15. func({0,0},0,0) for if func({1,0},1,0) for if(!0) |because a[1]=0|
    16. func({0,0},0,0) for if func({1,0},1,0) for if if(1==1) |because ii=1, func({0,0},ii,0)|
    17. func({0,0},0,0) for if func({1,0},1,0) for if if return 0
    18. func({0,0},0,0) for if w=0 |because func({1,0},1,0) gives 0|
    19. func({0,0},0,0) for j=1

    And from now, something is happening that I cannot understand:
    20. func({0,0},0,0) for Outputing "10"
    Why so? If func has itselfs local variables, including a[2]={0,0}.
    I was expecting this:
    20. func({0,0},0,0) for Outputing "00"

    So a[2] array is not local variable. Why it happens?

    Code:
    #include <iostream>
     
    using namespace std;
     
    int func(bool a[],int ii,int aj)
    {
        int w=0;
        static bool aa=0;
        if(aa){a[aj]=1;}else{aa=1;}
            for(int j=0;j<2;j++)
            { 
                cout<<"a[]= "<<a[0]<<a[1]<<endl;
                if(!a[j])
                {
                    if(ii==1){return 0;}
                    else{w+=func(a,ii+1,j);}
                }
            }
        return w;
    }
     
    int func()
    {   
        bool a[2];
        for(int i=0;i<2;i++)a[i]=0;
        int ii=0,aj=0;
        return func(a,ii,aj);
    }
     
    void main()
    {
        cout<<func();
        getchar();getchar();
    }
    But if I define array a[2] as a vector, all goes fine.
    Code:
    #include <iostream>
    #include <vector>
     
    using namespace std;
     
    int func(vector<bool> a,int ii,int aj)
    {
        int w=0;
        static bool aa=0;
        if(aa){a[aj]=1;}else{aa=1;}
            for(int j=0;j<2;j++)
            { 
                cout<<"a[]= "<<a[0]<<a[1]<<endl;
                if(!a[j])
                {
                    if(ii==1){return 0;}
                    else{w+=func(a,ii+1,j);}
                }
            }
        return w;
    }
     
    int func()
    {   
        vector<bool> a(2);
        for(int i=0;i<2;i++)a[i]=0;
        int ii=0,aj=0;
        return func(a,ii,aj);
    }
     
    void main()
    {
        cout<<func();
        getchar();getchar();
    }

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

    Re: Being argument, array a[2] isn't copied when calling function

    Quote Originally Posted by Qeeet View Post
    So a[2] array is not local variable. Why it happens?
    When you pass an array to a function, it decays to a pointer to the first element. In other words
    Code:
    int func(bool a[],int ii,int aj);
    // is essentially the same as
    int func(bool* a,int ii,int aj);
    If you want to pass an array to a function by value, you can wrap it in a struct and pass the struct or use std::array (std::tr1::array in older compilers). std::vector can also be a good alternative.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Re: Being argument, array a[2] isn't copied when calling function

    Thanks!! Now I've understood it

  4. #4
    Join Date
    Oct 2008
    Posts
    1,146

    Re: Being argument, array a[2] isn't copied when calling function

    just to be picky,

    Quote Originally Posted by D_Drmmr View Post
    When you pass an array to a function, it decays to a pointer to the first element. In other words
    Code:
    int func(bool a[],int ii,int aj);
    // is essentially the same as
    int func(bool* a,int ii,int aj);
    If you want to pass an array to a function by value, you can wrap it in a struct and pass the struct or use std::array (std::tr1::array in older compilers). std::vector can also be a good alternative.
    actually, they are exactly the same; function parameters of array type ( with or without bounds ) always decay to a pointer type when the function type is formed. So, the types T*,T[],T[N] are different but the types void(*)(T*),void(*)(T[]),void(*)(T[N]) are indeed exactly the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center