Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22272

Are gcc's dynamically-sized arrays effectively identical to standard arrays after declaration?

$
0
0

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?

I tried this simple code:

#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


Viewing all articles
Browse latest Browse all 22272

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>