Thread: [Dev-C++] [Dev-C++]: Errors in structures
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jeethu R. <je...@sa...> - 2001-07-03 03:51:10
|
Hi,
I've noticed this strange little Bug.I'm not certain weather its a bug or a
feature.
The size of this structure is actually 13 bytes.
But sizeof(header) says its 16.
struct tea_hdr
{
char jte[3];
unsigned char version;
DWORD infile_size;
DWORD infile_crc;
unsigned char fname_sz;
}header;
No, if I comment out the fname_sz member,
struct tea_hdr
{
char jte[3];
unsigned char version;
DWORD infile_size;
DWORD infile_crc;
//unsigned char fname_sz;
}header;
sizeof(header) returns 12.
Is the compiler trying to align structure on DWORD boundaries?
How do I get rid of this Bug/Feature ?
Thnx,
Jeethu Rao
|
|
From: Johannes P. <joh...@pr...> - 2001-07-03 05:44:45
|
Hi there
-------------
I am sorry to say that I dont have an answer for you . The LibC help file
does say that the Data Types can vary from Compiler
to Compiler But I WOULD like to show what I have also picked up with your Code
//Snippet - START
#include <iostream.h>
#include <stdlib.h>
#include <limits.h>
struct tea_hdr{
int version; //-- 1
char jte[3]; // -- 2
//char fname_sz; // -- 3
//char infile_size; // -- 4
// char infile_crc; // -- 5
}header;
int main()
{
cout<<"Char Minimum : "<<CHAR_MIN<<endl;
cout<<"Char Max : "<<CHAR_MAX<<endl;
cout<<"Unsigned Char Max : "<<UCHAR_MAX<<endl;
cout <<"Header : "<<sizeof(header)<<endl;
cout<<"Struct : "<<sizeof(tea_hdr)<<endl;
system("PAUSE");
return 0;
}
//Snippet END
Now if you compile it as follow the Size of the Structure is 8 (The integer
(5) and the char[3]) That is ok.,
Now remove the comment before item 3 and compile and run Again.. The size
is STILL the same ?? Is this just my
PC.. must I reboot.. BUT there is more.
Now uncomment item 4 .. Compile and RUN.. SIZE is 12 that is 4 bytes
extra,But there is still more
Now uncomment item 5 and compile and run.. IT stays at 12 bytes again..
This seems to be variable
items count related. I have sifted the vars around and nothing is with
there posisions..
Also it only starts when MIXING variables in the structure.. I have a
feeling that this is something
that can be compiler related.. BUT what I did notice in the 'stddef.h' File
is that the MingW32 oaks have
a prob with the defs of the Gcc compiler and has added some macro to fix it
.. I wonder if maybe something else
also was missed that can relate to this. That the problem can maybe be at
the MingW32 project.
I don't have any answer's just maybes. but maybe it can help you in getting
to a answer For IF I cam correct you are trying to
read the information into the structure from a File .. like the header of a
MP3..
I dont have any other Ideas atound this but I hope you get a solution
Good luck
Johannes
At 09:22 03/07/2001 +0530, you wrote:
>Hi,
>
>I've noticed this strange little Bug.I'm not certain weather its a bug or a
>feature.
>
>The size of this structure is actually 13 bytes.
>
>But sizeof(header) says its 16.
>
>struct tea_hdr
> {
> char jte[3];
> unsigned char version;
> DWORD infile_size;
> DWORD infile_crc;
> unsigned char fname_sz;
> }header;
>
>No, if I comment out the fname_sz member,
>
>struct tea_hdr
> {
> char jte[3];
> unsigned char version;
> DWORD infile_size;
> DWORD infile_crc;
> //unsigned char fname_sz;
> }header;
>
>sizeof(header) returns 12.
>
>Is the compiler trying to align structure on DWORD boundaries?
>
>How do I get rid of this Bug/Feature ?
>
>Thnx,
>Jeethu Rao
>
>
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Lover of C++ <lo...@gl...> - 2001-07-03 06:07:55
|
It is not a bug, it is compiler feature. For some optimizations some c++
compilers adjust the size of structue to the multiple of 8.
Hi there
-------------
I am sorry to say that I dont have an answer for you . The LibC help file
does say that the Data Types can vary from Compiler
to Compiler But I WOULD like to show what I have also picked up with your
Code
//Snippet - START
#include <iostream.h>
#include <stdlib.h>
#include <limits.h>
struct tea_hdr{
int version; //-- 1
char jte[3]; // -- 2
//char fname_sz; // -- 3
//char infile_size; // -- 4
// char infile_crc; // -- 5
}header;
int main()
{
cout<<"Char Minimum : "<<CHAR_MIN<<endl;
cout<<"Char Max : "<<CHAR_MAX<<endl;
cout<<"Unsigned Char Max : "<<UCHAR_MAX<<endl;
cout <<"Header : "<<sizeof(header)<<endl;
cout<<"Struct : "<<sizeof(tea_hdr)<<endl;
system("PAUSE");
return 0;
}
//Snippet END
Now if you compile it as follow the Size of the Structure is 8 (The integer
(5) and the char[3]) That is ok.,
Now remove the comment before item 3 and compile and run Again.. The size
is STILL the same ?? Is this just my
PC.. must I reboot.. BUT there is more.
Now uncomment item 4 .. Compile and RUN.. SIZE is 12 that is 4 bytes
extra,But there is still more
Now uncomment item 5 and compile and run.. IT stays at 12 bytes again..
This seems to be variable
items count related. I have sifted the vars around and nothing is with
there posisions..
Also it only starts when MIXING variables in the structure.. I have a
feeling that this is something
that can be compiler related.. BUT what I did notice in the 'stddef.h' File
is that the MingW32 oaks have
a prob with the defs of the Gcc compiler and has added some macro to fix it
.. I wonder if maybe something else
also was missed that can relate to this. That the problem can maybe be at
the MingW32 project.
I don't have any answer's just maybes. but maybe it can help you in getting
to a answer For IF I cam correct you are trying to
read the information into the structure from a File .. like the header of a
MP3..
I dont have any other Ideas atound this but I hope you get a solution
Good luck
Johannes
At 09:22 03/07/2001 +0530, you wrote:
>Hi,
>
>I've noticed this strange little Bug.I'm not certain weather its a bug or a
>feature.
>
>The size of this structure is actually 13 bytes.
>
>But sizeof(header) says its 16.
>
>struct tea_hdr
> {
> char jte[3];
> unsigned char version;
> DWORD infile_size;
> DWORD infile_crc;
> unsigned char fname_sz;
> }header;
>
>No, if I comment out the fname_sz member,
>
>struct tea_hdr
> {
> char jte[3];
> unsigned char version;
> DWORD infile_size;
> DWORD infile_crc;
> //unsigned char fname_sz;
> }header;
>
>sizeof(header) returns 12.
>
>Is the compiler trying to align structure on DWORD boundaries?
>
>How do I get rid of this Bug/Feature ?
>
>Thnx,
>Jeethu Rao
>
>
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Elias P. <eli...@ao...> - 2001-07-03 11:08:03
|
> It is not a bug, it is compiler feature. For some optimizations some c++
> compilers adjust the size of structue to the multiple of 8.
>
I think it is only a multiple of 4. The reason is that the compiler you are
using is for a 32bit processor. You will find the same thing on any other
compiler which outputs 32bit object code as well. (A 64bit processor
probably will use 8 byte.) The reason for this is, for a 32-processor the
memory is just a block of 32 bit long units, and 32 bits are 4 bytes. So
every variable (even structs) should start on a 4 bytes boundary. That's
why they have to be aligned to the length of a multiple of 4 bytes.
To turn the alignment of structs off in mingw, try this (the code will
not be portable anymore then though, because if you compile with another
compiler like MSVC, it won't recognize it):
struct mystruct {
char a;
int b;
} __attribute__((packed)) c;
sizeof(c) should now be 5, one char (1) and one int (4).
Regards,
Elias Pschernig
|
|
From: Vassili <v.v...@bt...> - 2001-07-03 18:17:56
|
This is due to the compiler aligning members at addresses that are multiples
of 8 bytes. You'll find the something happens if you use classes.
----- Original Message -----
From: "Elias Pschernig" <eli...@ao...>
To: <dev...@li...>
Sent: Tuesday, July 03, 2001 12:07 PM
Subject: Re: [Dev-C++] [Dev-C++]: Errors in structures
> > It is not a bug, it is compiler feature. For some optimizations some c++
> > compilers adjust the size of structue to the multiple of 8.
> >
>
> I think it is only a multiple of 4. The reason is that the compiler you
are
> using is for a 32bit processor. You will find the same thing on any other
> compiler which outputs 32bit object code as well. (A 64bit processor
> probably will use 8 byte.) The reason for this is, for a 32-processor the
> memory is just a block of 32 bit long units, and 32 bits are 4 bytes. So
> every variable (even structs) should start on a 4 bytes boundary. That's
> why they have to be aligned to the length of a multiple of 4 bytes.
>
> To turn the alignment of structs off in mingw, try this (the code will
> not be portable anymore then though, because if you compile with another
> compiler like MSVC, it won't recognize it):
>
> struct mystruct {
> char a;
> int b;
> } __attribute__((packed)) c;
>
> sizeof(c) should now be 5, one char (1) and one int (4).
>
> Regards,
>
> Elias Pschernig
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Ioannis V. <no...@ya...> - 2001-07-03 11:29:09
|
This is not an error. A structure's space in memory is not the summary
of the sizeof() of everyone of its members. It may occupy more memory
due to compiler's memory management technique. For more information read
section 8.7 in K&R 2. For example a compiler's memory management can be
a connected list of structs (compilers, operating systems e.t.c. are
written in C/C++ too). So in the example of K&R it has a list of unions
connected structs:
typedef long Align;
union header
{
struct
{
union header *ptr; /* points to next block */
unsigned size;
}s;
Align x;
};
typedef union header Header;
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Jeethu
Rao
Sent: Tuesday, July 03, 2001 5:52 AM
To: Dev CPP Mailing List
Subject: [Dev-C++] [Dev-C++]: Errors in structures
Hi,
I've noticed this strange little Bug.I'm not certain weather its a bug
or a feature.
The size of this structure is actually 13 bytes.
But sizeof(header) says its 16.
struct tea_hdr
{
char jte[3];
unsigned char version;
DWORD infile_size;
DWORD infile_crc;
unsigned char fname_sz;
}header;
No, if I comment out the fname_sz member,
struct tea_hdr
{
char jte[3];
unsigned char version;
DWORD infile_size;
DWORD infile_crc;
//unsigned char fname_sz;
}header;
sizeof(header) returns 12.
Is the compiler trying to align structure on DWORD boundaries?
How do I get rid of this Bug/Feature ?
Thnx,
Jeethu Rao
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|