Dynamic array to int array

Hi all,

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...)

Here's what I started under main():
 
  int** mainArr=new int* [0];


Thanks in advance.
You have a good start. To initialize each array pointer in your array, you can do this:
mainArr[idx] = new int[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? ;)

Good luck!
Hi,

Thank you both.
You helped me a lot!

I have one more little question:
The program has switch, when I choose case '3' the program should exits.

BUT, I must delete all arrays (pointers and sub-arrays) properly to free the memory.

How should I do this?

I tried this with no success:
1
2
3
4
for (int i=0; i<lines; ++i)
delete [] mainArray[i];

delete [] mainArray;


Without '[]' did not work too...

Help please
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?
Actually, this is a long code.
Anyway, see below for "main points"...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void data(int** &mainArr, int num)
{
	mainArr[x]=new int[3];

	//...

}

int main()
{
	int num=0, choice;
	int** mainArr=new int* [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:

http://s23.postimg.org/fjw4wbv0b/error.jpg
Last edited on
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.
Last edited on
I'm towards the end of the code. Maybe I will learn to use smart pointers on my next code.
But until then, how do I fix the problem? What is the code?

I created an array pf 0 elements, but during the program the array increases...
Like I said, when I choose 'case 1' I add a pointer in 'mainArr'...

If I don't choose 'case 1' before I choose 'case 3' - it works fine. Why?
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.
How do I resize mainArr?

Just to add
 
int** mainArr=new int* [num];
?

It would 'overwrite' int** mainArr=new int* [0]; every time the array is increased?
Last edited on
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=new int* [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.
Last edited on
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.
You helped me out
Thanks a lot...
Topic archived. No new replies allowed.