473,666 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_parameter s;

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

input_parameter s parameters;

printf ("parameters.da teStart = %X, %X, %X\n",
&(parameters.da teStart.day),
&(parameters.da teStart.month),
&(parameters.da teStart.year)
);
printf ("parameters.da teEnd = %X, %X, %X\n",
&(parameters.da teEnd.day),
&(parameters.da teEnd.month),
&(parameters.da teEnd.year)
);
printf ("parameters.da teToPredict = %X, %X, %X\n",
&(parameters.da teStart.day),
&(parameters.da teStart.month),
&(parameters.da teStart.year)
);

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

int symbolCountComp are;

printf ("symbolCountCo mpare = %d @ %X\n",
symbolCountComp are,
&symbolCountCom pare);
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 symbolCountComp are and weekDates[2][5].month
are both the same.
2. the memory location of each of the members of the
paramaters.date Start and parameters.date ToPredict 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 2189
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 symbolCountComp are;

printf ("symbolCountCo mpare = %d @ %X\n",
symbolCountComp are,
&symbolCountCom pare);
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_parameter s;

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

......snip....
date_ints weekDates[2][weekCount];

int symbolCountComp are;

printf ("symbolCountCo mpare = %d @ %X\n",
symbolCountComp are,
&symbolCountCom pare);
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 symbolCountComp are 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.com base.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #3
j
li**********@li feistolive.com (David Thorp) wrote in message news:<9e******* *************** ***@posting.goo gle.com>...
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_parameter s;

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

input_parameter s parameters;

printf ("parameters.da teStart = %X, %X, %X\n",
&(parameters.da teStart.day),
&(parameters.da teStart.month),
&(parameters.da teStart.year)
);
printf ("parameters.da teEnd = %X, %X, %X\n",
&(parameters.da teEnd.day),
&(parameters.da teEnd.month),
&(parameters.da teEnd.year)
);
printf ("parameters.da teToPredict = %X, %X, %X\n",
&(parameters.da teStart.day),
&(parameters.da teStart.month),
&(parameters.da teStart.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 symbolCountComp are;

printf ("symbolCountCo mpare = %d @ %X\n",
symbolCountComp are,
&symbolCountCom pare);
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 symbolCountComp are and weekDates[2][5].month
are both the same.
2. the memory location of each of the members of the
paramaters.date Start and parameters.date ToPredict 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_parameter s;

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

input_parameter s parameters;

printf ("parameters.da teStart = %X, %X, %X\n",
&(parameters.da teStart.day),
&(parameters.da teStart.month),
&(parameters.da teStart.year)
);
printf ("parameters.da teEnd = %X, %X, %X\n",
&(parameters.da teEnd.day),
&(parameters.da teEnd.month),
&(parameters.da teEnd.year)
);
printf ("parameters.da teToPredict = %X, %X, %X\n",
&(parameters.da teStart.day),
&(parameters.da teStart.month),
&(parameters.da teStart.year)
Try

&(parameters.da teToPredict.day ),
&(parameters.da teToPredict.mon th),
&(parameters.da teToPredict.yea r)

instead.
);

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

int symbolCountComp are;

printf ("symbolCountCo mpare = %d @ %X\n",
symbolCountComp are,
&symbolCountCom pare);
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
3035
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
368
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 that contains an array of other structs. typedef struct { int nWidth; int nHeight; DWORD dwFlags; } HCA_MODE; typedef struct
11
3391
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 don't know how many points there will be in every struct in the end, so I have to allocate memory dynamically for them and can't use an array of fixed size, unfortunately. I would like to know if there is a better way to access struct members...
10
4112
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
5730
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
3123
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
2414
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 X will either be short integer or double, and other info in the struct can be used to determine if X should be interpreted as short integer or double. I have found the docs for and for . However, one doc state that these fields only control...
3
2836
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; } *arrays; It's supposed to be a structure containing an array of chars, an array of ints and an int. I declare functions like this : arrays *parseline(char *line, int N)
18
2252
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.