Declarations of structs, and arrays seem to be overlapping in memory..?!

Discussion in 'C Programming' started by David Thorp, Sep 1, 2003.

  1. David Thorp

    David Thorp Guest

    New to this list (first post), and relatively new to C, so hi
    everyone...

    If anyone can help me with this I'll be most grateful... The following
    code is from a rather elaborate (for me) program I'm writing (hence
    all the meaningles [to you no doubt] variable names and so on), but
    I've narrowed the problem down to these snippets. Just to make sure,
    I created this code alone (new file separate to my larger project) and
    the problems are still exhibiting themselves. Here's the code:

    -------------------------------------------------
    #include <stdio.h>


    typedef struct DateInts {
    int day, month, year;
    } date_ints;

    typedef struct InputParameters {
    date_ints dateStart, dateEnd, dateToPredict;
    } input_parameters;

    int main (int argc, const char * argv[]) {

    input_parameters parameters;

    printf ("parameters.dateStart = %X, %X, %X\n",
    &(parameters.dateStart.day),
    &(parameters.dateStart.month),
    &(parameters.dateStart.year)
    );
    printf ("parameters.dateEnd = %X, %X, %X\n",
    &(parameters.dateEnd.day),
    &(parameters.dateEnd.month),
    &(parameters.dateEnd.year)
    );
    printf ("parameters.dateToPredict = %X, %X, %X\n",
    &(parameters.dateStart.day),
    &(parameters.dateStart.month),
    &(parameters.dateStart.year)
    );

    int weekCount = 6;//CountWeeks (parameters);
    printf ("weekCount = %d\n", weekCount);
    date_ints weekDates[2][weekCount];

    int symbolCountCompare;

    printf ("symbolCountCompare = %d @ %X\n",
    symbolCountCompare,
    &symbolCountCompare);
    printf ("&weekDates[2][5] = %X, %X, %X\n",
    &(weekDates[2][5].day),
    &(weekDates[2][5].month),
    &(weekDates[2][5].year));

    return 0;
    }
    -------------------------------------

    I hope that's come out ok.

    I've put in all the printf statements to illustrate that certain
    variables are overlapping in memory. It seems to be consistent in
    this code to the code in my bigger project, even though that's got a
    lot of other stuff going on as well.

    Note I haven't initialized most of them, but please note that when I
    do it doesn't change anything. As I understand it the memory is
    allocated at declaration time, so initialization should make no
    difference I believe.

    The above code exhibits two problems (at least on my machine - a
    PowerMac G4, dual 1.25GHz, Mac OS 10.2.6, Apple's Project Builder 2.1
    or the built in unix Terminal):

    1. the memory location of symbolCountCompare and weekDates[2][5].month
    are both the same.
    2. the memory location of each of the members of the
    paramaters.dateStart and parameters.dateToPredict are the same.

    I accept it's possible that this may be different on other machines,
    so would any of you be willing to compile this on your machines and
    let me know if you get different or the same results?

    I just can't for the life of me figure out why this would be
    happening. Sure this is perhaps complex data structures, but at the
    end of the day, most of the above code is just variable declarations.
    Why on earth are they overlapping?

    I'm wondering if I've misunderstood how structs and arrays handle
    memory. I admit I don't have a very good grasp of pointers and such -
    I tend to try to avoid them as much as I can. So if the answer to all
    this is really obvious and I've missed it, I apologise.

    Regards,
    David.
    David Thorp, Sep 1, 2003
    #1
    1. Advertising

  2. On Sun, 31 Aug 2003 23:21:51 -0700, David Thorp wrote:


    We'll just ignore the second dimension of your array for now.

    > date_ints weekDates[2][weekCount];


    Here you're creating an array with 2 elements.

    >
    > int symbolCountCompare;
    >
    > printf ("symbolCountCompare = %d @ %X\n",
    > symbolCountCompare,
    > &symbolCountCompare);
    > printf ("&weekDates[2][5] = %X, %X, %X\n",
    > &(weekDates[2][5].day),
    > &(weekDates[2][5].month),
    > &(weekDates[2][5].year));


    Here you are reading the address of the third element of the array. Wich
    doesn't exist. (you're getting an address past the end of the array.)


    Remember in C counting starts at zero.

    int array[2];

    array[0] = 0; /* valid */
    array[1] = 1; /* valid */
    array[2] = 2; /* undefined behavior */

    Undifined behavior means trying to write 2 to a memory location past the
    end of the array. This will often result in your program crashing, but if
    that memory belongs to your program and is used for something else, you
    just overwrote the value there with 2 and may experience strange behavour,
    or the program may crash at a completely different part wich is very hard
    to debug.

    Simple rule: Always make sure you stay inside the boundries of your arrays
    and it will save you hours of debugging.


    hth
    NPV
    Nils Petter Vaskinn, Sep 1, 2003
    #2
    1. Advertising

  3. David Thorp

    Al Bowers Guest

    Re: Declarations of structs, and arrays seem to be overlapping inmemory..?!

    David Thorp wrote:
    > New to this list (first post), and relatively new to C, so hi
    > everyone...
    >
    > If anyone can help me with this I'll be most grateful... The following
    > code is from a rather elaborate (for me) program I'm writing (hence
    > all the meaningles [to you no doubt] variable names and so on), but
    > I've narrowed the problem down to these snippets. Just to make sure,
    > I created this code alone (new file separate to my larger project) and
    > the problems are still exhibiting themselves. Here's the code:
    >
    > -------------------------------------------------
    > #include <stdio.h>
    >
    >
    > typedef struct DateInts {
    > int day, month, year;
    > } date_ints;
    >
    > typedef struct InputParameters {
    > date_ints dateStart, dateEnd, dateToPredict;
    > } input_parameters;
    >
    > int main (int argc, const char * argv[]) {
    >


    ......snip....

    > date_ints weekDates[2][weekCount];
    >
    > int symbolCountCompare;
    >
    > printf ("symbolCountCompare = %d @ %X\n",
    > symbolCountCompare,
    > &symbolCountCompare);
    > printf ("&weekDates[2][5] = %X, %X, %X\n",
    > &(weekDates[2][5].day),
    > &(weekDates[2][5].month),
    > &(weekDates[2][5].year));
    >
    > return 0;
    > }


    ....snip...

    >
    > The above code exhibits two problems (at least on my machine - a
    > PowerMac G4, dual 1.25GHz, Mac OS 10.2.6, Apple's Project Builder 2.1
    > or the built in unix Terminal):
    >
    > 1. the memory location of symbolCountCompare and weekDates[2][5].month
    > are both the same.


    Forgive me if i have missed something.
    It appears that you have declared weekdays as follows:

    date_ints weekDates[2][weekCount];

    Since weekCount is 6 in your code, this is equivalent to

    date_ints weekDates[2][6];

    You then try to output the address of &(weekDates[2][5].month, etc.
    The last element of array would be weekDates[1][5]. weekDates[2][5]
    would be beyond your declared array.

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
    Al Bowers, Sep 1, 2003
    #3
  4. Re: Declarations of structs, and arrays seem to be overlapping inmemory..?!

    On 31 Aug 2003, David Thorp wrote:

    > #include <stdio.h>
    >
    >
    > typedef struct DateInts {
    > int day, month, year;
    > } date_ints;
    >
    > typedef struct InputParameters {
    > date_ints dateStart, dateEnd, dateToPredict;
    > } input_parameters;
    >
    > int main (int argc, const char * argv[]) {
    >
    > input_parameters parameters;
    >
    > printf ("parameters.dateStart = %X, %X, %X\n",
    > &(parameters.dateStart.day),
    > &(parameters.dateStart.month),
    > &(parameters.dateStart.year)
    > );
    > printf ("parameters.dateEnd = %X, %X, %X\n",
    > &(parameters.dateEnd.day),
    > &(parameters.dateEnd.month),
    > &(parameters.dateEnd.year)
    > );
    > printf ("parameters.dateToPredict = %X, %X, %X\n",
    > &(parameters.dateStart.day),
    > &(parameters.dateStart.month),
    > &(parameters.dateStart.year)


    Try

    &(parameters.dateToPredict.day),
    &(parameters.dateToPredict.month),
    &(parameters.dateToPredict.year)

    instead.

    > );
    >
    > int weekCount = 6;//CountWeeks (parameters);
    > printf ("weekCount = %d\n", weekCount);
    > date_ints weekDates[2][weekCount];
    >
    > int symbolCountCompare;
    >
    > printf ("symbolCountCompare = %d @ %X\n",
    > symbolCountCompare,
    > &symbolCountCompare);
    > printf ("&weekDates[2][5] = %X, %X, %X\n",
    > &(weekDates[2][5].day),
    > &(weekDates[2][5].month),
    > &(weekDates[2][5].year));


    The array weekDates has *two* elements (weekDates[0] and weekDates[1]).

    --
    =?ISO-8859-1?Q?Johan_Aur=E9r?=, Sep 1, 2003
    #4
  5. David Thorp

    j Guest

    (David Thorp) wrote in message news:<>...
    > New to this list (first post), and relatively new to C, so hi
    > everyone...
    >
    > If anyone can help me with this I'll be most grateful... The following
    > code is from a rather elaborate (for me) program I'm writing (hence
    > all the meaningles [to you no doubt] variable names and so on), but
    > I've narrowed the problem down to these snippets. Just to make sure,
    > I created this code alone (new file separate to my larger project) and
    > the problems are still exhibiting themselves. Here's the code:
    >
    > -------------------------------------------------
    > #include <stdio.h>
    >
    >
    > typedef struct DateInts {
    > int day, month, year;
    > } date_ints;
    >
    > typedef struct InputParameters {
    > date_ints dateStart, dateEnd, dateToPredict;
    > } input_parameters;
    >
    > int main (int argc, const char * argv[]) {
    >
    > input_parameters parameters;
    >
    > printf ("parameters.dateStart = %X, %X, %X\n",
    > &(parameters.dateStart.day),
    > &(parameters.dateStart.month),
    > &(parameters.dateStart.year)
    > );
    > printf ("parameters.dateEnd = %X, %X, %X\n",
    > &(parameters.dateEnd.day),
    > &(parameters.dateEnd.month),
    > &(parameters.dateEnd.year)
    > );
    > printf ("parameters.dateToPredict = %X, %X, %X\n",
    > &(parameters.dateStart.day),
    > &(parameters.dateStart.month),
    > &(parameters.dateStart.year)
    > );


    This is the second time you display the memory addresses of the
    members of dateStart. Perhaps you meant to use dateToPredict here?

    >
    > int weekCount = 6;//CountWeeks (parameters);
    > printf ("weekCount = %d\n", weekCount);
    > date_ints weekDates[2][weekCount];
    >
    > int symbolCountCompare;
    >
    > printf ("symbolCountCompare = %d @ %X\n",
    > symbolCountCompare,
    > &symbolCountCompare);
    > printf ("&weekDates[2][5] = %X, %X, %X\n",
    > &(weekDates[2][5].day),
    > &(weekDates[2][5].month),
    > &(weekDates[2][5].year));
    >
    > return 0;
    > }
    > -------------------------------------
    >
    > I hope that's come out ok.
    >
    > I've put in all the printf statements to illustrate that certain
    > variables are overlapping in memory. It seems to be consistent in
    > this code to the code in my bigger project, even though that's got a
    > lot of other stuff going on as well.
    >
    > Note I haven't initialized most of them, but please note that when I
    > do it doesn't change anything. As I understand it the memory is
    > allocated at declaration time, so initialization should make no
    > difference I believe.
    >
    > The above code exhibits two problems (at least on my machine - a
    > PowerMac G4, dual 1.25GHz, Mac OS 10.2.6, Apple's Project Builder 2.1
    > or the built in unix Terminal):
    >
    > 1. the memory location of symbolCountCompare and weekDates[2][5].month
    > are both the same.
    > 2. the memory location of each of the members of the
    > paramaters.dateStart and parameters.dateToPredict are the same.
    >
    > I accept it's possible that this may be different on other machines,
    > so would any of you be willing to compile this on your machines and
    > let me know if you get different or the same results?
    >
    > I just can't for the life of me figure out why this would be
    > happening. Sure this is perhaps complex data structures, but at the
    > end of the day, most of the above code is just variable declarations.
    > Why on earth are they overlapping?
    >
    > I'm wondering if I've misunderstood how structs and arrays handle
    > memory. I admit I don't have a very good grasp of pointers and such -
    > I tend to try to avoid them as much as I can. So if the answer to all
    > this is really obvious and I've missed it, I apologise.
    >
    > Regards,
    > David.
    j, Sep 2, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    612
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    539
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    626
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,361
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    452
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page