0

I am writing a program to read in two arrays, the first of length C and the second of length N. Then, it prints the two arrays. However, when my program prints the arrays, it prints strange numbers that never existed in the array. In addition, I have noticed that when I sort the array before printing, the strange numbers disappear when the array is printed.

For example, when C = 10 and N = 10 and both arrays are {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, my program gives this:

First array: 1 2 3 4 5 6 7 8 9 10 4199003 0
First array sorted: 1 2 3 4 5 6 7 8 9 10
Second array: 1 2 3 4 5 6 7 8 9 10 -1159496120 32765 1 2 3 4 5 6 7 8 9 10
Second array sorted: 1 2 3 4 5 6 7 8 9 10

Here is my code:

#include <iostream>
#include <algorithm>

using namespace std;

void println(int arr[]) {
    for (int i = 0; i < (&arr)[1] - arr; ++i) {
        cout << arr[i] << " ";
    }
    cout << std::endl;
}

int main () {
    int c, n;
    cin >> c >> n;
    int t[c], a[n];
    for (int i = 0; i < c; ++i) {
        cin >> t[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    cout << "First array: ";
    println(t);
    sort(t, t+c);
    cout << "First array sorted: ";
    println(t);
    cout << "Second array: ";
    println(a);
    cout << "Second array sorted: ";
    sort(a, a+n);
    println(a);
}

I've tried the program using both clang and gnu compilers and they both give me similar results. Why are these numbers appearing and why does sorting eliminate this problem? Any help is appreciated!

zxcvbnm
  • 41
  • 1
  • 8
  • 5
    Not sure what you wanted to achieve with `i < (&arr)[1] - arr`, but anyway when the arrays are passed to `println` they decay into pointers. You need to pass their length as a separate parameter. Also it is recommended to use `std::vector` instead of a raw C array, which will eliminate the problem altogether. – wohlstad Jun 28 '22 at 03:55
  • 6
    Variable length array is not part of C++ standard. https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard Try using `vector` instead. – Haoliang Jun 28 '22 at 03:56
  • Thanks for your replies! I wanted to use pointer arithmetic to find the length of the array. Also, why is it recommended to use a vector instead of an array and why does array have this problem? – zxcvbnm Jun 28 '22 at 03:58
  • About reasons for using `std::vector` see here: https://stackoverflow.com/questions/10865861/when-to-use-vectors-and-when-to-use-arrays-in-c. – wohlstad Jun 28 '22 at 04:00
  • About array decaying into pointer (and "loosing" it's length), see here: https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay. – wohlstad Jun 28 '22 at 04:01
  • 4
    @zxcvbnm `int t[c], a[n];` -- This is not valid C++ code. That's the main issue. So specifying "arrays" is a moot point, since those arrays aren't real. Second, it seems you are learning C++ from looking at poor websites -- the reason why I say this is that there are *no* C++ books that show declaring arrays using a runtime variable like that. The reason why you won't see any good C++ books showing it is because...it isn't valid. – PaulMcKenzie Jun 28 '22 at 04:05
  • 1
    @zxcvbnm -- *I've tried the program using both clang and gnu compilers* -- In addition, there have been versions of the compiler you're using, where using those "arrays" in STL functions such as `std::sort` caused a lot of compiler errors. I don't remember the exact version of `g++`, but those fake arrays could not be used in `std::sort`, unlike a true, non-fake, array. – PaulMcKenzie Jun 28 '22 at 04:10
  • @PaulMcKenzie I rewrote my code and I think the array initialization does work. Is it not valid because it isn't good programming practice? – zxcvbnm Jun 28 '22 at 04:14
  • 3
    @zxcvbnm -- It isn't good practice because it isn't legal C++. Your code [fails to compile](https://godbolt.org/z/8vqo4ejTj) using the Microsoft C++ compiler. See the error: `(16): error C2131: expression did not evaluate to a constant` – PaulMcKenzie Jun 28 '22 at 04:14
  • `(&arr)[1]` treats `&arr` as an array of pointers then tries to access the second element of that array, as `&arr` only points to a single element your code has undefined behaviour – Alan Birtles Jun 28 '22 at 06:14
  • Does this answer your question? [array length using pointers](https://stackoverflow.com/questions/17945816/array-length-using-pointers), with [When a function has a specific-size array parameter, why is it replaced with a pointer?](https://stackoverflow.com/questions/1328223/) explaining why that question's `int *arr` is equivalent to this question's `int arr[]`. – JaMiT Jun 28 '22 at 06:59
  • Why does sorting the array allow the println function to work? – zxcvbnm Jun 28 '22 at 21:05
  • @wohlstad Why does sorting the array allow the println function to work? – zxcvbnm Jun 28 '22 at 22:52
  • @zxcvbnm *"Why does sorting the array allow the println function to work"* -- You have undefined behavior. Sometimes adding an unrelated function call makes the code seem to "work". Sometimes adding an unused variable has the same effect. Undefined behavior is just weird like that. *In fact, the symptom of sorting eliminating a problem suggests undefined behavior as a prime suspect, even before looking at the code.* – JaMiT Jun 30 '22 at 02:10

0 Answers0