Actually, the question is:
Why view code:
char *r = (char *)malloc(5 * sizeof(char));
runs without errors, that is, the memory is allocated normally, but in the code:
char **r = (char **)malloc(5 * sizeof(char *));
I see in the value of the variable r the following -
0xbaadf00d <error: Cannot access memory at address 0xbaadf00d>
Why is that? For an array of characters, everything is normally allocated, but for an array of pointers there?
UPD : Similar allocation of memory for an array of pointers in another function works fine.
UPD2 : the value of r
tracked through the debugger.
r
value)?printf("%p\n",r)
? - Harrychar*
(yes, a pointer to pointers). But in those 20 bytes (5 * 4) the garbage is written. But the debugger does not yet know about them. and an attempt to read at these addresses is sad. Moreover baadfood hints. - KoVadimvoid main() { char **r = (char **)malloc(5 * sizeof(char *)); printf("%p\n",r); }
void main() { char **r = (char **)malloc(5 * sizeof(char *)); printf("%p\n",r); }
Compiles, displays something like0041E050
... What should I do to drop? - Harry