1

This is my code and it works as expected. But after adding a rising integer (even though it has no influence on the code) my code doesn't work as expected


#include <iostream>

int i;
int j = 0;
int nums[] = {};
int co = 0;

void rq1() //rq = request
{
    std::cout << ("How many numbers?");
    std::cin >> i;
}

void rq2 () 
{
    int n2 = 0;
    for (int n = 1; n <=  i; n++) 
    { 
        std::cout << n2 + 1 << (". number?"); 
        std::cin >> nums[n2]; 
        n2++;
    }
}

void sort ()
{
    for (int n4 = 0; n4 < i; n4++)
    {        
        int k = j + 1;

        for (int n3 = 1; n3 < i; n3++) 
        {
            if (nums[j]==nums[k]) 
            {
                if (j<k) {std::cout << j << "," << k << std::endl; k++;}
                else {k++; return;}
            }
            else {k++;}
        }

        j++;        
    }
}

int main() 
{
    rq1();
    rq2();
    sort();    
} 

Input: 4 numbers (1,2,1,2) Output: (0 , 2) (1 , 3)

But after adding co++ the Output is (0,2) but expected Output is (0 , 2) (1 , 3)

{
    for (int n4 = 0; n4 < i; n4++ )
    {
        int k = j + 1;

        for (int n3 = 1; n3 < i; n3++) 
        {
            if (nums[j]==nums[k]) 
            {
            
                if (j<k) {std::cout << j << "," << k << std::endl; k++; co++;}
                else {k++; return;}
            }

            else {k++;}
        }

        j++;        
    }
}

I don't know why my outputs changes. I mean int co doesn't do anything else

harper
  • 13,345
  • 8
  • 56
  • 105
Ball
  • 33
  • 4
  • 5
    `int nums[] = {};` has no elements. Accessing any index is undefined behavior. Adding `co++` just made that apparent, but `co++` or not, the output of the program could be anything – 463035818_is_not_an_ai Oct 01 '21 at 12:21
  • I think you want `std::vector nums;` instead – 463035818_is_not_an_ai Oct 01 '21 at 12:22
  • ```int nums[]``` hast no elements because the user is supposed to set it. But I dont get why ```co++``` has influence on my code. It only appears in this line of code and it only stacks up. I mean ```int nums``` isn't the reason why it doesn't work. It only crashes after adding ```c++``` – Ball Oct 01 '21 at 12:24
  • @Ball that's not how arrays work, that array is empty and trying to access it in any way will most likely crash your program. If you want to have an array "where user can add elements" use `std::vector` and `push_back`. – Kaldrr Oct 01 '21 at 12:26
  • "ìnt nums[]` hast no elements because the user is supposed to set it" that's not how C++ works. "But I dont get why co++ has influence on my code". You program has Undefined Behavior i.e. can behave in absolutely any way. – bolov Oct 01 '21 at 12:27
  • Even thought arrays don't work like that, my code works as long I don't add ```co++```. I only want to know why it crash when I'm adding ```co++``` – Ball Oct 01 '21 at 12:28
  • `int nums[]` is the reason why it doesnt work. Even when your code appeared to be fine without `co++` it wasnt. Wrong code can still appear to be ok, then you change something unrelated and suddenly it breaks, it was broken already before – 463035818_is_not_an_ai Oct 01 '21 at 12:28
  • its a logical fallacy. Wrong results are caused by wrong code, but that does not necessarily imply that wrong code must produce wrong results – 463035818_is_not_an_ai Oct 01 '21 at 12:30
  • @Ball Even if a program SEEMS to work it doesn't mean it is correct! That is what undefined behavior means, anything can happen even seemingly correct running of the program. – Pepijn Kramer Oct 01 '21 at 12:30
  • that's the nature of Undefined Behavior. Spooky things can happen. – bolov Oct 01 '21 at 12:30
  • So it actually has no real reason why it breaks at this point? – Ball Oct 01 '21 at 12:31
  • welcome to the wonderful land of undefined behavior, where anything can happen and normal reasoning is futile ;). Seriously, the reason is in implementation details of the compiler you are using and in the generated assembly. It has very little to do with how C++ actually works – 463035818_is_not_an_ai Oct 01 '21 at 12:32
  • By the way what compiler did you use, since clang and msvc both give compiler errors on your code. Even if its just warnings don't ignore those either, see compiler explorer : https://godbolt.org/z/fG1Yze6fr – Pepijn Kramer Oct 01 '21 at 12:33
  • it doesn't break at this point. It was always broken. It just happens that the first version behaves as you expected it to and the second one doesn't. Trying the understand the behavior of on Undefined Behavior program is futile. – bolov Oct 01 '21 at 12:34
  • @Ball By the way we're not being mean or angry. We are trying to get a very important point across. From the moment code hits a point where behavior isn't defined (as stated in the c++ standard: (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf) it is no longer a valid program and nothing is guaranteed anymore. Reading from/writing to memory that's not properly allocated is one of those areas. – Pepijn Kramer Oct 01 '21 at 12:42
  • [Look here](https://godbolt.org/z/PPhh9zooa) then [read this](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). – n. m. could be an AI Oct 01 '21 at 12:48
  • @Ball *So it actually has no real reason why it breaks at this point?* -- The point others are trying to make is that the program is ill-formed -- it isn't even a valid C++ program, since C++ does not allow 0-length arrays. In other words, no program should even exist. – PaulMcKenzie Oct 01 '21 at 12:56
  • @Ball *(even though it has no influence on the code)* -- Also, that is not how a compiler works when it comes to optimizing the code. The code you write is only a description of what you want to occur. The compiler can rearrange your code so that it does what you are describing, but that rearranging of the code can look *nothing* like the original code you are looking at. See the [as-if rule](https://en.cppreference.com/w/cpp/language/as_if) – PaulMcKenzie Oct 01 '21 at 13:02

1 Answers1

3
int nums[] = {};

This array variable has no elements. This isn't allowed in C++. The program is ill-formed.

std::cin >> nums[n2];

Here you access the empty array outside of its bounds. The behaviour of the program is undefined.

I don't know why my outputs changes.

It's because the behaviour of the program is undefined.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • And how do I create a variable array? – Ball Oct 01 '21 at 12:38
  • @Ball The size of all arrays is constant. To implement the abstract data structure "resizable array", you can use dynamic storage (the size of dynamic array may be determined at runtime), and to resize you can allocate a larger array, copy the element from the old one, and free the old array. There's no need to implement it yourself since the standard library comes with one. It's called `std::vector`. – eerorika Oct 01 '21 at 12:43