473,379 Members | 1,283 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,379 software developers and data experts.

will this code work?

hi

will the following code work.
typedef struct
{
unsigned char date[64];
unsigned char sortie_num[64];
unsigned int start_address[64];
unsigned short offset[64];
} sortie_data;

unsigned short table_buff[256];
sortie_data *sort_temp = (sortie_data *)table_buff ; // pointer to
structure containing table

read(table_sector,table_buff); // read the required sector from the
flash disk

sort_temp ->date[sector_position] = date1;
sort_temp ->sortie_num[sector_position] = sortie_number2;
sort_temp ->start_address[sector_position] = secnum;
sort_temp ->offset[sector_position] = offset;

// write the table back in flash
write(table_sector,table_buff);

read and write are some functions which take a input of unsigned short
table_buff[256], table_sector is some location of the same size of
table_buff[256]. but here the the data is written and read as unsigned
short. the values read back are modified as above.

regards
Anil

Nov 14 '05 #1
3 1254
Anil <an****@gmail.com> wrote:
will the following code work.
typedef struct
{
unsigned char date[64];
unsigned char sortie_num[64];
unsigned int start_address[64];
unsigned short offset[64];
} sortie_data; unsigned short table_buff[256];
sortie_data *sort_temp = (sortie_data *)table_buff ; // pointer to
structure containing table
It will probably work on a lot of systems, but you can never be sure.
The compiler can insert as many padding bytes into a structure as it
likes to, so sizeof *sort_temp can be larger than sizeof table_buff.
That's not a problem in itself, as long as you don't _read_ in more
than sizeof table_buff bytes, but...
read(table_sector,table_buff); // read the required sector from the
flash disk
Be careful with a function name like read(), on many systems there
already is a system-specific read() function (which typically takes
three arguments).
sort_temp ->date[sector_position] = date1;
sort_temp ->sortie_num[sector_position] = sortie_number2;
sort_temp ->start_address[sector_position] = secnum;
sort_temp ->offset[sector_position] = offset;
Now things get dangerous. If the compiler inserted padding bytes
then the places addressed above may not be where you expect them
to be and you may be writing past the end of the memory you have
(and in the wrong places).
// write the table back in flash
write(table_sector,table_buff);
The same caveat as about the read() function applies also for
write()....
read and write are some functions which take a input of unsigned short
table_buff[256], table_sector is some location of the same size of
table_buff[256]. but here the the data is written and read as unsigned
short. the values read back are modified as above.


To summarize: there's no guarantee that e.g. the 'sortie_num' array
in the 'sortie_data' structure is at the same address as table_buff[64].
And if it isn't your scheme won't work. While you have a good chance
that it works on most systems I wouldn't like to place bets on that if
important data might get lost.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Je***********@physik.fu-berlin.de wrote:
Anil <an****@gmail.com> wrote:
will the following code work.
typedef struct
{
unsigned char date[64];
unsigned char sortie_num[64];
unsigned int start_address[64];
unsigned short offset[64];
} sortie_data;

unsigned short table_buff[256];
sortie_data *sort_temp = (sortie_data *)table_buff ; // pointer to
structure containing table


It will probably work on a lot of systems, but you can never be sure.
The compiler can insert as many padding bytes into a structure as it
likes to, so sizeof *sort_temp can be larger than sizeof table_buff.


In theory, there needn't even be any padding. Granted, systems where
CHAR_BIT==16, sizeof(short)==1, and sizeof(int)>1 are rare, probably
nonexistent, but still...

Richard
Nov 14 '05 #3
On 11 Mar 2005 01:05:07 -0800, "Anil" <an****@gmail.com> wrote:
hi

will the following code work.
typedef struct
{
unsigned char date[64];
unsigned char sortie_num[64];
unsigned int start_address[64];
unsigned short offset[64];
} sortie_data;

unsigned short table_buff[256];
sortie_data *sort_temp = (sortie_data *)table_buff ; // pointer to
structure containing table
You have no clue if table_buff is properly aligned so that its address
is suitable to be treated as the address of a struct.

read(table_sector,table_buff); // read the required sector from the
flash disk

sort_temp ->date[sector_position] = date1;
sort_temp ->sortie_num[sector_position] = sortie_number2;
sort_temp ->start_address[sector_position] = secnum;
sort_temp ->offset[sector_position] = offset;

// write the table back in flash
write(table_sector,table_buff);

read and write are some functions which take a input of unsigned short
table_buff[256], table_sector is some location of the same size of
table_buff[256]. but here the the data is written and read as unsigned
short. the values read back are modified as above.

regards
Anil


<<Remove the del for email>>
Nov 14 '05 #4

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

Similar topics

4
by: Nomen Nescio | last post by:
can anyone be so kind as to look at http://www.mysolution.ws/HYPOCRITE.php and let me know why it isn't passing the form data to http://www.mysolution.ws/insertHYPOCRITES.php for the most...
34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
23
by: Antoon Pardon | last post by:
I have had a look at the signal module and the example and came to the conclusion that the example wont work if you try to do this in a thread. So is there a chance similar code will work in a...
7
by: John | last post by:
Hi: I want all objects of a class share the same set of data, which will be input from a file when the code start to run. In my design, I use static data member. The definition of the class is...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
6
by: Geir Baardsen | last post by:
Hi! This is a routine for copying a recordset into a new order. It has worked fine under Win98. However, my client has changed to Win XP, and suddenly it doesn't work anymore. I keep getting the...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
7
by: Peter Steele | last post by:
I have code to add a domain user to a local group but I'm not sure if it will work with NT domains or whether it will only work with Active Directory based systems. Here's the code: public void...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.