array of pointers and pointer to an array in c++ - c++

i have a class in which it's protected section i need to declare an array with unknown size (the size is given to the constructor as a parameter), so i looked around and found out that the best possible solution is to declare an array of pointers, each element points to an integer:
int* some_array_;
and simply in the constructor i'll use the "new" operator:
some_array_ = new int[size];
and it worked, my question is: can i declare an array in a class without defining the size? and if yes how do i do it, if not then why does it work for pointers and not for a normal array?
EDIT: i know vecotrs will solve the problem but i can't use them on my HW

You have to think about how this works from the compiler's perspective. A pointer uses a specific amount of space (usually 4 bytes) and you request more space with the new operator. But how much space does an empty array use? It can't be 0 bytes and the compiler has no way of knowing what space to allocate for an array without any elements and therefore it is not allowed.

You could always use a vector. To do this, add this line of code: #include <vector> at the top of your code, and then define the vector as follows:
vector<int> vectorName;
Keep in mind that vectors are not arrays and should not be treated as such. For example, in a loop, you would want to retrieve an element of a vector like this: vectorName.at(index) and not like this: vectorName[index]

Lets say that you have an integer array of size 2. So you have Array[0,1]
Arrays are continuous byte of memery, so if you declare one and then you want to add one or more elements to end of that array, the exact next position (in this case :at index 2(or the 3rd integer) ) has a high chance of being already allocated so in that case you just cant do it. A solution is to create a new array (in this case of 3 elements) , copy the initial array into the new and in the last position add the new integer. Obviously this has a high cost so we dont do it.
A solution to this problem in C++ is Vector and in Java are ArrayLists.

Related

Is there a way to store an array of integer variables, into a single element of another array?

So instead of using classes, or structs, I want to use a 5 element array. I want to use these just to keep track of simple numbers, and using several identical arrays, I can treat each one as an "object". However I'm trying to figure out if there's a way to store the entire array, into an element of another array, and be able to access the sub array elements when needed. It's been a while since I've done object oriented programming, so I'm struggling at the moment.
Thanks in advance
you can use a two-dimensional array in c++. a two dimensional array is kinda an array of arrays, and can be declared like this:
int ar[4][3];
(this is a 4 element array and each element is a 3 element array)
it can also be initiated too:
int ar[4][3]={{1,2,5},{10,15,6},{11,1,3},{7,5,3}};
and you can access the numbers like this:
a=ar[2][3];
(now 'a' is the 3rd element of the 2nd array of 'ar')
I hope it helps!

Why isn't my array default initialization 0?

I have this very large array, called grid. When I declare the array as below, every value in the array should be set to 0 according to the array constructor for integers
int testGrid[226][118];
However when I iterate through the entire array, I seem to get 0s for the majority of the array, however towards the lower part of the array I get arbitrary trash. The solution it is seems is to iterate over the array and manually set each value to 0. Is there a better way to do this?
You could do:
int testGrid[226][118] = {};
which will initialize your entries to 0.
Please see this C answer, which may come in handy for C++ too.
By the way, since this is C++, consider using an std::array, or an std::vector.

searching a 2D array given the address of one element

A rather quick question concerning pointers in c++
My problem is,let's say I have a function isWon(char * sign, int i, int j). I call this method by giving
the address of an element in a 2D array
it's coordinates in a locally declared array
Is there any way of e.g. knowing the elements neighbors and getting to them?
Thanks for the help :)
If the array is a true array 2D array and not an array of pointers or something like that, then you can add/subtract to/from sign to get other elements' addresses.
For example, memory-wise the previous element in the array is at sign - 1. If you think of your 2D array as a grid, sign - 1 might not be the element in the previous "column".
You have to be careful how much you step in your array and ask yourself why you resort to such low-level dangerous mechanisms that feel out of place in C++.

2D Dynamic Array, using two data types?

I've searched around the internet, and I can't seem to find the answer to my solution (or I'm blind/dumb and just can't figure out how to do it). Part of one of my assignments is as follows:
Constructor – creates an empty 2xn dynamic array. Your dynamic array should start as 2x5 in size but can grow to any length. The default value for empty `elements is “empty” and 0. The class should also have a nextElement variable that keeps track of the next empty spot in the array and is increment each time a data element is added.
Is there a way to create a 2*5 array that will accept string in one of the dimensions and integers in the other?

Empty vector in large array of objects

I have a large array of objects (10s Millions) of class A and I want to add a vector as a member to the class A. This vector is needed just for few percent of objects in the array. I was wondering, would it be a wise choice to add a vector to the class? how much memory will take an empty vector?
Now we know that empty vector are not very “big” (VC2012x64 intellisense show sizeof(std::vector<int>) is 16 byte). If sizeof(A) is much bigger than size-of vector adding a vector member to A could be a good solution for you. But if it is not good, and will add to much memory, and really not many A has the vector, I’d create a second container with the vectors. For example:
#include <unordered_map>
unordered_map<size_t , vector<T>> VectorForA;
where size_t is mean to be the type of the index of the big array of A, and vector<T> the type of the vector you want to add to A. This could be good for a fixed index "big" array. If somehow the A's in the big array dont have fixed positions, making the value of A the key could led to a simpler code(also, only if the values of A dont repeat).
NOTE: I was (I'm) wating to see a full answer from #Andy Prowl or #Tony D with I think will be very usefull