Arnthor
Arnthor

Reputation: 2623

Large out-of-range numbers appear in C++ rand()

Need to have an array full of random numbers within range. Code:

void fillArray(int *arr){
        for(int i=0;i<(sizeof arr);i++){
            arr[i] = rand() % 2 - 2;
            }
        }

int *arrPoint(int *arr, int max){
    for (int i=0;i<max;i++){
        printf("%d is %d\n",i,arr[i]);
    }
    return arr;
}

int main(int argc, char *argv[]){
    srand ( time(NULL) );
    int arr_f[15];
    fillArray(arr_s);
    arrPoint(arr_s, 15);
    system("PAUSE");
    return EXIT_SUCCESS;
}    

Output:

    0 is -2
1 is -1
2 is -2
3 is -1
4 is 0
5 is 0
6 is 4633240
7 is 2686652
8 is 1973724226
9 is 1974338216
10 is 2686716
11 is 1973744850
12 is 8
13 is 1973752206
14 is 1973752162
Press any key to continue . . .

What the hell? Putting rand() % 2 into brackets doesn't help either. What these numbers are and how do I get rid of them?

P.S. Tried this in crappy Dev-C++ and Code::Blocks with the same result. Need the program to be small sized (putting it to dropbox), so no, I can't use 100mb boost lib.

Upvotes: 0

Views: 1139

Answers (5)

Roland Illig
Roland Illig

Reputation: 41686

Your problem is the sizeof operator. arr is a pointer to an int, which is usually 4 or 8 bytes long. You must pass the length of the array (not the size in bytes) as an additional parameter to the function.

Upvotes: 0

therealszaka
therealszaka

Reputation: 453

Try using _countof(arr) instead of sizeof(arr)._countof returns size of array, not pointers-be aware of this.

Upvotes: -1

Kerrek SB
Kerrek SB

Reputation: 477434

Arrays don't work like you think. You cannot use sizeof to get the array size magically. Arrays decay to pointers to their first element when passed as a function argument, and you must provide the size information separately:

int main()
{
  int * arr = new int[20];
  the_function(arr, 20);
  //...
  delete[] arr;
}

void (int * arr, std::size_t size)
{
  for (std::size_t i = 0; i != size; ++i)
  {
     // something with arr[i]
  }
}

Much better yet, use std::vector<int> to spare yourself all this headache, and tons of further headache that you may not even have thought about.

(There is an exception to my statement: For an automatic array in the local scope, int b[10], sizeof(b) will indeed return 10 * sizeof(int). But that is only because the compiler knows the array size. The size information is not passed around, and it is not available at runtime.)

Upvotes: 3

David
David

Reputation: 28188

In fillArray (sizeof arr) is the size of a pointer, not the size of your array.

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182827

void fillArray(int *arr){
    for(int i=0;i<(sizeof arr);i++){

Since arr is a pointer to an integer, sizeof arr is equivalent to sizeof (int *), which is apparently 4 (32-bits) on your platform. That's clearly not what you want. You only pass fillArray a pointer to the first element of the array.

If you need the number of elements in the array a pointer points to, you need to pass that information. The C and C++ languages provides no way to tell how many bytes a pointer points to given just the pointer.

You do it correctly in arrPoint. Do it that way in fillArray.

Upvotes: 8

Related Questions