C99 Variable Length Array Max sizes and sizeof Function

后端 未结 1 732
故里飘歌
故里飘歌 2021-01-25 01:39

I am experimenting with the use of Variable Length Arrays (VLAs) in my C code and trying to iron out my understanding of what they should and shouldn\'t do.

I have the f

1条回答
  •  伪装坚强ぢ
    2021-01-25 02:24

    The problem you're running into is a bug (or perhaps more accurately a missing feature) in gdb. gdb does not correctly handle the sizeof operator applied to a VLA (variable-length array).

    This message from a gdb mailing list indicates that support for sizeof on VLAs has been implemented in gdb, but only relatively recently. Apparently it isn't in the version you and I are both using (gdb 7.7.1). Without that fix, it incorrectly prints the size of a VLA as 0. Your code itself should behave correctly; it's just gdb that isn't handling it properly.

    There's nothing particularly wrong with your code, as long as (a) it's compiled with a compiler that supports VLAs, and (b) the size of the array is positive and not too large. (VLAs are not supported in C90, except perhaps as an extension, were introduced as a standard feature in C99, and were made optional in C11.)

    A possible workaround is to modify your program to save the value of sizeof vla to a variable that you can then print from gdb.

    Another problem with gdb is that printing the VLA object itself behaves differently than printing a fixed-size array object. It apparently treats a VLA as a pointer to its first element rather than as an array object.

    Here's a gdb transcript that illustrates the problem:

    GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
    [SNIP]
    (gdb) list
    1       #include 
    2       #include 
    3       int main(void) {
    4           int len = 6;
    5           char vla[len];
    6           const size_t vla_size = sizeof vla;
    7           char arr[6];
    8           strcpy(vla, "hello");
    9           strcpy(arr, "world");
    10      }
    (gdb) break 10
    Breakpoint 1 at 0x400600: file c.c, line 10.
    (gdb) run
    Starting program: /home/kst/c 
    
    Breakpoint 1, main () at c.c:10
    10      }
    (gdb) print sizeof vla
    $1 = 0
    (gdb) print vla_size
    $2 = 6
    (gdb) print sizeof arr
    $3 = 6
    (gdb) print vla
    $4 = 0x7fffffffdc10 "hello"
    (gdb) print arr
    $5 = "world"
    (gdb) print arr+0
    $6 = 0x7fffffffdc40 "world"
    (gdb) continue
    Continuing.
    [Inferior 1 (process 28430) exited normally]
    (gdb) quit
    

    Is 12008 chars too large for a VLA?

    Probably not. For most implementations, a VLA can be just as large as a fixed-size array. There's no real difference (in terms of memory allocation) between:

    {
        int size = 12008;
        char buf[size];
    }
    

    and

    {
        int buf[12008];
    }
    

    Many systems limit the amount of memory you can allocate on the stack, but a 12008-byte array isn't likely to push those limits.

    Still, if you're going to be allocating large arrays, it's probably better to do so via malloc() (which means you'll need to explicitly call free() for each allocated object).

    0 讨论(0)
提交回复
热议问题