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