Below I have some code that I'm having trouble with. I make an array called myArray and initialise it with the integers from 1 to 10. I output three things to start with , myArray, &myArray and &myArray[0]. All of them appear to have the same memory address. My first question is: why does this happen?
When I pass either myArray or &myArray[0] to print_array, it will output as expected, when I pass &myArray, it will not compile - if somebody could explain to me why this happens, I'd be very grateful.
Thirdly, in my print_array function, I have hard-coded the size of my array. Is there a way that C++ can figure out how large the array is?
I'm quite willing to believe there is a much neater way of passing an array to a function by reference, if anyone could help me find out what it is, I'd be very happy!
&myArray, it will not compile - if somebody could explain to me why this happens, I'd be very grateful.
&myArray will be an int**, you can't pass that to a function that expects an int*.
Is there a way that C++ can figure out how large the array is?
Not with those array types. You normally do that by passing the size of the array along with the function.
I'm quite willing to believe there is a much neater way of passing an array to a function by reference, if anyone could help me find out what it is, I'd be very happy!
Yes, it's called "Arrays are evil, use std::vectors instead". Your program with vectors:
Thanks for your quick reply! Are there any performance issues by using vectors instead of arrays?
I noticed that in your code your prototype for print_array says that you're going to give it a reference to a vector of ints, but actually, you just give it a vector - would it be possible to explain why this is the case? When I tried to pass it a reference to the vector, it still compiled.
I noticed that in your code your prototype for print_array says that you're going to give it a reference to a vector of ints, but actually, you just give it a vector - would it be possible to explain why this is the case?
Magic. print_array(myVector); You can think of it as "it creates a reference to myVector here and passes it".
lso, if I didn't pass a reference, but the actual vector, would it just pass a reference anyway, like it would with arrays?
Well, not the same as with arrays. In your example, you would simply pass the array pointer - arrays in C++ are implicitly pointers - around, whereas references behave more like as if they are the actual object.
For example:
1 2 3 4 5 6 7 8 9
A object1;
A object2;
A *ptrObject;
A &refObject;
ptrObject = &object1; //ptrObject points to object1
ptrObject = &object2; //ptrObject points to object2
refObject = object2; //refObject becomes a reference to object2
refObject = object1; //is the same thing as object2 = object1
References behave more like aliases, and once you have bound them to an object you can't seperate them anymore. Pointers are just numbers that save the location of something in memory.
No, it's not a cast. As I said earlier, it's best to think of a reference to an object as the same thing as the object, with the only difference being that it's not a distinct entity, but rather a sort of "place holder" for another object.
The STL classes (like vector, map etc) are very perfomant. Trust me, the little extra overhead they have is completely neglibible, especially compared to the functionality gain they provide. In C++, use STL containers over arrays whenever possible, only use arrays when you really have to.