pointers to an array of pointers crash?

Hi guys

can anybody spot why this code seems to not print any of my cout statements

the program will successfully terminate without printing anything to the terminal,

it takes unusually long to terminate but it does terminate

any idea why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>

using namespace std;

int main()
{

    char** points = new char*[4];
    char b = 'b';

    cout << "this far 1" << endl;
    *points[0] = 'a';
    *points[1] = 'd';
    *points[2] = 'a';
     points[3] = &b;

    cout << "this far 2" << endl;

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

        cout << "hi" << endl;
        cout << *points[i];
    }
}
Last edited on
I am probably answering my own question here but I will leave this open in case anybody else gets benefit from it,


I think the reason it is crashing is because 'a' 'd' and 'a' and rvalues are temporary literals, so *points[0] to [3] are or could be pointing to invalid memory right?

thanks
It's certainly true that every char* in the array is pointing at random memory when you create them, and points[3] is until you set it to point at char b.

As such, you're writing over random memory, and then reading and outputting random memory. ANything could happen.

Nothing to do with 'a' and 'd' being rvalues. You're simply writing the char 'a' and the char 'd' into random memory.
Last edited on
N.B.: If you're using print statements to track control-flow through a program which is crashing, use std::cerr or std::clog instead of std::cout. std::cout writes to an internal buffer before the output device, so you might not see the message immediately after it is printed. std::cerr and std::clog, on the other hand, bypass any internal buffer and make the output visible immediately, on stderr and stdout, respectively.
Last edited on
By default, std::clog and std::wclog are buffered.

Also, unlike std::cerr and std::wcerr, by default these streams are not tied to std::cout / std::wcout.
Thanks!
Nothing to do with 'a' and 'd' being rvalues. You're simply writing the char 'a' and the char 'd' into random memory.


really? how are you writing it into random memory?

'a' 'd' and 'a' will be addresses in memory? I thought 'a' 'd' and 'a' are rvalues?

You're simply writing the char 'a' and the char 'd' into random memory.


how am I writing into random memory?
Last edited on
how are you writing it into random memory?

char** points = new char*[4]

This creates an array. The array contains four char-pointers. Each char-pointer has a value. You didn't set the value; each char-pointer's value is whatever happened to be in the memory that is now an array. Random values.

So, each of the four char-pointers is pointing into random memory.

*points[0] = 'a';

This says "the char-pointer in the first element of the array, the first char pointer, that's pointing at some random memory; write the character 'a' into that random memory."

So that's how.

Is that clear enough?


Maybe you're mixed up between "a" and 'a'.

"a" is a char-pointer, pointing at a byte of memory that contains the char 'a'. This is known as a string literal.
'a' is the char 'a'.
Last edited on
oh ok that makes sense

so what I was doing was trying to put the value 'a' into points[0]

that makes sense,I was looking at it totally wrong what I thought was

that 'a' will be an rvalue random memory address and *points[0] would equal that memory address

but now looking back I need to be careful of what I'm doing,I am using *points[0] not points[0]


thanks Repeater :)
Last edited on
Maybe you're mixed up between "a" and 'a'.

"a" is a char-pointer, pointing at a byte of memory that contains the char 'a'. This is known as a string literal.
'a' is the char 'a'.


yes exactly what I was thinking! :)
Last edited on
Topic archived. No new replies allowed.