2

その挙動がわかりません。それは間違っていると思います。GLKVectors を含む構造体が問題を引き起こします。ここに簡単なコードがあります。妥当な結果だと思いませんか?

// Test.h
#import <GLKit/GLKit.h>

typedef struct
{
    GLKVector2 v1;
    float f1;
} V2F1;

typedef struct
{
    GLKVector2 v1;
    GLKVector2 v2;
} V2V2;

typedef struct
{
    GLKVector3 v1;
    GLfloat f1;
} V3F1;

typedef struct
{
    GLKVector3 v1;
    GLKVector2 v2;
} V3V2;

typedef struct
{
    GLKVector3 v1;
    GLKVector3 v2;
} V3V3;

typedef struct
{
    GLKVector3 v1;
    GLKVector4 v2;
} V3V4;


typedef struct
{
    GLKVector4 v1;
    GLfloat f1;
} V4F1;

typedef struct
{
    GLKVector4 v1;
    GLKVector2 v2;
} V4V2;

typedef struct
{
    GLKVector4 v1;
    GLKVector3 v2;
} V4V3;

typedef struct
{
    GLKVector4 v1;
    GLKVector4 v2;
} V4V4;

typedef struct
{
    GLKVector4 v1;
    float f1; float f2;
} V4F2;

typedef struct
{
    GLKVector4 v1;
    float f1; float f2; float f3;
} V4F3;

typedef struct
{
    GLKVector4 v1;
    float f1; float f2; float f3; float f4;
} V4F4;

typedef struct
{
    GLKVector4 v1;
    float f1; float f2; float f3; float f4; float f5;
} V4F5;

typedef struct
{
    GLKVector4 v1;
    float f1; float f2; float f3; float f4; float f5; float f6;
} V4F6;

typedef struct
{
    GLKVector4 v1;
    float f1; float f2; float f3; float f4; float f5; float f6; float f7;
} V4F7;

typedef struct
{
    GLKVector4 v1;
    float f1; float f2; float f3; float f4; float f5; float f6; float f7; float f8;
} V4F8;

@interface Test : NSObject
@end


// Test.m
#include "Test.h"
@implementation Test
+ (void)doTest
{
#define ConfirmSize(s_) size == s_ ? @"" : [NSString stringWithFormat:@" (!%d)", s_]
    int size = 0;
    NSLog(@"Size of V2F1 = %d%@", size = sizeof(V2F1), ConfirmSize(12));
    NSLog(@"Size of V2V2 = %d%@", size = sizeof(V2V2), ConfirmSize(16));
    NSLog(@"Size of V3F1 = %d%@", size = sizeof(V3F1), ConfirmSize(16));
    NSLog(@"Size of V3V2 = %d%@", size = sizeof(V3V2), ConfirmSize(20));
    NSLog(@"Size of V3V3 = %d%@", size = sizeof(V3V3), ConfirmSize(24));
    NSLog(@"Size of V3V4 = %d%@", size = sizeof(V3V4), ConfirmSize(28));
    NSLog(@"Size of V4F1 = %d%@", size = sizeof(V4F1), ConfirmSize(20));
    NSLog(@"Size of V4V2 = %d%@", size = sizeof(V4V2), ConfirmSize(24));
    NSLog(@"Size of V4V3 = %d%@", size = sizeof(V4V3), ConfirmSize(28));
    NSLog(@"Size of V4V4 = %d%@", size = sizeof(V4V4), ConfirmSize(32));
    NSLog(@"Size of V4F1 = %d%@", size = sizeof(V4F1), ConfirmSize(20));
    NSLog(@"Size of V4F2 = %d%@", size = sizeof(V4F2), ConfirmSize(24));
    NSLog(@"Size of V4F3 = %d%@", size = sizeof(V4F3), ConfirmSize(28));
    NSLog(@"Size of V4F4 = %d%@", size = sizeof(V4F4), ConfirmSize(32));
    NSLog(@"Size of V4F5 = %d%@", size = sizeof(V4F5), ConfirmSize(36));
    NSLog(@"Size of V4F6 = %d%@", size = sizeof(V4F6), ConfirmSize(40));
    NSLog(@"Size of V4F7 = %d%@", size = sizeof(V4F7), ConfirmSize(44));
    NSLog(@"Size of V4F8 = %d%@", size = sizeof(V4F8), ConfirmSize(48));
}
@end

以下は、[テスト doTest] の結果です。

iOS:

Size of V2F1 = 16 (!12)
Size of V2V2 = 16
Size of V3F1 = 16
Size of V3V2 = 24 (!20)
Size of V3V3 = 24
Size of V3V4 = 32 (!28)
Size of V4F1 = 32 (!20)
Size of V4V2 = 32 (!24)
Size of V4V3 = 32 (!28)
Size of V4V4 = 32
Size of V4F1 = 32 (!20)
Size of V4F2 = 32 (!24)
Size of V4F3 = 32 (!28)
Size of V4F4 = 32
Size of V4F5 = 48 (!36)
Size of V4F6 = 48 (!40)
Size of V4F7 = 48 (!44)
Size of V4F8 = 48

OSX:

Size of V2F1 = 12
Size of V2V2 = 16
Size of V3F1 = 16
Size of V3V2 = 20
Size of V3V3 = 24
Size of V3V4 = 32 (!28)
Size of V4F1 = 32 (!20)
Size of V4V2 = 32 (!24)
Size of V4V3 = 32 (!28)
Size of V4V4 = 32
Size of V4F1 = 32 (!20)
Size of V4F2 = 32 (!24)
Size of V4F3 = 32 (!28)
Size of V4F4 = 32
Size of V4F5 = 48 (!36)
Size of V4F6 = 48 (!40)
Size of V4F7 = 48 (!44)
Size of V4F8 = 48

私の Xcode のバージョンは 4.6.1 です。

4

1 に答える 1

3

パディングは実装固有です。さらに制御が必要な場合は、alignedおよびpacked 属性を試してみてください。

例えば:

__attribute__((packed))

これらの属性をいじると、移植性の問題が発生する可能性があることに注意してください。

于 2013-04-09T16:10:12.007 に答える