problem with pointer to a pointer

so i get the following error when i run my code
Run-Time Check Failure #2 - Stack around the variable 'parr1' was corrupted.

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
	int parr=0;
	int *parr1=&parr;
	int **parr2=&parr1;

	for(int i=0;i<2;i++)
	{

	*(parr2+i) = new int [5]; 

	}



	for(int j=0;j<2;j++)
	{
		for(int i=0;i<5;i++)
		{
			parr2[j][i]=1;
			std::cout << parr2[j][i];
		}
		std::cout << std::endl;
	}


	std::cin.get();
	for(int i=0;i<2;i++)
	{
		delete [i]parr2;

	}




now i am trying to understand pointers and i some what do but can some one explain why this code doesnt work and what would and why ?
it seems to me that i am deleting parr2 wrong if so then how would i go about doing it and how it works ?
First of all let me ask this, why do you need to have a pointer to a pointer? It's kinda redundant. From my understanding, a pointer is simply a variable that literally points to another variable. I may be wrong on that, but whatever xD.

And P.S.: you can use "using namespace std;" after your "#include"s to make it so you don't have to use std::
the pointer to a pointer is just to have a 2 dimensional array of any size
was to lasy to do using namespace std xD
because i dont use cout much
Oh, well I don't seem to be of much use to you
And I find that ironic, I use "using namespace std" to BE lazy xD
Try

1
2
3
4
int parr=0;
int *parr1=&parr;
int *parr2=parr1;
cout << *parr2;
closed account (28poGNh0)
I dont know what do want to achieve,however I think this may help

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
#include <iostream>
using namespace std;

int main()
{
	int parr=0;
	int *parr1=&parr;
	int **parr2=&parr1;

	for(int i=0;i<2;i++)
        *(parr2+i) = new int [5];

	for(int j=0;j<2;j++)
	{
		for(int i=0;i<5;i++)
		{
			parr2[j][i]=1;
			cout << parr2[j][i];
		}
		cout << endl;
	}

	for(int i=0;i<2;i++)
        delete[] parr2[i];
    delete[] parr2;

	cin.get();

    return 0;
}
While parr2 points to a well defined variable (parr2 + i) doesn't.

You may initialize parr2 as

parr2 = new int*[2];


EDIT: Fixed syntax error
Last edited on
I would do it this way.

In fact, I wouldn't. I'd use a vector.

But if I was insane and needed to use dynamic 2D arrays, I'd do it this way.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <ctime>

int main( int argc, char* argv[] )
{
  int **myArray;
  int someSize = -1;
  srand( time( NULL ) );

  std::cout << "Enter size between 1 and 10: ";

  while( !( std::cin >> someSize ) || someSize < 1 || someSize > 10  )
  {
    std::cout << "Invalid size, enter again: ";
    std::cin.clear();
    std::cin.ignore( 256, '\n' );
  }

  // Allocate array memory
  myArray = new int*[someSize];

  for( int i=0; i < someSize; ++i )
  {
    myArray[i] = new int[someSize];
  }

  // Populate with random data
  for( int i=0; i < someSize; ++i )
  {
    for( int j=0; j < someSize; ++j )
    {
      myArray[i][j] = rand() % 100 + 1;
      std::cout << myArray[i][j] << " ";
    }
    std::cout << "\n";
  }

  // Delete memory
  for( int i=0; i < someSize; ++i )
  {
    delete [] myArray[i];
  }

  delete [] myArray;

  return 0;
}

Last edited on
closed account (28poGNh0)
@iHutch105

I was gonna him the same code as yours, because that's how I do it mostly but I thought what if
*(parr2+i) = new int [5]; is also right so do you have any anti prove that it is wrong

same question for you @tcs
Last edited on
well what i am trying to do is to makepointer to a pointer behave like a 2 dimensional array
techno there was an error message with your code
and hutch where would the x size and y size of the array go ?
here is my code so far
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
34
35
36
#include <iostream>
using namespace std;

int main()
{
	int **parr2;

	int someSize=3;
	parr2 = new int*[someSize];
	
	for( int i=0; i < someSize; ++i )
    {
    parr2[i] = new int[someSize];
    }

	for(int j=0;j<someSize;j++)
	{
		for(int i=0;i<someSize;i++)
		{
			parr2[j][i]=1;
			cout << parr2[j][i];
		}
		cout << endl;
	}

	for(int i=0;i<someSize;i++)
	{
        delete[] parr2[i];

	}
    delete[] parr2;

	cin.get();

    return 0;
}


but it gives me the following error
Windows has triggered a breakpoint in thetest2.exe.

This may be due to a corruption of the heap, which indicates a bug in thetest2.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while thetest2.exe has focus.

The output window may have more diagnostic information.

and after pressing continue bunch of times i get

Debug Assertion Failed

Program ....


Expression _crtisvalidheappointer.... etc
Last edited on
@Techno01:
1. In your solution parr2 points to an array of exactly one element. So referencing (parr2 + 1) and (parr2 + 2) will access unallocated memory.
2. Line 25 will also fail because you've never allocated parr2. It just points to the stack - not to the heap.
1
2
3
4
5
6
7
   int someSize=3;
   parr2 = new int*[someSize];

   for( int i=0; i < 5; ++i )
   {
      parr2[i] = new int[someSize];
   }
parr2[3] and parr2[4] are invalid

Learn to indent.
xD you are right well was testing all the codes given so i must have missed that sry about that but if lets say i want it to be 5 in hight 3 in width where would i put those values ?? sry for being such an idiot and thx for all the help
Last edited on
¿what does the first dimension represent?
ne i don't get your question ?? width??
now i figured it out i think

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
34
35
36
37
#include <iostream>
using namespace std;

int main()
{
	int **parr2;
	int xc=5;
	int yc=3;
	int someSize=5;
	parr2 = new int*[yc];
	
	for( int i=0; i < yc; ++i )
    {
    parr2[i] = new int[xc];
    }

	for(int j=0;j<yc;j++)
	{
		for(int i=0;i<xc;i++)
		{
			parr2[j][i]=1;
			cout << parr2[j][i];
		}
		cout << endl;
	}

	for(int i=0;i<yc;i++)
	{
        delete[] parr2[i];

	}
    delete[] parr2;

	cin.get();

    return 0;
}


is that right ?
if it is then i understand almost everything in this code besides the delete part can some one explain it ?
If you want it to represent width, it can represent width
If you want it to represent height, it can represent height


If you allocate with new[] you must deallocate with delete[] ¿what you don't understand?
Never mind I got it :D thanks all I really appreciate it.
Just been working on this for 5 hours searching the net etc.. so got a little tired
srsly though thx helped allot.
Topic archived. No new replies allowed.