This is the mail archive of the gdb@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: gdb printing of dynamically allocated matrix


On Mon, 23 Jul 2007, Jim Blandy wrote:

> 
> Peter Toft <pto@linuxbog.dk> writes:
> > On Fri, 20 Jul 2007, Jim Blandy wrote:
> >
> >> 
> >> Peter Toft <pto@linuxbog.dk> 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.

Clearly my focus is dynamic memory allocation.
 
> 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.

Ok - we are now on the same (memory) page :-)
I was just a bit confused by your answer.

-- 
Peter Toft, Ph.D. [pto@linuxbog.dk] http://petertoft.dk
Følg min Linux-blog på http://www.version2.dk/blogs/petertoft

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]