C++ 零长度数组及其类型转换

C++ 中在一个结构体或类的最后可以申明一个长度为 0 的数组,这会使结构体成为可变长的。

class Line {
public:
  string ToString() const {
    stringstream s;
    s << "length: " <<  length << " content: ";
    for (int i = 0; i < length; ++i) {
      s << content[i] << " ";
    }
    return s.str();
  }

  void SetLength(int l) {
    length = l;
  }

  void SetContent() {
    for (int i = 0; i < length; ++i) {
      content[i] = i + 1;
    }
  }
  
private:
  int length;
  int content[0];
};

以上面的 Line 类为例,它有一个 int 类型成员和一个零长度数组成员。对于编译器来说,此时长度为 0 的数组不占空间,因为数组名本身不占空间,它只是一个偏移量,数组名这个符号本身代表了一个不可修改的地址常量。

Line *line1 = new Line;
cout << sizeof(*line1) << endl;

// output:
// 4

上面代码的输出是 4 ,可以看到只占用一个 int 的存储空间。

零长度数组的使用

const int CUR_LENGTH = 3;
Line *line1 = (class Line *) malloc(sizeof(Line) + sizeof(int) * CUR_LENGTH);
line1->SetLength(CUR_LENGTH);
line1->SetContent();
cout << line1->ToString() << endl;
cout << sizeof(*line1) << endl;
// 销毁
free(line1);

// output:
// length: 3 content: 1 2 3 
// 4

可以通过 malloc 为使用零长度数组的结构体申请存储空间,申请空间之后可以像使用普通数组一样使用可变长数组,但需要注意访问数组时下标不要越界。即使 content 数组有 3 个元素,sizeof(*line1) 的结果仍然是 4。

普通结构体到零长度数组结构体的类型转换

class ConcreteLine {
public:
  ConcreteLine() {
    length = 3;
    for (int i = 0; i < length; ++i) {
      content[i] = i + 1;
    }
  }

  string ToString() const {
    stringstream s;
    s << "length: " <<  length << " content: ";
    for (int i = 0; i < length; ++i) {
      s << content[i] << " ";
    }
    return s.str();
  }

private:
  int length;
  int content[3]{};
};

上面是一个正常的类,它有一个大小固定为 3 的 int 数组,可以通过 reinterpret_cast 将其转换为 Line 类型的指针。

ConcreteLine *cline = new ConcreteLine;
cout << cline->ToString() << endl;
cout << "size of *cline: " << sizeof(*cline) << endl;

Line *line = reinterpret_cast<Line *>(cline);
cout << line->ToString() << endl;
cout << "size of *line: " << sizeof(*line) << endl;

// output: 
// length: 3 content: 1 2 3 
// size of *cline: 16
// length: 3 content: 1 2 3 
// size of *line: 4

转换后 line 是一个 Line 类型的指针,但它实际上指向一个 ConcreteLine 类型的对象。

普通数组到零长度数组结构体的类型转换

还可以将普通数组转换为使用零长度数组的结构体,以下面代码为例:

int data[] = {3, 2, 3, 4};
Line *line = reinterpret_cast<Line *>(data);
cout << line->ToString() << endl;

// output:
// length: 3 content: 2 3 4

如上所示,转换后 data 数组的第一个元素为 linelength 成员,后面 3 个元素为 content 中的值。

reinterpret_cast 只是对原来地址上的内容重新做解释,因此 data 数组的类型甚至不需要是 int。

char data[] = {3, 0, 0, 0,
               2, 0, 0, 0,
               3, 0, 0, 0,
               4, 0, 0, 0};
Line *line2 = reinterpret_cast<Line *>(data);
cout << line2->ToString() << endl;

// output:
// length: 3 content: 2 3 4

reinterpret_cast 本质上依赖机器,macOS 是大端字节序(Big Endian),因此我的代码中逆序存储四个 char 元素使其表示一个 int。

char data[16]{};
Line *line2 = reinterpret_cast<Line *>(data);

line2->SetLength(3);
line2->SetContent();
cout << line2->ToString() << endl;
for (const auto &item : data) {
  cout << int(item) << " ";
}
cout << endl;

// output:
// length: 3 content: 1 2 3 
// 3 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 

通过 line2 改变所指对象的值,也会修改 data 数组的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值