Pointer program keeps crashing

Why does my pointer to pointer program keep crashing? Shouldn't I be able to point to another pointer? This pointer stuff is so damn confusing, despite the fact I've deleted both variables. In this case, the value of "a" should point to the value of "b;" b itself should be equal to another variable.

1
2
3
4
5
6
7
8
9
10
11
12
 #include <iostream>
using namespace std;
int main()
{
int **b, *a;
**b=*a;
*a=4;
cout<<**b;
delete a;
delete b;
return 0;
}
Last edited on
Why does my pointer to pointer program keep crashing?
Because you delete memory you never allocated.

This pointer stuff is so damn confusing,
Yes, certainly pointers are difficult so don't use them. In modern C++ you can do without them. The STL has vectors, list, set, map so better learn to use them.
closed account (E0p9LyTq)
In modern C++ you can do without them.

And if someone absolutely must use a pointer the STL has smart pointers. Available in many different flavors.
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
   int* a = new int(0);
   int* b = new int(20);
   
   std::cout << "*a: " << std::dec << *a << "\ta: x" << std::hex << a << '\n';
   std::cout << "*b: " << std::dec << *b << "\tb: x" << std::hex << b << '\n';

   b = a;  // memory leak!  No way to access b's previous address!
   *a = 4;

   std::cout << "*a: " << std::dec << *a << "\ta: x" << std::hex << a << '\n';
   std::cout << "*b: " << std::dec << *b << "\tb: x" << std::hex << b << '\n';

   delete a;
   // delete b; // can't delete memory already released!
}

*a: 0   a: x0354C300
*b: 20  b: x0354C380

*a: 4   a: x0354C300
*b: 4   b: x0354C300
Last edited on
Thank you for the responses and the program above. I'm really having a hard time figuring out when to use the "*" and the "&" symbol. The below program (right below the paragraph) works, but I can't figure out why? Normally don't you have to use a "*" in order to get a variable equal to a value (eg. *a=b)? I think it's because you can't physically set a pointer directly to the value in another pointer.

#include <iostream>
int main()
{
int **a, *b;
a=&b;
*b=4;
cout<<**a;
return 0;
}

The below doesn't work though. The program should put the variable of A=B=C. I thought adding a normal variable c would fix the problem. I can normally set *A=another variable.

#include <iostream>
int main()
{
int **a, *b, c;
**a=*b;
*b=c;
c=3;
cout<<**a;
return 0;
}
Last edited on
The below program works, but I can't figure out why?

No, it does not. What you have is undefined behaviour. Too bad it deceptively seems to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
int main()
{
  int* * a; // The a is a pointer that is not set to point to anywhere
  int* b; // The b is a pointer that is not set to point to anywhere
  a = &b; // The a now points to b

  *b = 4; // value 4 is assigned to the integer that the b points to #error1

  cout << **a; // by deferencing a (*a) we reach the object the a points to (b)
  // the b is pointer, so by deferencing it (**a) we reach the object the b points to
  // #error2

  return 0;
}

#error1 is fatal: you write to some unknown memory location. Anything could happen due to that.

#error2 simple attempts to read from unknown memory location. That happens to be the same memory, where you did previously write integer 4.

How to fix that? For example:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main()
{
  int foo; // real integer
  int* b = &foo;
  int** a = &b;

  *b = 4; // value 4 is assigned to foo
  cout << **a; // value is read from foo

  return 0;
}
closed account (E0p9LyTq)
PLEASE, learn to use code tags, it makes reading your source MUCH easier
http://www.cplusplus.com/articles/jEywvCM9/

You can edit your posts and add code tags.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

int main()
{
   // pointers can point to variable
   int  a = 5;
   int* b = &a;
   std::cout << "a:  " << std::dec << a << "\t&a: " << std::hex << &a << '\n';
   std::cout << "*b: " << std::dec << *b << "\tb:  " << std::hex << b << "\t&b: " << &b << "\n\n";

   // pointers can point to memory on the heap
   int* c = new int(20);
   std::cout << "*c: " << std::dec << *c << "\tc:  " << std::hex << c << "\t&c: " << &c << "\n\n";

   // pointers can point to the first element of an array
   int  d[ ] = { 1, 2, 3, 4, 5 };
   int* e = d;

   for (size_t loop = 0; loop < 5; loop++)
   {
      // std::cout << e[loop] << ' ';
      std::cout << *(e + loop) << ' ';
   }
   std::cout << "\n\n";

   // pointers can point to 2 dimensional arrays
   int f[3][3] = { { 3, 6, 9 }, { 2, 4, 6 }, { 1, 2, 3 } };
   int(*g)[3]  = f;

   for (size_t row = 0; row < 3; row++)
   {
      for (size_t col = 0; col < 3; col++)
      {
         // std::cout << g[row][col] << ' ';
         std::cout << *(*(g + row) + col) << ' ';
      }
      std::cout << '\n';
   }
   
   // you can deference to the first element of a 2d array
   std::cout << "\n**g: " << **g << "\n\n";

   // creating a 2d array on the heap requires a bit more work
   int** h = new int*[3];
   for (size_t row = 0; row < 3; row++)
   {
      h[row] = new int[3];
   }

   // the values in the 2d array are garbage
   for (size_t row = 0; row < 3; row++)
   {
      for (size_t col = 0; col < 3; col++)
      {
         std::cout << *(*(h + row) + col) << ' ';
      }
      std::cout << '\n';
   }

   // delete memory when done
   delete c;
   delete[] h;
}

a:  5   &a: 0079FDF0
*b: 5   b:  0079FDF0    &b: 0079FDF4

*c: 20  c:  00BADA78    &c: 0079FDEC

1 2 3 4 5

3 6 9
2 4 6
1 2 3

**g: 3

6e006f 43003b 5c003a
720065 650076 5c0072
6c006f 5c0073 690042
Topic archived. No new replies allowed.