#2148 Improper handling of arrays


Improper handling of arrays

The compiler inserts the extra data in the array
The compiler is losing data in the array

$ sdcc --Werror -pp16f877a --opt-code-size -Ilibmain -DF_CPU=4000000 -mpic14 --use-non-free bug2.c
message: using default linker script "/usr/share/gputils/lkr/16f877a.lkr"
$ sdcc -v
SDCC : mcs51/gbz80/z80/z180/r2k/r3ka/ds390/pic16/pic14/TININative/ds400/hc08/s08 3.2.0 #8008 (Jul 6 2012) (Linux)

cat bug2.c

include <pic16f877a.h>

include <stdint.h>

const static uint8_t arr[3][4]={
{ 1, 2, 3, },
{ 4, 5, 6, },
{ 7, 8, 9, },
{ 10, 11, 12, },

void main(void) {

cat bug2.asm
; File Created by SDCC : free open source ANSI-C Compiler
; Version 3.2.0 #8008 (Jul 6 2012) (Linux)
; This file was generated Fri Mar 29 08:14:43 2013
; PIC port for the 14-bit core
; .file "bug2.c"
ID_bug2_0 code
retlw 0x01
retlw 0x02
retlw 0x03
retlw 0x00
retlw 0x04
retlw 0x05
retlw 0x06
retlw 0x00
retlw 0x07
retlw 0x08
retlw 0x09
retlw 0x00

; overlayable items in internal ram

1 Attachments


  • BsAtHome

    BsAtHome - 2013-03-29

    C-arrays are in row-major order. Your definition tells the compiler to create 3 arrays of 4 length. See https://en.wikipedia.org/wiki/Row-major_order

    Your array should read:
    const static uint8_t arr[4][3]= {...}

  • Kharitonov Dmitry

    I think that the compiler has to report data format mismatch

    • BsAtHome

      BsAtHome - 2013-03-29

      Well, by default, C just pads with zeros if no initializer is provided, so your assertion of "compiler inserts the extra data in the array" is invalid. It does what you asked it to do because you reversed the index sizes.

      The compiler is not actually losing data, but an excess of initializers is provided, which are dropped silently. This I would argue is an issue. The compiler should warn "excess initializers", just like most other do.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks