From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25884 invoked by alias); 3 Sep 2010 10:24:36 -0000 Received: (qmail 25870 invoked by uid 22791); 3 Sep 2010 10:24:35 -0000 X-SWARE-Spam-Status: No, hits=-1.7 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,TW_BF,TW_XB,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from mail-qw0-f41.google.com (HELO mail-qw0-f41.google.com) (209.85.216.41) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 03 Sep 2010 10:24:02 +0000 Received: by qwf7 with SMTP id 7so2133805qwf.0 for ; Fri, 03 Sep 2010 03:24:00 -0700 (PDT) MIME-Version: 1.0 Received: by 10.224.66.3 with SMTP id l3mr897378qai.0.1283509440679; Fri, 03 Sep 2010 03:24:00 -0700 (PDT) Received: by 10.229.226.17 with HTTP; Fri, 3 Sep 2010 03:24:00 -0700 (PDT) In-Reply-To: <4C80BA7A.3030309@lip6.fr> References: <4C80BA7A.3030309@lip6.fr> Date: Fri, 03 Sep 2010 10:24:00 -0000 Message-ID: Subject: Re: printing array in function From: Steffen Dettmer To: gdb@sourceware.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2010-09/txt/msg00012.txt.bz2 On Fri, Sep 3, 2010, Nicolas Sabouret wrote: > The problem we have is that arrays passed to functions are seen as > pointers by gdb. Here is a simple example : > > 1: =A0void f(int tab[]) { > 5: =A0 =A0int t[] =3D {-1,-1}; > > (gdb) p tab =A0 =A0 -> (int *) 0xbffff440 I think this is absolutely correct. tab is an int pointer, the storage size of a referenced "array" isn't known. > The only solution we found to display tab as an array is to use "p > *tab@2", but this requires knowing the exact size of the array (2 in > this example). Yes, absolutely, same as in C: ------------------------------------------------------------------->8=3D=3D= =3D=3D=3D=3D=3D #include void f(int tab[]) { tab[0] =3D 1; /* (%z is not known to all compilers) */ printf("sizeof(tab) =3D %lu\n", (unsigned long)sizeof(tab)); } int main() { int t[] =3D {-1,-1}; printf("sizeof(t) =3D %lu\n", (unsigned long)sizeof(t)); f(t); return 0; } =3D=3D=3D=3D=3D=3D=3D8<----------------------------------------------------= --------------- produces sizeof(t) =3D 8 sizeof(tab) =3D 4 so function f() also needs to know the size or number of elements, even when a "size" is given: void f(int tab[2]) { assert(sizeof(tab) =3D=3D sizeof(int*)); } to illustrate this, development rules may forbid array notation for function notation instead of pointer, leading to: void f(int *tab) { tab[0] =3D 1; } which, as far as I know, the same as above and should be portable. > Our problem is that the gdb calls are integrated in a front-end for > students (they do not type gdb commands directly) and that our frontend > has no way of "guessing" what is the correct size for the array. maybe by passing a size parameter (which is usually needed anyway to be able to know the size of the array): void f(int *tab, size_t tab_size) { tab[0] =3D 1; } { int t[] =3D {-1,-1}; f(t, sizeof(t)/sizeof(t[0])); } and using tab_size in the frontend? oki, Steffen