Rudra
Rudra

Reputation: 53

Extra number while looping through an array in C++

I am trying to loop through an array of integers using pointers using the following code:

#include <iostream>

int main (int argc, char ** argv)
{
    int ar[] = {1,1,2,3,5,8,13,21,34,55};
    char s[] = "string";

    std::cout << "Print fibonacci until ten using pointers" << std::endl;
    for (int * p = ar; *p; p++)
    {
        std::cout << *p << std::endl;
    }

    //  for (char * cp = s; *cp; cp++)
    //      std::cout << "char is " << *cp << std::endl;

    return 0;
}

On running this code, I get all 10 elements plus a number, 4196368. But on uncommenting the second for-loop and running it again, the numbers vanishes.

Can someone explain why this happens? If needed, the code is compiled in a 64-bit Linux box.

Upvotes: 0

Views: 367

Answers (4)

CinCout
CinCout

Reputation: 9619

You are invoking an undefined behavior.

The first for loop's termination condition is *p. So it is trying to access memory past what actually is owned by ar. Your loop then runs until it finds a memory location that contains 0 (read false) before terminating. In your case, it ran just one extra time (lucky you!). At my end, it ran four more times before terminating.

You must loop only as many times as the size of the array, which is sizeof(ar)/sizeof(ar[0])

Upvotes: 2

AchmadJP
AchmadJP

Reputation: 903

Well, actually this will result in a different outcome on a different machine or a different condition. The one that causes this is your for statement

for (int * p = ar; *p; p++)
{
    std::cout << *p << std::endl;
}

Here, you used *p as a conditional for your for loop to keep running. As we know, C++ treat number > 0 as a true and 0 as a false. While in the for statement, your program checks the next memory address if the value in that address is zero or not (True or False). And as you know, the value of the next address in this particular case on your particular PC is 4196368. So the for loop keeps going until the value of the next address is zero. You can see this with printing the address.

for (int * p = ar; *p; p++)
{
    std::cout << *p << " " << p << std::endl;
}

You will know here that your code check the next address to see its value an if it is indeed not zero, it will continue the loop.

Upvotes: 0

AnatolyS
AnatolyS

Reputation: 4319

Ensure that you have terminated zero:

int ar[] = {1,1,2,3,5,8,13,21,34,55, 0};

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

You're lucky the loop stopped at all; you could have blown up your entire neighbourhood!

Your loop expects to find a "zero" to terminate the array iteration, but your array doesn't have one. Thus, your loop will just keep incrementing past the end of the array until god knows what. The practical results depend on too many practical factors to be either predictable or usefully explained.

I presume that this is an exercise, because using "null-termination" to iterate over an int array is mighty peculiar. In reality you'd just write:

for (auto x : ar)
    std::cout << x << '\n';
}

Upvotes: 3

Related Questions