Sign in to follow this  
forth

[C++] array initalization not working as it supposed to?

Recommended Posts

Can someone please compile the following code and paste me your output? i'm getting weird results with my array initialization. shouldn't "int solution[5] = { -1 };" fill the whole 'solution' array with -1's?
#include <iostream>

using namespace std;

int solution[5] = { -1 };
int s = 0;

bool visited[5] = { false };

void dfs( bool *m, int root )
{
    if( visited[root] == false )
    {
        solution[s++] = root;
        visited[root] = true;

        for( int i = 0; i < 5; ++i )
        {
            if( *(m + (root*5) + i) == true )
            {
                dfs( m, i );
            }

        }
    }
}

int main()
{

    bool m[5][5] =
    {
        0,0,0,1,0,
        0,0,0,1,0,
        0,1,0,0,0,
        0,0,0,0,1,
        0,1,0,0,0
    };

    for( int i = 0; i < 5; ++i )
    	cout << solution[i] << " ";
    cout << endl;

    for( int i = 0; i < 5; ++i )
    	cout << visited[i] << " ";
    cout << endl;

    dfs( &m[0][0], 0 );

    int i = 0;
    while( solution[i] != -1 && i < 5 )
    {
    	cout << solution[i] << " ";

    	++i;
    }

	return 0;
}

my output: -1 0 0 0 0 0 0 0 0 0 0 3 4 1 0

Share this post


Link to post
Share on other sites
Quote:
Original post by forth
shouldn't "int solution[5] = { -1 };" fill the whole 'solution' array with -1's?

No. Every element you don't specify will be initialized to its default value, in this case 0.

Share this post


Link to post
Share on other sites
i see. thank you!

i guess, this man should be condemned, then:
http://www.fredosaurus.com/notes-cpp/arrayptr/array-initialization.html

Share this post


Link to post
Share on other sites
Quote:
Original post by forth
i see. thank you!

i guess, this man should be condemned, then:
http://www.fredosaurus.com/notes-cpp/arrayptr/array-initialization.html

Why?
Quote:
Missing initialization values use zero

If an explicit array size is specified, but an shorter initiliazation list is specified, the unspecified elements are set to zero.

Share this post


Link to post
Share on other sites
STL algorithms to the rescue!


#include <algorithm>

// Elsewhere!

int arr[5];

std::fill_n( &arr[0], 5, -1 );
/* or */ std::fill( &arr[0], &arr[5], -1 );
/* or */ std::fill_n( &arr[0], sizeof(arr)/sizeof(int), -1 );
/* or */ std::fill( &arr[0], &arr[0] + sizeof(arr)/sizeof(int), -1 );


Share this post


Link to post
Share on other sites
Unfortunately, you can't arbitrarily execute code to perform initializations like that. In order to execute fill() or fill_n() the function calls need to be inside a function of some sort, and initialization will then become subject to potential order dependencies. In C++ there is no good substitute to the actual initializers for arrays.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this