c++ - What does int **p in mean? -
i understand use when need give pointer array of pointers in dynamic memory don't understand how works in stack.
does make array of pointers in stack pointing array of pointers in heap or make single pointer in stack pointing array of pointers in heap? if yes, difference between
int **p = new int*[100]
and
int *p = new int[100]
thanks in advance. have been trying understand long time , have read lot of documentation online still don't understand this.
int **p
declares pointer on stack points pointer(s) on heap. each of pointer(s) point integer or array of integers on heap.
this:
int **p = new int*[100];
means declared pointer on stack , initialized points array of 100 pointers on heap. each of 100 pointers point nowhere. "nowhere" mean point neither valid chunk of memory, nor nullptr
s. not initialized, contain garbage value in memory before pointers allocated. should assign sensible them in loop before usage. note p[0]
- p[99]
pointers not guaranteed point adjacent regions of memory if assign return values of new
them. example, if allocate memory each of them p[i] = new int[200];
, p[0][201]
not reference p[1][2]
, lead undefined behavior.
and this:
int *p = new int[100];
is pointer on stack points array of 100 integers on heap.
Comments
Post a Comment