From: Steffen Dettmer <steffen.dettmer@googlemail.com>
To: gdb@sourceware.org
Subject: Re: printing array in function
Date: Fri, 03 Sep 2010 10:24:00 -0000 [thread overview]
Message-ID: <AANLkTik9TFx-WaWgCg7=4DJn5wYAH5P0ojXwANLKUfh8@mail.gmail.com> (raw)
In-Reply-To: <4C80BA7A.3030309@lip6.fr>
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
next prev parent reply other threads:[~2010-09-03 10:24 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-03 9:06 Nicolas Sabouret
2010-09-03 9:16 ` Jan Kratochvil
2010-09-03 9:38 ` Nicolas Sabouret
2010-09-03 10:24 ` Steffen Dettmer [this message]
2010-09-03 16:21 ` Andreas Schwab
2010-09-03 19:18 ` Nicolas Sabouret
2010-09-03 23:10 ` Joachim Protze
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='AANLkTik9TFx-WaWgCg7=4DJn5wYAH5P0ojXwANLKUfh8@mail.gmail.com' \
--to=steffen.dettmer@googlemail.com \
--cc=gdb@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox