Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

going crazy using sizeof. what could be wrong? 1

Status
Not open for further replies.

subra

Programmer
Aug 6, 2000
45
IN
i am using DOS and Turbo-c 3.0

I have two small programs, i. to create a data file
ii. to read and index the data file.

the structure of the data file and the offending code is as below.

typedef struc
{
char custno[7];
char custnm[31];
char amtnum[3];
} CUSTOMER;

CUSTOMER cust;

void main()
{
int csize;
csize = sizeof(cust);
}

i have included the necessary files and the program compiles without error. in the data base creation programme, the value of csize is shown correctly as 41, while in the indexing programme, the value of csize is shown wrongly as 42. why?

please help.

subra if you find this useful let me know at vksubra@icenet.net
 
I think it is a padding issue. I am not sure though because all that is in the structs are characters. I tested it quickly on my compiler and got 41 with sizeof so I assume it is a compiler issue. I think your comilier pads to a 16 bit boundry instead of an 8 bit boundry. I dont know if you can set this in the options for the compilier or not.

Matt
 
Thanks Matt for the response.

This may not be due to a compiler problem. As I mentioned earlier, the same struc and sizeof in another program returns the correct values. Not only that, I have another struct in the same "problem program". The sizeof returns the correct value on one of these structures. Incidentally, further investigations show that if I ask for sizeof of the individual elements of the structure, sizeof returns the correct values. Further if i // the first element char custno[7] and ask for the struc then the sizeof of the rest of the struc is also returned correctly. sizeof(cust.custno) is also returned correctly.

This is crazy.

Subra. if you find this useful let me know at vksubra@icenet.net
 
Hello Matt,

Just in case, - I checked my compiler settings. I think you are correct. With the compiler options/ Word alignment set to on, the sizeof returns 42, with the option set to off, the compiler returns 41. You are right. Wow!!

Thanks a lot.

Subra. if you find this useful let me know at vksubra@icenet.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top