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

以下是 [Test 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

操作系统:

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

填充是特定于实现的 - 如果您需要更多控制,您可以尝试使用alignedpacked 属性

例如:

__attribute__((packed))

请记住,使用这些属性可能会导致可移植性问题

于 2013-04-09T16:10:12.007 回答