Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Jan Kratochvil <jan.kratochvil@redhat.com>,
	Neven Sajko <nsajko@gmail.com>
Cc: gdb@sourceware.org
Subject: Re: Printing a 2D array in a C program
Date: Fri, 04 Mar 2016 18:24:00 -0000	[thread overview]
Message-ID: <56D9D2D3.4020706@redhat.com> (raw)
In-Reply-To: <20160304174859.GA15741@host1.jankratochvil.net>

On 03/04/2016 05:48 PM, Jan Kratochvil wrote:
> The bug with the parameter:
> (gdb) s
> p (m=0x7fffffffd2a0, n=1) at matrix2.c:9
> (gdb) ptype m
> type = int (*)[17]
> (gdb) p m
> $2 = (int (*)[17]) 0x7fffffffd2a0
> (gdb) p *m
> $3 = {-134241616, 32767, -134252848, 32767, -140382932, 32767, 2224, 0, -134252032, 32767, -136422399, 32767, 2224, 0, -140329216,
>    32767, -134252112}

It's a C gotcha, but I don't think it's a bug.  Essentially, a parameter
declared as an array is really treated as a pointer parameter.

From http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf:

"
6.7.5.3 Function declarators (including prototypes)
Constraints
(...)

7

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’ (...)
"

So the DWARF describes the type as really what is is.  See:

~~~~~~~~~~~~~~~~
$ cat array-param.c
#include <stdlib.h>
#include <stdio.h>

enum
{
  sz = 17
};

void
p (int m[sz][sz])
{
  printf ("m: sizeof m = %d\n", (int) sizeof (m));
}

void
f (void)
{
  int m[sz][sz];

  printf ("f: sizeof m = %d\n", (int) sizeof (m));

  p (m);
}

int
main ()
{
  f ();
  return 0;
}

$ gcc -v
gcc version 6.0.0 20160301 (experimental) (GCC)
$ gcc array-param.c -o array-param -Wall -Wextra -O2
array-param.c: In function ‘p’:
array-param.c:12:46: warning: ‘sizeof’ on array function parameter ‘m’ will return size of ‘int (*)[17]’ [-Wsizeof-array-argument]
   printf ("m: sizeof m = %d\n", (int) sizeof (m));
                                              ^
array-param.c:10:8: note: declared here
 p (int m[sz][sz])
        ^
$ ./array-param 
f: sizeof m = 1156
m: sizeof m = 8
~~~~~~~~~~~~~~~~

Thanks,
Pedro Alves


  reply	other threads:[~2016-03-04 18:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-04 14:27 Neven Sajko
2016-03-04 14:42 ` Jan Kratochvil
2016-03-04 16:15   ` Neven Sajko
2016-03-04 17:49     ` Jan Kratochvil
2016-03-04 18:24       ` Pedro Alves [this message]
2016-03-04 18:59         ` Jan Kratochvil
2016-03-04 19:16           ` Pedro Alves
2016-03-04 18:34     ` Andreas Schwab
2016-03-04 21:48       ` Neven Sajko
2016-03-04 22:16         ` Andreas Schwab
2016-03-05  0:59           ` Neven Sajko
2016-03-05  8:23             ` Andreas Schwab

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=56D9D2D3.4020706@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb@sourceware.org \
    --cc=jan.kratochvil@redhat.com \
    --cc=nsajko@gmail.com \
    /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