I'm newbie and I need little help.
I tried different ways, but from what I can see NULL means 0 in c ++, so when want my (for example) int arr[4]=0, then it says that it's also NULL. Is there any way to see when value is 0 and not NULL (or empty?).
Sounds like a job for std::optional:
#include <iostream>
#include <optional>
int main()
{
std::optional<int> arr[] = { 10, 0, std::nullopt, -52 };
for (auto i = 0U; i < sizeof(arr) / sizeof(*arr); i++)
{
if (arr[i])
{
std::cout << "arr[" << i << "] = " << *(arr[i]) << std::endl;
}
else
{
std::cout << "arr[" << i << "] = std::nullopt" << std::endl;
}
}
}
Output:
arr[0] = 10
arr[1] = 0
arr[2] = std::nullopt
arr[3] = -52
It sounds like you're looking for a null concept for your data type (int array[4]), but you want 0 to be a valid (non-null) value. The null concept is useful when your variable is not guaranteed to hold valid data. You need a null concept if, for a variable x, you want to be able to ask the question "does x contain a valid value?".
First, recognize that NULL is an implementation-defined way to represent a pointer that points at nothing. Here, implementation means "specific compiler". Usually, NULL is an integer constant equal to 0, which in practice makes it ill-suited to differentiate from your otherwise-valid value of 0.
TL;DR: I think you want to use std::vector, but you have several options available.
Null concept for int
Depending on what your data represents, you may be able to represent a null by selecting a value or range of values that are not valid for your specific use case, but otherwise representable using your data type.
Some example null concepts for int data type:
0-- is a perfectly fine null when 0 is not a valid value. The null test code is very clean as well: if (x) { ... }.
negative values-- You can select a specific negative value or the entire range of negative values. Or even a subrange of all negative values, although I've never seen this last one in practice. Generally the specific value of -1 is used. The null test code looks like: if (x >= 0) { ... } or if (x != -1) { ... }.
extreme positive values-- You can select a very large number to represent the null concept. If a valid instance of your data will never reach this value. I recommend std::numeric_limits<int>::max(), which requires #include <limits>. The null test looks like: if (x != std::numeric_limits<int>::max()) { ... }
std::optional<T>
When all possible values of your data type represent valid values, then you need to include extra overhead if you want to represent the null concept. std::optional<T> wraps any data type and is used specifically for the case where you need to represent invalid data. The access semantics looks very similar to accessing a pointer.
#include <optional>
void do_something(int);
int main() {
std::optional<int> a; // default initialization is "empty"
std::optional<int> b = 1; // can be assigned just as if it were the type
if (a) { // You can check in a natural-feeling way if the data is valid
do_something(*a); // and access the data as if it were a pointer
do_something(a.value()); // or use std::optional<T>::value()
}
// If the data may be invalid, you must check before accessing
// *a; // WRONG: throws an exception
// a.value(); // WRONG: for the same reason
// If you know for certain the data is valid, feel free to access it
do_something(*b);
// You can't access it entirely as if it were an int, dereferencing is necessary
// int c = b + 2; // invalid, no overloaded operator+ for std::optional<int> type
int c = *b + 2; // valid
// An array of optionals looks similar to an array of ints
std::optional<int> array[4]; // initialized as all empty
Just don't deal with invalid data at all
If your data doesn't necessarily need to exist until it is valid then you can use a variable size container to just... not have invalid data at all.
std::vector<T> is the go-to dynamically sized container in C++, and allows you to have just enough space to handle only the data you need. std::vector<T> also has many class methods that allow easy access to container information such as std::vector<T>::size() or iterators with std::vector<T>::begin() and std::vector<T>::end().
#include <vector>
void do_something(int);
int main() {
std::vector<int> data; // initially empty, no elements
// you can add new values
data.push_back(1); // vector contains { 1 }
data.emplace_back(2); // vector contains { 1 , 2 }
// looping is the same as with arrays
for (int i = 0; i < data.size(); i++) {
do_something(data[i]);
}
// or you can use range-based for loops for cleaner looking code
for (auto& d : data) {
do_something(d);
}
}
If that is an array of pointers:
int* arr[10] = {};
Then here is how you check if an element is null:
if (arr[4] == nullptr)
And here is how you check if the pointed value is zero:
if (*arr[4] == 0) // note the *
Related
I am trying to create a sorting function with the parameters being a pointer of a list and I am trying to access an element of the given list. Hopefully this code speaks for the problem better than I can:
void bubbleSort(std::vector<int> *L) {
unsigned int i = 0; int temp;
while(isSorted(*L)) {
if(i==L->size()-1) {
i = 0;
}
if(i<L[i]/*<-ERROR here.*/) {
temp = L[i+1]; // ERROR HERE
L[i+1] = L[i]; // ERROR HERE
L[i] = temp; // ERROR HERE
}
}
}
You don't need to painfully dereference every individual use of L (and indeed doing so is error-prone, as you've demonstrated by missing one in your answer).
Instead, just write:
void bubbleSort(std::vector<int> *Lptr) {
auto &L = *Lptr;
and keep the rest of the code the same.
NB. It would be even better to change the function itself, to
void bubbleSort(std::vector<int> &L) {
as it should have been written in the first place, but I'm assuming there's some artificial reason you can't do that.
The function accepts a pointer to an object of type std::vector<int>.
void bubbleSort(std::vector<int> *L) {
To access the original vector using the pointer, you can write either *L or L[0]. That is, both expressions yield an lvalue reference of type std::vector<int> & to the vector.
To get the i-th element of the vector using the subscript operator through the pointer, you can write either (*L)[i] or L[0][i],
However, in this if statement:
if(i<L[i]/*<-ERROR here.*/) {
You are trying to compare the variable i of type unsigned int to the object L[i] of type std::vector<int>. When i is not equal to 0, this yields a non-existent object of the vector type.
It seems you mean something like the following instead:
if ( (*L)[i] < (*L)[i+1] ) {
or:
if ( L[0][i] < L[0][i+1] ) {
or, vice versa:
if ( L[0][i+1] < L[0][i] ) {
Depending on whether the vector is sorted in ascending or descending order.
Pay attention to the fact that there is no sense in declaring the parameter as a pointer to a std::vector<int>. The function would be much clearer and readable if it accepted the vector by reference instead:
void bubbleSort(std::vector<int> &L) {
In this case, the if statement would look like this:
if ( L[i] < L[i+1] ) {
Although I prefer to change the source code as other answer. But, for this question, you can use ->at() function to access the element in a vector pointer.
if(i<L->at(i)) {
temp = L->at(i+1);
L->at(i+1) = L->at(i);
L->at(i) = temp;
}
I am confused about the output in the following program about the vec in Test. Why it's a vector with size 100 instead of 1? I thought std::vector<T> var{a} is the same as std::vector<T> var = {a}.
#include <iostream>
#include <vector>
using namespace std;
struct Value {
int a;
int b;
};
class Test {
public:
std::vector<struct Value> vec{100};
};
class Test2 {
public:
std::vector<int> vec{100};
};
int main()
{
Test test;
std::cout << "test size: " << test.vec.size() << std::endl;
Test2 test2;
std::cout << "test2 size: " << test2.vec.size();
return 0;
}
Output:
test size: 100
test2 size: 1
std::vector has a constructor with a std::initializer_list<T> argument. When using an initializer list like {100} this constructor will always take priority, if it is applicable.
For a std::vector<int> the initializer {100} is compatible with std::initializer_list<int> so that constructor will be used. It will create a vector containing the collection {100} which is a single int with the value 100.
For std::vector<Value> the initializer {100} is NOT compatible with a std::initializer_list<Value> argument. Value has no converting constructor for turning an int to a Value so you cannot construct a std::initializer_list<Value> from {100}. You can verify that this conversion is not allowed with this example. The compiler will try to take a lower priority constructor instead, and will use the constructor which initializes 100 default constructed Value.
If you add a Value::Value(int) constructor or use {{100}} as the initializer for Test2 you will find that the std::vector<Value> will now only contain a single element. In both cases, the initializer list is convertible to std::initializer_list<Value> and that constructor will now be used.
As you discovered the meaning of {100}, changes for T == int.
To answer your question briefly:
The 100 in vector<Value>{100} cannot be interpreted as a Value and therefore the size constructor takes precedence.
If you insist, {100} can be interpreted as Value, so you may need an extra curly braces, vector<Value>{ {100} }.
See the illustration here: https://godbolt.org/z/xcMT1oc5z
My advice, avoiding further discussion on legalities, is the following:
To keep the meaning across types, initialize consistently parenthesis for size-initialization and brackets for element(s), which forces you to do this:
std::vector<int> vec = std::vector<int>(100);
And in general:
std::vector<T> vec = std::vector<T>(100);
In this case 100 is always the size.
This may not be a totally helpful answer, but I decided to put a breakpoint in the class declaration for vector in the STD library.
Answer
In the definition of a vector, there are 3 ways it handles the assignment.
A struct will provide a value-construction, where as an int will be assigned as a sized range construction.
It reads std::vector<struct> vect{100}; as building a vector of length 100, while std::vector<int> vect{100}; is acting similarly vect.insert(v.end(),100);
This is based on the type of object passed in for .
For information's sake, the final option is taking a given value, and assigning it to a number of spots. So if you had 100, "x", it would put "x" into your vector 100 times.
The Journey
What I learned from this is that there's a point where your vector takes a size_type input and a _Valty&& input (which I don't know what that is yet. Will be looking it up later) and provides a construction between 3 different args.
My best guess would be that your struct is filling in for 1-args path and acts as a length declaration, while int as a native type falls into the 2-args path and acts as a value assignment.
The sizeof(Value) may == 0, while the size of an int will be 1.
Edit: I guessed 1 and 2 (or _Count == 0, and Count == 1), however I was wrong about this. It's _Count == 0 and _Count == 2. Which was very interesting.
template <class... _Valty>
_CONSTEXPR20 void _Construct_n(_CRT_GUARDOVERFLOW const size_type _Count, _Valty&&... _Val) {
// Dispatches between the three sized constructions.
// 1-arg -> value-construction, e.g. vector(5)
// 2-arg -> fill, e.g. vector(5, "meow")
// 3-arg -> sized range construction, e.g. vector{"Hello", "Fluffy", "World"}
auto& _Al = _Getal(); //////////////// For test1, _Count is 100, for test2, _Count is 1;
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Al);
auto& _My_data = _Mypair._Myval2;
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _My_data);
if (_Count != 0) {
_Buy_nonzero(_Count);
_Tidy_guard<vector> _Guard{this};
// This one happens with a struct
if constexpr (sizeof...(_Val) == 0) {
_My_data._Mylast = _Uninitialized_value_construct_n(_My_data._Myfirst, _Count, _Al);
} else
if constexpr (sizeof...(_Val) == 1) {
_STL_INTERNAL_STATIC_ASSERT(is_same_v<_Valty..., const _Ty&>);
_My_data._Mylast = _Uninitialized_fill_n(_My_data._Myfirst, _Count, _Val..., _Al);
} else
// This one happens with an int
if constexpr (sizeof...(_Val) == 2) {
_My_data._Mylast = _Uninitialized_copy(_STD forward<_Valty>(_Val)..., _My_data._Myfirst, _Al);
} else {
static_assert(_Always_false<_Ty>, "Should be unreachable");
}
_Guard._Target = nullptr;
}
_Proxy._Release();
}
What's really interesting as that it appears to be happening when the Allocator reference is assigned. I'm by no means an expert in the language, but I really wanted to figure this puzzle one! Thanks for the interesting challenge!
If you've never walked through a class definition before, I would recommend trying it out.
If I were to create an array with int* array = new int[10]; and fill part of the array with values, how can I check how much of the array is filled? I want to loop through and check if each value is the default value but I'm not sure what the default value of each array element is. Would it be null 0 or garbage values?
This is how to set a default value in C++ when making an array.
int array[100] = {0};
Now every element is set to 0. Without doing this every element it garbage and will be undefined behavior if used.
Not all languages are like this. Java has default values when declaring a data structure but C++ does not.
There is no default value so it's garbage.
You can't do what are you hoping to, not when the type is int.
The uninitialized elements of the array will have unpredictable values. In addition, accessing those elements is cause for undefined behavior.
You can initialize the elements of the array to a sentinel value at the time of allocation using:
int* ptr = new int[10]{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
Use whatever sentinel value works for you if -1 does not.
The default value of array is indeterminate means garbage.
how can I check how much of the array is filled?
You cannot check, C/C++ has no array bounds check. You have to do it yourself.You need to keep track of the data inserted by a user. When your counter reaches the size of the array, the array is full
You can solve your problem by a more C++ way. You can create struct or class, which contain your value and bool flag. Bool flag must be set to false in default constructor and set to true in operator=. There is ready implementation of such class - boost.optional. std::optional will be in C++17.
#include <boost/optional.hpp>
#include <iostream>
int main()
{
const size_t nArr = 100;
auto pArr = new boost::optional<int>[nArr];
const size_t nInit = 30;
for (size_t i = 0; i < nInit; ++i)
{
pArr[i] = i; //initialize nInit first values of pArr
}
size_t n = 0;
for (; n < nArr; ++n)
{
if (!pArr[n].is_initialized()) break;
// or more compact form:
//if(!pArr[n]) break;
assert(*pArr[n] == n);
}
std::cout << "nInit = " << nInit << ", n = " << n << std::endl;
assert(nInit == n);
delete[] pArr;
}
It should be pointed out the default is uninitialized only for basic types like int. classes will use their defined parameterless constructor. Here is a MWE that wraps an int in such a class. OP also asked about checking the values so that is here too:
#include <iostream>
struct Element {
int Value;
Element() : Value{42} {}
};
struct Array {
Element Values[10];
};
int main() {
Array array;
for(Element element: array.Values)
std::cout << element.Value << " ";
}
Note: POD (or record) classes, which have no custom constructor, will remain uninitialised. Non-POD classes that have no parameterless constructor cannot usually be held in arrays.
I don't know if this helps but in c++17 onwards there's the std::array class that can be used. In this class you have the option to use class functions such as std::array.fill(...)
std::array<int, 10> arr;
arr.fill(-1);
cpp docs: cpp-docs
This get's you closer to the experience from other languages such as Java and Python both having Arrays.fill(arr, -1); and arr = [-1]*N respectively to fill the array with non-default values.
By default, the objects allocated by new are default initialized. This mean when you wrote:
int* array = new int[10]; //block of 10 uninitialized ints
Here, all of the 10 ints are uninitialized. That is, they have indeterminate value.
It is possible to value initialize the elements by adding an empty pair of parenthesis as shown below:
int* array = new int[10](); //block of 10 ints value initialized to 0
Here all of the 10 ints are initialized to 0.
But note that although we can use empty parentheses to value initialize the elements of an array, we cannot supply an element initializer inside the parentheses. This means for example,
int* array = new int[10](55); // INVALID
The above statement is invalid because we cannot supply an element initializer inside the parenthesize.
You can use std::fill_n instead of supplying an element initializer inside the parentheses as shown below:
int* array = new int[10]; //block of 10 uninitialized ints
std::fill_n(array, 10, 55); // all elements will now hold 55
If I were to create an array with int* array = new int[10]; and fill part of the array with values, how can I check how much of the array is filled? I want to loop through and check if each value is the default value but I'm not sure what the default value of each array element is. Would it be null 0 or garbage values?
This is how to set a default value in C++ when making an array.
int array[100] = {0};
Now every element is set to 0. Without doing this every element it garbage and will be undefined behavior if used.
Not all languages are like this. Java has default values when declaring a data structure but C++ does not.
There is no default value so it's garbage.
You can't do what are you hoping to, not when the type is int.
The uninitialized elements of the array will have unpredictable values. In addition, accessing those elements is cause for undefined behavior.
You can initialize the elements of the array to a sentinel value at the time of allocation using:
int* ptr = new int[10]{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
Use whatever sentinel value works for you if -1 does not.
The default value of array is indeterminate means garbage.
how can I check how much of the array is filled?
You cannot check, C/C++ has no array bounds check. You have to do it yourself.You need to keep track of the data inserted by a user. When your counter reaches the size of the array, the array is full
You can solve your problem by a more C++ way. You can create struct or class, which contain your value and bool flag. Bool flag must be set to false in default constructor and set to true in operator=. There is ready implementation of such class - boost.optional. std::optional will be in C++17.
#include <boost/optional.hpp>
#include <iostream>
int main()
{
const size_t nArr = 100;
auto pArr = new boost::optional<int>[nArr];
const size_t nInit = 30;
for (size_t i = 0; i < nInit; ++i)
{
pArr[i] = i; //initialize nInit first values of pArr
}
size_t n = 0;
for (; n < nArr; ++n)
{
if (!pArr[n].is_initialized()) break;
// or more compact form:
//if(!pArr[n]) break;
assert(*pArr[n] == n);
}
std::cout << "nInit = " << nInit << ", n = " << n << std::endl;
assert(nInit == n);
delete[] pArr;
}
It should be pointed out the default is uninitialized only for basic types like int. classes will use their defined parameterless constructor. Here is a MWE that wraps an int in such a class. OP also asked about checking the values so that is here too:
#include <iostream>
struct Element {
int Value;
Element() : Value{42} {}
};
struct Array {
Element Values[10];
};
int main() {
Array array;
for(Element element: array.Values)
std::cout << element.Value << " ";
}
Note: POD (or record) classes, which have no custom constructor, will remain uninitialised. Non-POD classes that have no parameterless constructor cannot usually be held in arrays.
I don't know if this helps but in c++17 onwards there's the std::array class that can be used. In this class you have the option to use class functions such as std::array.fill(...)
std::array<int, 10> arr;
arr.fill(-1);
cpp docs: cpp-docs
This get's you closer to the experience from other languages such as Java and Python both having Arrays.fill(arr, -1); and arr = [-1]*N respectively to fill the array with non-default values.
By default, the objects allocated by new are default initialized. This mean when you wrote:
int* array = new int[10]; //block of 10 uninitialized ints
Here, all of the 10 ints are uninitialized. That is, they have indeterminate value.
It is possible to value initialize the elements by adding an empty pair of parenthesis as shown below:
int* array = new int[10](); //block of 10 ints value initialized to 0
Here all of the 10 ints are initialized to 0.
But note that although we can use empty parentheses to value initialize the elements of an array, we cannot supply an element initializer inside the parentheses. This means for example,
int* array = new int[10](55); // INVALID
The above statement is invalid because we cannot supply an element initializer inside the parenthesize.
You can use std::fill_n instead of supplying an element initializer inside the parentheses as shown below:
int* array = new int[10]; //block of 10 uninitialized ints
std::fill_n(array, 10, 55); // all elements will now hold 55
In C++11 you can use a range-based for, which acts as the foreach of other languages. It works even with plain C arrays:
int numbers[] = { 1, 2, 3, 4, 5 };
for (int& n : numbers) {
n *= 2;
}
How does it know when to stop? Does it only work with static arrays that have been declared in the same scope the for is used in? How would you use this for with dynamic arrays?
It works for any expression whose type is an array. For example:
int (*arraypointer)[4] = new int[1][4]{{1, 2, 3, 4}};
for(int &n : *arraypointer)
n *= 2;
delete [] arraypointer;
For a more detailed explanation, if the type of the expression passed to the right of : is an array type, then the loop iterates from ptr to ptr + size (ptr pointing to the first element of the array, size being the element count of the array).
This is in contrast to user defined types, which work by looking up begin and end as members if you pass a class object or (if there is no members called that way) non-member functions. Those functions will yield the begin and end iterators (pointing to directly after the last element and the begin of the sequence respectively).
This question clears up why that difference exists.
I think that the most important part of this question is, how C++ knows what the size of an array is (at least I wanted to know it when I found this question).
C++ knows the size of an array, because it's a part of the array's definition - it's the type of the variable. A compiler has to know the type.
Since C++11 std::extent can be used to obtain the size of an array:
int size1{ std::extent< char[5] >::value };
std::cout << "Array size: " << size1 << std::endl;
Of course, this doesn't make much sense, because you have to explicitly provide the size in the first line, which you then obtain in the second line. But you can also use decltype and then it gets more interesting:
char v[] { 'A', 'B', 'C', 'D' };
int size2{ std::extent< decltype(v) >::value };
std::cout << "Array size: " << size2 << std::endl;
According to the latest C++ Working Draft (n3376) the ranged for statement is equivalent to the following:
{
auto && __range = range-init;
for (auto __begin = begin-expr,
__end = end-expr;
__begin != __end;
++__begin) {
for-range-declaration = *__begin;
statement
}
}
So it knows how to stop the same way a regular for loop using iterators does.
I think you may be looking for something like the following to provide a way to use the above syntax with arrays which consist of only a pointer and size (dynamic arrays):
template <typename T>
class Range
{
public:
Range(T* collection, size_t size) :
mCollection(collection), mSize(size)
{
}
T* begin() { return &mCollection[0]; }
T* end () { return &mCollection[mSize]; }
private:
T* mCollection;
size_t mSize;
};
This class template can then be used to create a range, over which you can iterate using the new ranged for syntax. I am using this to run through all animation objects in a scene which is imported using a library that only returns a pointer to an array and a size as separate values.
for ( auto pAnimation : Range<aiAnimation*>(pScene->mAnimations, pScene->mNumAnimations) )
{
// Do something with each pAnimation instance here
}
This syntax is, in my opinion, much clearer than what you would get using std::for_each or a plain for loop.
It knows when to stop because it knows the bounds of static arrays.
I'm not sure what do you mean by "dynamic arrays", in any case, if not iterating over static arrays, informally, the compiler looks up the names begin and end in the scope of the class of the object you iterate over, or looks up for begin(range) and end(range) using argument-dependent lookup and uses them as iterators.
For more information, in the C++11 standard (or public draft thereof), "6.5.4 The range-based for statement", pg.145
How does the range-based for work for plain arrays?
Is that to read as, "Tell me what a ranged-for does (with arrays)?"
I'll answer assuming that - Take the following example using nested arrays:
int ia[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
for (auto &pl : ia)
Text version:
ia is an array of arrays ("nested array"), containing [3] arrays, with each containing [4] values. The above example loops through ia by it's primary 'range' ([3]), and therefore loops [3] times. Each loop produces one of ia's [3] primary values starting from the first and ending with the last - An array containing [4] values.
First loop: pl equals {1,2,3,4} - An array
Second loop: pl equals {5,6,7,8} - An array
Third loop: pl equals {9,10,11,12} - An array
Before we explain the process, here are some friendly reminders about arrays:
Arrays are interpreted as pointers to their first value - Using an array without any iteration returns the address of the first value
pl must be a reference because we cannot copy arrays
With arrays, when you add a number to the array object itself, it advances forward that many times and 'points' to the equivalent entry - If n is the number in question, then ia[n] is the same as *(ia+n) (We're dereferencing the address that's n entries forward), and ia+n is the same as &ia[n] (We're getting the address of the that entry in the array).
Here's what's going on:
On each loop, pl is set as a reference to ia[n], with n equaling the current loop count starting from 0. So, pl is ia[0] on the first round, on the second it's ia[1], and so on. It retrieves the value via iteration.
The loop goes on so long as ia+n is less than end(ia).
...And that's about it.
It's really just a simplified way to write this:
int ia[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
for (int n = 0; n != 3; ++n)
auto &pl = ia[n];
If your array isn't nested, then this process becomes a bit simpler in that a reference is not needed, because the iterated value isn't an array but rather a 'normal' value:
int ib[3] = {1,2,3};
// short
for (auto pl : ib)
cout << pl;
// long
for (int n = 0; n != 3; ++n)
cout << ib[n];
Some additional information
What if we didn't want to use the auto keyword when creating pl? What would that look like?
In the following example, pl refers to an array of four integers. On each loop pl is given the value ia[n]:
int ia[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
for (int (&pl)[4] : ia)
And... That's how it works, with additional information to brush away any confusion. It's just a 'shorthand' for loop that automatically counts for you, but lacks a way to retrieve the current loop without doing it manually.
Some sample code to demonstrate the difference between arrays on Stack vs arrays on Heap
/**
* Question: Can we use range based for built-in arrays
* Answer: Maybe
* 1) Yes, when array is on the Stack
* 2) No, when array is the Heap
* 3) Yes, When the array is on the Stack,
* but the array elements are on the HEAP
*/
void testStackHeapArrays() {
int Size = 5;
Square StackSquares[Size]; // 5 Square's on Stack
int StackInts[Size]; // 5 int's on Stack
// auto is Square, passed as constant reference
for (const auto &Sq : StackSquares)
cout << "StackSquare has length " << Sq.getLength() << endl;
// auto is int, passed as constant reference
// the int values are whatever is in memory!!!
for (const auto &I : StackInts)
cout << "StackInts value is " << I << endl;
// Better version would be: auto HeapSquares = new Square[Size];
Square *HeapSquares = new Square[Size]; // 5 Square's on Heap
int *HeapInts = new int[Size]; // 5 int's on Heap
// does not compile,
// *HeapSquares is a pointer to the start of a memory location,
// compiler cannot know how many Square's it has
// for (auto &Sq : HeapSquares)
// cout << "HeapSquare has length " << Sq.getLength() << endl;
// does not compile, same reason as above
// for (const auto &I : HeapInts)
// cout << "HeapInts value is " << I << endl;
// Create 3 Square objects on the Heap
// Create an array of size-3 on the Stack with Square pointers
// size of array is known to compiler
Square *HeapSquares2[]{new Square(23), new Square(57), new Square(99)};
// auto is Square*, passed as constant reference
for (const auto &Sq : HeapSquares2)
cout << "HeapSquare2 has length " << Sq->getLength() << endl;
// Create 3 int objects on the Heap
// Create an array of size-3 on the Stack with int pointers
// size of array is known to compiler
int *HeapInts2[]{new int(23), new int(57), new int(99)};
// auto is int*, passed as constant reference
for (const auto &I : HeapInts2)
cout << "HeapInts2 has value " << *I << endl;
delete[] HeapSquares;
delete[] HeapInts;
for (const auto &Sq : HeapSquares2) delete Sq;
for (const auto &I : HeapInts2) delete I;
// cannot delete HeapSquares2 or HeapInts2 since those arrays are on Stack
}