The size of the structure array 
Author Message
 The size of the structure array

Quote:

> I declare a structure array and the size is 35bytes(could be caculated by
> yourself), but the REAL size of is 50bytes!!!
> I want to use it as 35 bytes. Where is the problem? How should I declare the
> structure array?

> The language I am using is VC++ 6.0.

> You can try the following small program to see the problem.

> Thanks!

> Yuan Shan

> http://www.*-*-*.com/

> struct { //7 bytes
> unsigned char flag; //1 byte
> struct {
>   unsigned short startpoint; //2 bytes
>   unsigned char length; //1 byte
> }chain[2];
> }array[5]; //This array should be 35 bytes

You are seeing structure padding and compilers are allowed to do this -- it
starts each member at an address on a 32-bit boundary.  On some processors (but
not Intel) padding is mandatory.  If you must have it be the minimum size
(computation will be slower) use

#pragma pack(1)
struct {....};
#pragma pack()

--
Scott McPhillips [VC++ MVP]



Sun, 18 Aug 2002 03:00:00 GMT  
 The size of the structure array
I declare a structure array and the size is 35bytes(could be caculated by
yourself), but the REAL size of is 50bytes!!!
I want to use it as 35 bytes. Where is the problem? How should I declare the
structure array?

The language I am using is VC++ 6.0.

You can try the following small program to see the problem.

Thanks!

Yuan Shan

http://www.ChinaBadminton.com

struct { //7 bytes
unsigned char flag; //1 byte
struct {
  unsigned short startpoint; //2 bytes
  unsigned char length; //1 byte

Quote:
}chain[2];
}array[5]; //This array should be 35 bytes

#include <memory.h>
#include <stdio.h>
void main(void)
{
int i,j;
FILE *fp;
char name[] = "test.txt";

for (i=0; i<5; i++) {
  array[i].flag = 0x11;
  for (j=0; j<2; j++) {
   array[i].chain[j].startpoint = 0x11;
   array[i].chain[j].length = 0x11;
  }

Quote:
}

fp = fopen(name,"wb");
fwrite(array,1,sizeof(array),fp);
fclose(fp);

printf("size of array is:%d\n",sizeof(array));

Quote:
}



Mon, 19 Aug 2002 03:00:00 GMT  
 The size of the structure array

Quote:
> I declare a structure array and the size is 35bytes(could be caculated by
> yourself), but the REAL size of is 50bytes!!!
<snip>
> struct { file://7 bytes
> unsigned char flag; file://1 byte
> struct {
>   unsigned short startpoint; file://2 bytes
>   unsigned char length; file://1 byte
> }chain[2];
> }array[5]; file://This array should be 35 bytes

<snip>

It won't be as small as you think
because of the structure member
alignment setting in the project.

If you *really* want 35-bytes, go to
the Project-Settings-C/C++ and
change the "Code Generation"
entry for "Struct member alignement"
to 1-byte.

-------------------------------------------------------------------
"If you think you know the answer,
then you don't understand the question !"
-------------------------------------------------------------------



Mon, 19 Aug 2002 03:00:00 GMT  
 The size of the structure array
This is because the compiler will pading space after "char" data type so
that every data member in a struct will be aligned at the word(2 bytes)
boundary. So for all the chars that you declared in your struct, they
actraully occupy 2 bytes. That is why the size is 50.


Mon, 19 Aug 2002 03:00:00 GMT  
 The size of the structure array
This is because you are probably using the default 8 bytes alignment. Check
out "alignment of structures" in the MSDN for a detailled explaination and
for hints on how to control the alignment.

Vincent Jacquet



Mon, 19 Aug 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. The size of the structure array

2. The size of the structure array

3. How to return the size of a structure array..

4. An array in a structure Or an Array Of Structures

5. Largest Array of Structures Size Question

6. Find size of variable size structure?

7. Array of Structures in a Structure.

8. structure of array of structure : help needed

9. Novice: Pointing to arrays in array of structures

10. Novice: Pointing to arrays in array of structures

11. Differences between char array[SIZE] and char *array

12. Arrays [256][256] --- error Array size too large!

 

 
Powered by phpBB® Forum Software