mahmoud
mahmoud

Reputation: 29

what is the value of an array element if I don't assign it myself

what would be the result if I wrote this

int array1[2];
cout << array1[0] ;

and how can I do this pseudocode :

if array1[0] doesn't have a value then assign its value to 1  

I'm using C++ on DevCPP

Upvotes: 2

Views: 8354

Answers (6)

Faraaz Ahmad
Faraaz Ahmad

Reputation: 99

If you don't initialise the element yourself, the element will obtain the value from the memory location it is stored on now (and will most probably convert it to its data type). Consider this program :

#include <iostream>
using namespace std;

int foo(int A[])
{
    cout << A[0] << A[1];
}

int main()
{
    int array[2];
    foo(array);
}

This will give the output 00.

But now consider this code :

int main()
{
    int array[2];
    cout << array[0] << array[1];
}

It will give some random integer output. This is so because the uninitialised array picks up the value stored on the memory location it now occupies. You can check its memory adress by &array[0] and print it in different data types for some thought provoking questions.

eg: cout << &array[0] << char(array[0]) << bool(array[0]) etc.

Upvotes: 0

Alberto Bonsanto
Alberto Bonsanto

Reputation: 18042

As other users said, you need to initialize a then use a for loop to test each value one by one and modify them, if they fulfill a condition, I leave you a C snippet:

/* Variable declaration and initialization to 0s (You can use another value as default )*/
int a[ 5 ] = { 0 };

/* If the array[ i ] has 0 as value */
for( i = 0; i < 5; i++){
   if ( a[ i ] == 0  ){ 
      a[ i ] = 1;
   }
}

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490398

There is one point that the answers I'm seeing thus far seem to have missed. It depends on where your array is defined.

If the array is local to a function, like:

int f() { 
    int array1[2];
    cout << array1[0] ;
}

...then the other answers are correct: the content of array1 contains unspecified values, and your attempt to read the value and send it to cout gives undefined behavior.

On the other hand, you may have defined array1 as a global variable:

int array1[2];

int f() { 
    cout << array1[0];
}

In this case, the content of array1 is required to be initialized to 0 for any arithmetic type (or NULL for an array of pointers). In a case like this, writing out the value in array1[0] is perfectly fine and gives defined results -- it must be 0. In this case, there is no any way to tell whether an element of an array containing the value 0 has that value because it was automatically initialized to 0, or was assigned that value later.

If you really need to know whether a value has been written to a variable, it's possible to write a class that will do that:

template <class T>
class value { 
     T val;
     bool assigned;
public:
     value(T const init=T()) : assigned(false), val(init) {}

     value &operator=(T const &t) { 
         assigned = true;
         val = t;
     }
     operator T() { return val; }

     bool was_assigned() { return assigned; }
};

// ...

value<int> array2[2];

if (!array2[0].was_assigned())
    array2[0] = 1;

It's usually easier and more efficient to just define the type to always start out initialized to a known value, so you never really care about whether it's been assigned to or not though. In short, although this supports what you've asked for, my immediate reaction is that there's probably a better/cleaner way to accomplish your ultimate goal. Before you even consider using something like this, I'd strongly recommend stepping back from what you're doing, and trying to figure out if there's a better way to do it. My guess is that there is/will be (and if you can't find it, you might want to ask another question, telling us about why you're trying to do this, to see if somebody can see a more direct way to accomplish your goal).

Upvotes: 3

Kerrek SB
Kerrek SB

Reputation: 477378

The elements of array are uninitialized, and it is undefined behaviour to read them before writing to them. Your program is ill-formed. There is no way to "check" for this; it is your responsibility to write a correct program.

Upvotes: 8

cdhowie
cdhowie

Reputation: 169183

The initial value of unassigned array values is undefined (unless the array element type is a class/struct, in which case the default constructor will be called for each array element). In your first example, the behavior is undefined since you have not initialized the array element before using it.

If you want to retain an "unassigned" status then you need to use a class that encapsulates this, for example using the nullable pattern for value types.

Consider using Boost.Optional: you'd declare the array as boost::optional<int> array1[2]; and then you can test if (array1[0]) to see if that particular element has a value.

Upvotes: 3

Albert Oclarit
Albert Oclarit

Reputation: 143

As far I remember that depend on the OS

Upvotes: 1

Related Questions