-2

I have a simple example that sums over an array like:

static uint64_t ySumSimple(uint32_t* arr, uint32_t* to) {
    uint64_t sum = 0;

    while (arr < to) {
        sum = sum + *arr;
        ++arr;
    }

    return sum;
}

and use it in main method like :

using namespace std;
int main(){

    size_t size = 1000*1000;// len*loops;

    uint32_t * arr= (uint32_t *) malloc(size * sizeof(uint32_t));// random values here          

    auto sum = ySumSimple(arr,arr+size);

    cout <<" sum " <<  sum << endl;
}

This returns real sum (some big digit) if size = 1000*100 but with one million uints it returns 0. How can that be if the sum over one million uints can not overflow ulong, right?

Vishaal Shankar
  • 1,648
  • 14
  • 26
user184868
  • 193
  • 9
  • 2
    Possible duplicate of [Confused with accessing malloc memory (Uninitialized)](https://stackoverflow.com/questions/17687182/confused-with-accessing-malloc-memory-uninitialized) – phuclv Feb 14 '18 at 04:21
  • 1
    [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/q/11962457/995714) – phuclv Feb 14 '18 at 04:23
  • 1
    There is no such thing as "C/C++". –  Feb 14 '18 at 04:29
  • Navel-gazing about undefined behavior aside, the practical explanation is likely to do with how various-sized memory blocks are allocated. Many C++ runtime implementations keep a pool of smaller memory chunks around in-process for efficient re-use, but when you malloc() a truly huge amount of memory, they fall back to allocating memory directly from the OS's free store instead. To avoid security issues (e.g. unprivileged app A reading privileged app B's secret-but-free()'d data), memory from the OS's free store is usually zero'd out before being given to the app. – Jeremy Friesner Feb 14 '18 at 05:16

2 Answers2

0

Since you program in C++ (please remove the C language tag) you have undefined behavior.

You have UB because while the contents of the memory you allocate might seem random, its actual contents is indeterminate. And using (even just reading) indeterminate data leads to UB.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • i guess, there must be just random values, what else can lead to it – user184868 Feb 14 '18 at 04:19
  • @user184868 It doesn't matter if it is "random". It's uninitialized and therefore indeterminate and therefore you have undefined behavior. And if you have undefined behavior there's really no point in arguing about behavior. – Some programmer dude Feb 14 '18 at 04:23
  • undefined behavior doesn't mean random values will be read. You may get 0s if the allocated memory is a new page from the OS. There are architectures that don't even allow you to access uninitialized memory – phuclv Feb 14 '18 at 04:23
  • ahh good point about the OS page, seems values over 100k are in the new ? How got no error while setting some values there, thanks – user184868 Feb 14 '18 at 04:27
0

All these following values hold zero (0) each. Hence their sum will be Zero !

*arr is zero *(arr+1) is zero ... *(arr+size) is zero

Summing above all will give zero. You need to assign values in the array arr.

Find the corrected code below (containing assignment of array element values) and the sum comes as 499999500000.

#include <iostream>
using namespace std;

static uint64_t ySumSimple(uint32_t* arr, uint32_t* to) {
    uint64_t sum = 0;

    while (arr < to) {
        sum = sum + *arr;
        ++arr;
    }

    return sum;
}


int main() {
    size_t size = 1000*1000;// len*loops;

    uint32_t * arr= (uint32_t *) malloc(size * sizeof(uint32_t));// random values here          

    //Assign Values into the array elements
    for(int i=0; i< size; i++)
        *(arr+i) = i;

    auto sum = ySumSimple(arr,arr+size);


    cout <<" sum " <<  sum << endl;
    return 0;
}
Syed Waris
  • 1,056
  • 1
  • 11
  • 16