Re: Printing a 2D array in a C program

Jan Kratochvil
2016-03-04T17:49:06Z

[-- Attachment #1: Type: text/plain, Size: 2173 bytes --]

On Fri, 04 Mar 2016 17:15:26 +0100, Neven Sajko wrote:
> But in p `print *m` just gets me {a11, ..., a1n}.

I think it is a bug in GCC. I haven't found it in GCC Bugzilla.
I haven't filed it anywhere yet. All these compilers fail the same way:
gcc-5.3.1-2.fc23.x86_64
gcc-6.0.0-0.13.fc25.x86_64
gcc (GCC) 6.0.0 20160213 (experimental)
clang-3.7.0-4.fc23.x86_64
clang-3.8.0-0.3.fc25.x86_64

The .s diff is for: gcc -o matrix.s matrix.c -Wall -g -S -dA

Jan

The array variable:
(gdb) s
f (n=1) at matrix2.c:22
(gdb) ptype m
type = int [17][17]
(gdb) p m
$1 = {{-134241616, 32767, -134252848, 32767, -140382932, 32767, 2224, 0, -134252032, 32767, -136422399, 32767, 2224, 0, -140329216,
32767, -134252112}, {32767, -134252032, 32767, -11384, 32767, -11388, 32767, 1, 0, -136460380, 32767, -140329216, 32767,
[...]
-10400, 32767, -134225592, 32767, 0, 0, 1, 0, 4195965, 0, 4195328, 0, 0, 0, 4195888}}

The bug with the parameter:
(gdb) s
p (m=0x7fffffffd2a0, n=1) at matrix2.c:9
(gdb) ptype m
type = int (*)[17]
(gdb) p m
$2 = (int (*)[17]) 0x7fffffffd2a0
(gdb) p *m
$3 = {-134241616, 32767, -134252848, 32767, -140382932, 32767, 2224, 0, -134252032, 32767, -136422399, 32767, 2224, 0, -140329216,
32767, -134252112}

Workaroundable by:
(gdb) p *(int[17][17] *)m
$4 = {{-134241616, 32767, -134252848, 32767, -140382932, 32767, 2224, 0, -134252032, 32767, -136422399, 32767, 2224, 0, -140329216,
32767, -134252112}, {32767, -134252032, 32767, -11384, 32767, -11388, 32767, 1, 0, -136460380, 32767, -140329216, 32767,
[...]
-10400, 32767, -134225592, 32767, 0, 0, 1, 0, 4195965, 0, 4195328, 0, 0, 0, 4195888}}

After fixed DWARF without the workaround:
(gdb) s
p (m=0x7fffffffd2a0, n=1) at matrix2.c:9
(gdb) ptype m
type = int (*)[17][17]
(gdb) p m
$1 = (int (*)[17][17]) 0x7fffffffd2a0
(gdb) p *m
$2 = {{-134241616, 32767, -134252848, 32767, -140382932, 32767, 2224, 0, -134252032, 32767, -136422399, 32767, 2224, 0, -140329216,
32767, -134252112}, {32767, -134252032, 32767, -11384, 32767, -11388, 32767, 1, 0, -136460380, 32767, -140329216, 32767,
[...]
-10400, 32767, -134225592, 32767, 0, 0, 1, 0, 4195965, 0, 4195328, 0, 0, 0, 4195888}}

[-- Attachment #2: Fix of the DWARF --]
[-- Type: text/plain, Size: 403 bytes --]

--- matrix2.s-orig 2016-03-04 18:30:45.846203946 +0100
+++ matrix2.s 2016-03-04 18:33:33.912409159 +0100
@@ -329,7 +329,7 @@ main:
.byte 0 # end of children of DIE 0xf1
.uleb128 0xc # (DIE (0x101) DW_TAG_pointer_type)
.byte 0x8 # DW_AT_byte_size
- .long 0xf1 # DW_AT_type
+ .long 0x13d # DW_AT_type
.uleb128 0xd # (DIE (0x107) DW_TAG_subprogram)
# DW_AT_external
.ascii "f\0" # DW_AT_name

[-- Attachment #3: Effect of the DWARF fix --]
[-- Type: text/plain, Size: 1488 bytes --]

<2><ae>: Abbrev Number: 7 (DW_TAG_formal_parameter)
<af> DW_AT_name : m
<b1> DW_AT_decl_file : 1
<b2> DW_AT_decl_line : 7
<b3> DW_AT_type : <0x101>
<b7> DW_AT_location : 2 byte block: 91 58 (DW_OP_fbreg: -40)
<1><f1>: Abbrev Number: 10 (DW_TAG_array_type)
<f2> DW_AT_type : <0x34>
<f6> DW_AT_sibling : <0x101>
<2><fa>: Abbrev Number: 11 (DW_TAG_subrange_type)
<fb> DW_AT_type : <0x65>
<ff> DW_AT_upper_bound : 16
<2><100>: Abbrev Number: 0
<1><101>: Abbrev Number: 12 (DW_TAG_pointer_type)
<102> DW_AT_byte_size : 8
- <103> DW_AT_type : <0xf1>
+ <103> DW_AT_type : <0x13d>
<1><107>: Abbrev Number: 13 (DW_TAG_subprogram)
<2><12f>: Abbrev Number: 8 (DW_TAG_variable)
<130> DW_AT_name : m
<132> DW_AT_decl_file : 1
<133> DW_AT_decl_line : 18
<134> DW_AT_type : <0x13d>
<138> DW_AT_location : 3 byte block: 91 e0 76 (DW_OP_fbreg: -1184)
<2><13c>: Abbrev Number: 0
<1><13d>: Abbrev Number: 10 (DW_TAG_array_type)
<13e> DW_AT_type : <0x34>
<142> DW_AT_sibling : <0x153>
<2><146>: Abbrev Number: 11 (DW_TAG_subrange_type)
<147> DW_AT_type : <0x65>
<14b> DW_AT_upper_bound : 16
<2><14c>: Abbrev Number: 11 (DW_TAG_subrange_type)
<14d> DW_AT_type : <0x65>