In standard C and C++ the address of an array is the array itself, and sizeof
returns the size of the array, not the pointer. However, does gcc
's extension of dynamically-sized arrays offer the same guarantee?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
char foo[200];
char bar[atoi(argv[1])];
char *wibble = foo;
printf("%p, %p, %lu, %p, %p, %lu, %p, %p, %lu\n",
foo, &foo, sizeof(foo), bar, &bar, sizeof(bar), wibble, &wibble, sizeof(wibble));
return 0;
}
The output is as I hoped, indicating equivalence:
0x7ffc7dd132e0, 0x7ffc7dd132e0, 200, 0x7ffc7dd131e0, 0x7ffc7dd131e0, 200, 0x7ffc7dd132e0, 0x7ffc7dd132c8, 8
However, is this equivalence guaranteed by gcc
? Are there any times where using a dynamically-sized instead of a statically-sized array will result in different program behaviour?
※ Note I'm interested in if it works, not how to write it better using std::vector
, etc