I'm trying to allocate dynamic array (its size will be changed a few times during the program's running) which contains pointers to int arrays.
for example,
location 1 in the dynamic array will point to array of 3 numbers,
location 2 in the dynamic array will point to another array of 3 numbers and so on...
In addition, I would like to know how to access each location in the sub-arrays.
Hope I'm not complicating matters, but the changes of the sub-arrays values should be done by a function that is not the main function.
For instance, let's call it "void data(...)" (I'm not sure what suppose to be in the parentheses...)
You have a good start. To initialize each array pointer in your array, you can do this: mainArr[idx] = newint[3];
The subscript operator is designed to chain, so to access a sub array you do this: int num = mainArray[idx][jdx];
The operators evaluate left-to-right, so it is equivalent to saying:
1 2
int* innerPtr = mainArray[idx];
int num = innerPtr[jdx];
And lastly, you can pass the pointer to your array like any other pointer: void SomeFunction(int** 2dArrayPtr, int rows, int cols, ...);
Don't forget to pass the array dimensions to do bounds checking!
You can have even better start using C++11 or Boost's smart pointers.
Basically, smart pointers are pointers that allow you dynamic allocating, but they deal with garbage collection for you. They require little memory or processor power, and can save you much time with debugging(you won't have to look for memory leaks and such).
The only thing about smart pointers is that you have to remember some rules(but there aren't too many), that you have to obey in order for pointers to work.
You can google smart_ptr, unique_ptr, weak_ptr; these are currently in C++ standard.
You will probably have to use them sooner or later; why not learn now? ;)
I'm confused as to why that doesn't work. Can you post what you have (or part of what you have, if it's really long) and explain the errors you are getting?
void data(int** &mainArr, int num)
{
mainArr[x]=newint[3];
//...
}
int main()
{
int num=0, choice;
int** mainArr=newint* [0];
do {
cout << "Choose option 1-3" << endl;
cin >> choice;
switch (choice)
{
case 1: //send to 'data' function
data(mainArr, num);
//...
case 3: // exit
for (int i=0; i<num; ++i)
delete [] mainArr[i];
delete [] mainArr;
break;
}
} while (choise!=3)
}
If I choose '3' at first - it works fine (probably because there is no sub-arrays yet), but if I choose '1' (to create new sub-array of 3 elements) and then 3, I get this error message:
Oh, it's simple. You created an array of 0 elements. Later, you try to access its element(and there are none), so you got heap corruption.
PS. One of many reasons to use smart pointers. In your program you have to keep track of pointer by yourself, and your code has many dangers. Smart pointers prevent at least a portion of these.
You allocated your mainArr to have a size of zero, which means you don't have any space in that array to store anything. You have to allocate that array with at least size 1 for anything to work, and in your data function you will have to either resize mainArr or reject any arguments that try to allocate out of bounds.
Ideally, you should use an STL vector, since it is designed to be resizable. If you insist on using C++ arrays you will have to create a new larger one, then copy everything over, and delete the old one.
OK.
I created a temp array using int** tempArr=newint* [num];
Then I copyied all pointers from mainArr to temp, delete mainArr using delete[] mainArr; and changed the temporary array name to
'mainArr' by using tempArr=mainArr;
Is that correct?
When I close the program, delete[] mainArr; will delete mainArr and all sub-arrays? or should I use another command...? (I want the program to delete all 'leftovers' to free memory completely)
And...
How to delete one sub-array?
I mean, if mainArr in position [4] contains a pointer to a sub-array of 3 elements and I want to delete this pointer and sub-array only.
Well you assignment is backwards, it should be mainArr = tempArr.
You need to delete everything as you did before:
1 2 3
for (int i = 0; i < num; ++i)
delete[] mainArr[i];
delete[] mainArr;
The problem with deleting a single sub-array is that you have resize the array to be smaller. This is why a vector works because it will take care of these operations for you.