C debug on passing array between functions

I'm writing an ANSI C program and have a bug somewhere I'm unable to find. I'm new to C so don't discount the obvious.

I have three files in use. The first is a header file I include in the other 2 files using #include "myheader.h". Inside this header file I have, among other things, a function prototype of:

double function1(double *, double *, double *, int *, int, int);

The second file contains the main() function. This includes, among other things, the declaration of variable APRON as:

int APRON[8] = {1, 2, 3, 4, 5, 6, 7, 8};

Using gdb in this file I observe:

(gdb) p APRON 
$1 = {1, 2, 3, 4, 5, 6, 7, 8}

Then this array is passed to another file (file3) as an argument in the following function call:

zz = function1(aa, bb, cc, APRON, dd, ee);

In file3, the function is defined as:

double function1(double *aa, double *bb, double *cc, int *bins, int dd, int ee)  

When gdb enters this file3, I immediately observe:

(gdb) p bins
$3 = (int *) 0x7fffffffe390

although I fooled around with the formatting here (don't really know what I'm doing) and observed:

(gdb) p bins[0]
$5 = 1
(gdb) p bins[1]
$6 = 2
(gdb) p bins[2]
$5 = 3
(gdb) p bins[3]
$6 = 4
(gdb) p bins[4]
$5 = 5
(gdb) p bins[5]
$6 = 6
(gdb) p bins[6]
$5 = 7
(gdb) p bins[7]
$6 = 8

Why did gdb change the output of this array? Also, interesting (in a bad way), I see:

(gdb) p sizeof(bins)
$36 = 8

when it should be 4*8 = 32 (as I observe when doing this in file2).

Can someone help me understand what's happening to this array when it enters file3? (why it appears different here compared to just before it's passed into file3). I should mention the other arrays (aa, bb, cc) show the same behavior, although the scalars (dd, ee) come through fine in file3.


gdb pays attention to how your variables are declared.

In the first file, APRON is declared as an array of int. So sizeof() returns the size of the whole array, and gdb's p command displays the whole array.

In the second file, bins is declared as a pointer to an int. The size of a pointer to an int is 8 bytes (assuming you're on a 64 bit machine). And asking gdb to print the value of a pointer will produce a hex value just as you observed.

However, you also observed that you can use bins as the base of an array and index it just like APRON -- so you were able to get the array element values by saying bins[0], bins[1], bins[2], etc. Everything you report sounds just as I would expect it to.

When you pass APRON to function1(), you're just passing the address of the array. function1() knows nothing about the size of the array. Either your code has to know how long the array is and not run past the end of it, or you need to include another argument to function1() indicating the number of elements in the array.

Short answer: you get two different behaviours in GHDB because you have two different declarations.

I can't even believe that int APRON = {1, 2, 3, 4, 5, 6, 7, 8}; is legal - but obviously it is as your code compiels & links (it is, however, neither intuitive nor easilly maintainable)).

Your function prototype for that parameter is int *bins.

Rather than figure out what's the difference between the two declarations, why not declare them both as int[8]?

Make a typedef int apront_t[8] and use that. It would make your life much simpler, imo

btw, if you are developing in Linux, check out DDD, where you can see things like

Need Your Help

How to create image mosaic like Lego app?

iphone ios4 lego

I'm trying to play around with turning an image into mosaic bricks like the Lego Photo app.

Exposing events from .NET to COM

.net events com interop iunknown

recently I have been encountering problems with exposing events from .NET to COM.