2
int main(){

    char * ch = "hi";
    char ** chh = &ch;
    chh[100] = "hh";
}

What is chh[100] = "hh" doing?

Where is the address of "hh" being stored?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
rog
  • 93
  • 9
  • what happens on doing ch++? – rog Aug 25 '20 at 17:55
  • 3
    Since `chh` points at a single `char*` and not an array, assigning to `chh[100]` results in undefined behavior. There is nothing meaningful that can be said about it. – Pete Becker Aug 25 '20 at 17:57
  • 1
    A quick note about Undefined Behaviour: The behaviour is undefined. A useless-looking, but 100% correct statement. The C++ Standard doesn't say what will happen and as a result the compiler implementors are allowed to do anything they want. Normally they go with the path of least resistance and do the exact same thing they would do if the code was valid and let data be written into invalid memory and let the CPU do with it what it will (which could be anything), but the implementors could build in code to detect and warn you of the mistake. Technically, they could also make it rain unicorns. – user4581301 Aug 25 '20 at 18:11
  • 1
    What you are asking for is not "double char pointer", but rather: a pointer on a pointer... – ipavlu Aug 25 '20 at 18:35

3 Answers3

4

"hi" and "hh" are string literals, both of type const char[3], which decay into const char * pointers to their 1st char elements. The arrays are stored in the program's read-only static memory at compile-time.

At runtime, the address of "hi" in that static memory is being stored in the ch pointer (which is illegal in C++11 and later, since a non-const char* pointer can't point to const char data), then the address of ch is being stored in the chh pointer.

The statement chh[100]="hh" is undefined behavior. chh is pointing at ch, which is stored on the stack of the calling thread. chh is not a pointer to an array of 101+ char* pointers, so indexing into the 100th slot of chh is illegal (only index 0 is valid, as all variables can be treated as an array of 1 element). This code is reaching into stack memory that you don't own, and writing the address of "hh" into the 100th slot of chh is corrupting random memory on that stack (if it doesn't crash outright with an AccessViolation/SegFault).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
3

The expression:

char* ch = "hi";

Is ill-formed because C++ forbids assigning string literals to char*, the correct statement must be:

const char* ch = "hi";

The same goes for:

 const char ** chh = &ch;

Both ch and chh must be const qualified.

What is chh[100]="hh" doing?

Nothing it should be doing, chh[100] is a pointer to memory that does not belong to chh, this leads to undefined behavior.

Where is the address of hh being stored?

"hh" is a string literal and is normally stored in read-only memory, that is the reason why C++ forbids its assignment to a non-const pointer, as atempts to change it will lead to, you guessed it, undefined behavior.

Don't confuse this with assignment to a char array, that is legal because a copy of a string literal is assigned instead.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

it tries to put a pointer to "hh" array on the stack at the location where chh+100 is pointing. you should never do that because you have no idea what will be happening (maybe this memory is used by an other bit of your code. you may trigger a segfault or a buffer overflow.

laenNoCode
  • 244
  • 2
  • 8
  • 2
    This won't trigger a stack overflow. A stack overflow is when you place too much data on the stack. This is a case of not placing enough data on the stack. – user4581301 Aug 25 '20 at 18:06
  • i have to disagree. there is only one char* pointer allocated and he tries to write on the 100th element. it will stackoverflow – laenNoCode Aug 27 '20 at 13:18
  • This is an example of a [buffer overflow](https://en.wikipedia.org/wiki/Buffer_overflow). The buffer happens to be on the stack, but the stack would have to be uncommonly small to to be overflowed by a pair of `char` pointers. – user4581301 Aug 27 '20 at 15:34