will this code work?

A

Anil

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
 
J

Jens.Toerring

Anil said:
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
 
R

Richard Bos

Anil said:
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
 
B

Barry Schwarz

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

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top