0

I have written a simple function which reads in a pointer to an array. It looks at the elements of the array and compares adjacent values for equality. If they are equal it adds their value to "sum"

I am getting a runtime error telling me "local "sum" was referenced before being initialized." I don't understand this because sum is definitely initialized in my function. See code snipet.

int arrRead (char *Pinput){

    int sum, a, b = 0;

    while (*Pinput){
        a = *Pinput;
        ++Pinput;
        b = *Pinput;
        if(a == b)
            sum += a;
    }
    return sum;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • `sum` is not initialized before you start adding to it. Initialization is when you assign it an initial value. You are not giving it an initial value, so it starts with garbage. – Christian Gibbons Feb 21 '19 at 14:54
  • You don’t set `sum` to 0 before you use it. You would need `int sum = 0, a = 0, b = 0;` to zero initialize all three variables. – Jonathan Leffler Feb 21 '19 at 14:54
  • 2
    Where is `sum` *definitely initialized* in that function? – melpomene Feb 21 '19 at 14:54
  • Where is `sum` *definitely initialized* in that function? – pmg Feb 21 '19 at 14:56
  • duplicate of https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value ? – jhnc Feb 21 '19 at 14:58
  • 1
    Your compiler complains about a variable being used without initialization. This is not a runtime error. This is a compile time error/warning. – Gerhardh Feb 21 '19 at 15:15

3 Answers3

6
int sum, a, b = 0;

here only b is initialized with 0.

sum and a are uninitialized.

You need to write:

int sum = 0, a = 0, b = 0;

Or, each variable on its own line:

int sum = 0;
int a = 0;
int b = 0;

Even if in your function technically only sum needs to be initialized for the algorithm to work, it is a very good practice to initialize all the variables upon declaration.


Another good suggestion by @melpomene is to narrow the scope of the variables. For instance a and b are used only in the while loop:

int sum = 0;

while (*Pinput){
    int a = *Pinput;
    ++Pinput;
    int b = *Pinput;
    if(a == b)
        sum += a;
}
return sum;
bolov
  • 72,283
  • 15
  • 145
  • 224
  • Better yet: `int sum = 0, a, b;` Best: `int sum = 0; while (*Pinput) { int a = *Pinput++; int b = *Pinput;` – melpomene Feb 21 '19 at 14:58
  • 1
    @melpomene I wouldn't consider anything that uses the comma operator unnecessarily to be in the "better yet" category, but I guess that comes down to personal preference. – Christian Gibbons Feb 21 '19 at 15:01
  • @ChristianGibbons That's not the comma operator. – melpomene Feb 21 '19 at 15:05
  • @melpomene Perhaps I misused the terminology. What would the proper term be for the use of the comma in that instance? – Christian Gibbons Feb 21 '19 at 15:09
  • 1
    @ChristianGibbons Good question. The C standard doesn't mention a name; the grammar simply specifies that a declarator list consists of one or more declarators separated by `,`. – melpomene Feb 21 '19 at 15:15
  • @Melpomene thanks for suggestion. But wouldn't limiting the scope of a and b in the manner you suggest, result in them being declared anew for each loop iteration? – TestEngineer11 Feb 21 '19 at 15:21
  • @TestEngineer11 Not really. "Declaring" is a compile-time thing. It does nothing at runtime. – melpomene Feb 21 '19 at 15:23
2

You’re not initializing sum with

int sum, a, b = 0;

Only b is being initialized. You need to add an initializer for sum as well.

int sum=0, a=0, b = 0;
dbush
  • 205,898
  • 23
  • 218
  • 273
0

The operator += adds the right side value to the left side value, and then stores the sum back into the left side value.

If sum is uninitialized, the operation sum += X invokes undefined behavior; the result depends on the previous value of sum.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85