public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* printing array in function
@ 2010-09-03  9:06 Nicolas Sabouret
  2010-09-03  9:16 ` Jan Kratochvil
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Nicolas Sabouret @ 2010-09-03  9:06 UTC (permalink / raw)
  To: gdb

Hi all,

In the frame of a 1st-year course in C (starting in next october), we
are using a sub-set of gdb features (embedded in a geany plugin) to help
our students understand what is going on in their code.

The problem we have is that arrays passed to functions are seen as
pointers by gdb. Here is a simple example :

1:  void f(int tab[]) {
2:    tab[0] = 1;
3:  }
4:  int main() {
5:    int t[] = {-1,-1};
6:    f(t);
7:    return 0;
8:  }


$ gcc -g -o test test.c
$ gdb test
(gdb) b 6
(gdb) run
(gdb) p t       -> {-1, -1}
(gdb) s
(gdb) p tab     -> (int *) 0xbffff440


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).

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.

My question is : is there a way to display an array in a function
(without knowing the size a priori).

Thanks in advance for any help on this subject.
-- 
Nicolas Sabouret, Assistant Professor
University Pierre & Marie Curie
http://www-poleia.lip6.fr/~sabouret

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printing array in function
  2010-09-03  9:06 printing array in function Nicolas Sabouret
@ 2010-09-03  9:16 ` Jan Kratochvil
  2010-09-03  9:38   ` Nicolas Sabouret
  2010-09-03 10:24 ` Steffen Dettmer
  2010-09-03 16:21 ` Andreas Schwab
  2 siblings, 1 reply; 7+ messages in thread
From: Jan Kratochvil @ 2010-09-03  9:16 UTC (permalink / raw)
  To: Nicolas Sabouret; +Cc: gdb

On Fri, 03 Sep 2010 11:06:02 +0200, Nicolas Sabouret wrote:
> 1:  void f(int tab[]) {
> 2:    tab[0] = 1;
> 3:  }
> (gdb) p tab     -> (int *) 0xbffff440

I have only an idea by C++:

void f(int (&tab)[2]) {
(gdb) p tab
$1 = (int (&)[2]) @0x7fffffffde50: {-1, -1}


Regards,
Jan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printing array in function
  2010-09-03  9:16 ` Jan Kratochvil
@ 2010-09-03  9:38   ` Nicolas Sabouret
  0 siblings, 0 replies; 7+ messages in thread
From: Nicolas Sabouret @ 2010-09-03  9:38 UTC (permalink / raw)
  Cc: gdb

Dear Jan,

Le 03/09/2010 11:15, Jan Kratochvil a écrit :
> On Fri, 03 Sep 2010 11:06:02 +0200, Nicolas Sabouret wrote:
>> 1:  void f(int tab[]) {
>> 2:    tab[0] = 1;
>> 3:  }
>> (gdb) p tab     -> (int *) 0xbffff440
> 
> I have only an idea by C++:
> 
> void f(int (&tab)[2]) {
> (gdb) p tab
> $1 = (int (&)[2]) @0x7fffffffde50: {-1, -1}

Thanks for the quick answer. However, your solution requires :
1) to fix the array size in the function signature (this is something we
would prefer to avoid but why not)
2) to use a non-regular syntax in the function signature, which is
someting we cannot do (we want our students to lear the proper C-syntax).

Any other idea ?

-- 
Nicolas Sabouret
LIP6, BC169, 4 place Jussieu, 75005 Paris
http://www-poleia.lip6.fr/~sabouret

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printing array in function
  2010-09-03  9:06 printing array in function Nicolas Sabouret
  2010-09-03  9:16 ` Jan Kratochvil
@ 2010-09-03 10:24 ` Steffen Dettmer
  2010-09-03 16:21 ` Andreas Schwab
  2 siblings, 0 replies; 7+ messages in thread
From: Steffen Dettmer @ 2010-09-03 10:24 UTC (permalink / raw)
  To: gdb

On Fri, Sep 3, 2010, Nicolas Sabouret <Nicolas.Sabouret@lip6.fr> wrote:
> The problem we have is that arrays passed to functions are seen as
> pointers by gdb. Here is a simple example :
>
> 1:  void f(int tab[]) {
> 5:    int t[] = {-1,-1};
>
> (gdb) p tab     -> (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=======
   #include <stdio.h>

   void f(int tab[]) {
     tab[0] = 1;
     /* (%z is not known to all compilers) */
     printf("sizeof(tab) = %lu\n", (unsigned long)sizeof(tab));
   }
   int main() {
     int t[] = {-1,-1};
     printf("sizeof(t) = %lu\n", (unsigned long)sizeof(t));
     f(t);
     return 0;
   }
=======8<-------------------------------------------------------------------

produces

   sizeof(t) = 8
   sizeof(tab) = 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) == 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] = 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] = 1;
   }
   {
     int t[] = {-1,-1};
     f(t, sizeof(t)/sizeof(t[0]));
   }

and using tab_size in the frontend?

oki,

Steffen

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printing array in function
  2010-09-03  9:06 printing array in function Nicolas Sabouret
  2010-09-03  9:16 ` Jan Kratochvil
  2010-09-03 10:24 ` Steffen Dettmer
@ 2010-09-03 16:21 ` Andreas Schwab
  2010-09-03 19:18   ` Nicolas Sabouret
  2 siblings, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2010-09-03 16:21 UTC (permalink / raw)
  To: Nicolas Sabouret; +Cc: gdb

Nicolas Sabouret <Nicolas.Sabouret@lip6.fr> writes:

> The problem we have is that arrays passed to functions are seen as

There is no such thing as an array parameter in C.

> pointers by gdb. Here is a simple example :
>
> 1:  void f(int tab[]) {

This is the same as f(int *), see §6.7.5.3#7.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printing array in function
  2010-09-03 16:21 ` Andreas Schwab
@ 2010-09-03 19:18   ` Nicolas Sabouret
  2010-09-03 23:10     ` Joachim Protze
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Sabouret @ 2010-09-03 19:18 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: gdb

Hi,

Le 03/09/2010 18:21, Andreas Schwab a écrit :
> There is no such thing as an array parameter in C.

I know this. I was just wondering if there was any workaround in gdb. I
can understand why there is not, though.

> This is the same as f(int *), see §6.7.5.3#7.

Thanks for your answer.
I think we will have to write a specific parser... or to use other
pedagogical artifacts.

Regards,
-- 
Nicolas Sabouret
LIP6, BC169, 4 place Jussieu, 75005 Paris
http://www-poleia.lip6.fr/~sabouret

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: printing array in function
  2010-09-03 19:18   ` Nicolas Sabouret
@ 2010-09-03 23:10     ` Joachim Protze
  0 siblings, 0 replies; 7+ messages in thread
From: Joachim Protze @ 2010-09-03 23:10 UTC (permalink / raw)
  To: gdb

Hi,

Am 03.09.2010 21:18, schrieb Nicolas Sabouret:
> I think we will have to write a specific parser... or to use other
> pedagogical artifacts.

As a student in the final phase of the study of computer science i do
not see the benefit of feigning your students to wrong facts. Within the
function f there is no knowledge of the length of the array. Why do you
want to display it anyway? The only effect is to give the students a
wrong insight to C. The consequence is, that they will tend to produce
code with buffer overflows.

> (we want our students to lear the proper C-syntax)
Proper C-syntax, but wrong C-semantik ?!?

Joachim

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2010-09-03 23:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-03  9:06 printing array in function Nicolas Sabouret
2010-09-03  9:16 ` Jan Kratochvil
2010-09-03  9:38   ` Nicolas Sabouret
2010-09-03 10:24 ` Steffen Dettmer
2010-09-03 16:21 ` Andreas Schwab
2010-09-03 19:18   ` Nicolas Sabouret
2010-09-03 23:10     ` Joachim Protze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).