c - C99 Variable Length Array Max sizes and sizeof Function -
i experimenting use of variable length arrays (vlas) in c code , trying iron out understanding of should , shouldn't do.
i have following snippet function:
void get_pdw_frame_usb(pdws_t *pdw_frame, pdw_io_t *pdw_io) { ... unsigned char buf[pdw_io->packet_size]; unsigned char *pdw_buf; memset(buf, '\0', sizeof(buf));
pdw_io
data structure containing, amongst other things, packet_size
, of type size_t
the char array buf
used store contents of usb bulk transfer packet
i'm trying instantiate here automatic variable using c99 vla approach. i'm trying ensure contents zeros.
i'm having few issues.
firstly, if pdw_io->packet_size
set 8 (quite small), buf set reasonable looking value, i.e. debugging gdb can inspect follows:
(gdb) p buf $27 = 0xbffe5be8 "\270", <incomplete sequence \370\267>
if pdw_io->packet_size
set 12008 (fair bit larger), following doesn't good:
(gdb) p buf $29 = 0xbffe2d08 ""
is 12008 chars large vla? or perhaps gdb output not worry about, looks bit hasn't allocated me?
also when inspecting size of buf
following in both cases:
(gdb) p sizeof(buf) $30 = 0
which have expected 8 in 1st instance , 12008 in 2nd
am wrong in thinking should possible use sizeof
function in way vla?
my problem subsequent usb bulk transfer failing , want try , rule out fact may have use of vlas, bit of new area me..
update
wrote following minimal, complete , verifiable program try , confirm observations:
#include <stdio.h> void test_vla(size_t n) { unsigned char buf[n]; printf("sizeof buf = %zu\n", sizeof buf); } int main() { test_vla(12008); return 0; }
now if break on printf
statement gdb , run p sizeof buf
0 printf
outputs 12008.
gdb
version is:
(gdb) show version gnu gdb (ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
the problem you're running bug (or perhaps more accurately missing feature) in gdb
. gdb
not correctly handle sizeof
operator applied vla (variable-length array).
this message gdb mailing list indicates support sizeof
on vlas has been implemented in gdb
, relatively recently. apparently isn't in version , both using (gdb 7.7.1). without fix, incorrectly prints size of vla 0. code should behave correctly; it's gdb
isn't handling properly.
there's nothing particularly wrong code, long (a) it's compiled compiler supports vlas, , (b) size of array positive , not large. (vlas not supported in c90, except perhaps extension, introduced standard feature in c99, , made optional in c11.)
a possible workaround modify program save value of sizeof vla
variable can print gdb
.
another problem gdb
printing vla object behaves differently printing fixed-size array object. apparently treats vla pointer first element rather array object.
here's gdb
transcript illustrates problem:
gnu gdb (ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1 [snip] (gdb) list 1 #include <stdio.h> 2 #include <string.h> 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 @ 0x400600: file c.c, line 10. (gdb) run starting program: /home/kst/c breakpoint 1, main () @ 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 large vla?
probably not. implementations, vla can large 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 amount of memory can allocate on stack, 12008-byte array isn't push limits.
still, if you're going allocating large arrays, it's better via malloc()
(which means you'll need explicitly call free()
each allocated object).
Comments
Post a Comment