From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16070 invoked by alias); 23 Jul 2007 16:59:51 -0000 Received: (qmail 16062 invoked by uid 22791); 23 Jul 2007 16:59:51 -0000 X-Spam-Check-By: sourceware.org Received: from mail.codesourcery.com (HELO mail.codesourcery.com) (65.74.133.4) by sourceware.org (qpsmtpd/0.31) with ESMTP; Mon, 23 Jul 2007 16:59:49 +0000 Received: (qmail 7192 invoked from network); 23 Jul 2007 16:59:47 -0000 Received: from unknown (HELO localhost) (jimb@127.0.0.2) by mail.codesourcery.com with ESMTPA; 23 Jul 2007 16:59:47 -0000 To: Peter Toft Cc: gdb@sourceware.org Subject: Re: gdb printing of dynamically allocated matrix References: <20070718183326.GA25345@caradoc.them.org> From: Jim Blandy Date: Mon, 23 Jul 2007 17:05:00 -0000 In-Reply-To: (Peter Toft's message of "Sat, 21 Jul 2007 00:21:56 +0200 (CEST)") Message-ID: User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-IsSubscribed: yes 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: 2007-07/txt/msg00163.txt.bz2 Peter Toft writes: > On Fri, 20 Jul 2007, Jim Blandy wrote: > >> >> Peter Toft writes: >> >> (gdb) matprint c 2 3 >> >> Could you use sizeof to compute the dimensions automatically? Or at >> least one of them? > > Dear Jim > > I think I understand which direction you are point me to, but > "sizeof" in GDB does not ring a bell here. In C yes, but in GDB no. > > Can you give me an example e.g. for 1D arrays? We might target this > snippet befor we move to 2D matrices; > > int *a; > a=(int *)malloc(3*sizeof(int)); > a[0] = 1; > a[1] = 6; > a[2] = 8; Ah, if you're allocating dynamically, then there's no way to do it using sizeof. GDB supports the C 'sizeof' operator; in your example above, 'print sizeof (a)' will give you '4' or '8' or whatever is appropriate for a pointer on the program being debugged. There are no facilities (in C or in GDB) for finding the size of a malloc block; since the library may round up the size of the block, it might be impossible to do without changing the heap data structure.