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

D

David Thorp

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

Nils Petter Vaskinn

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
 
A

Al Bowers

David said:
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.
 
?

=?ISO-8859-1?Q?Johan_Aur=E9r?=

#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]).
 
J

j

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top