473,385 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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.
Nov 13 '05 #1
4 2172
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
Nov 13 '05 #2


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: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #3
j
li**********@lifeistolive.com (David Thorp) wrote in message news:<9e*************************@posting.google.c om>...
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.

Nov 13 '05 #4
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]).

--
au***@4x15.com
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Morten Gulbrandsen | last post by:
mysql> USE company; Database changed mysql> mysql> DROP TABLE IF EXISTS EMPLOYEE; -------------- DROP TABLE IF EXISTS EMPLOYEE -------------- Query OK, 0 rows affected (0.00 sec)
1
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
197
by: Steve Kobes | last post by:
Is this legal? Must it print 4? int a = {{1, 2}, {3, 4}}, *b = a; printf("%d\n", *(b + 3)); --Steve
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
1
by: Fuzzy via .NET 247 | last post by:
I have a struct that I would like to control the field layout on in the same manner of UNION structs in C++. This is not for passing data to unmanaged code, it is to save memory space because field...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.