Skip to main content

6 posts tagged with "c++"

View All Tags

cpp基础

· One min read

Storage duration

这个是描述变量的生命周期的,分为四类

  • automatic: 生命周期在代码块内,在代码块内分配内存,在代码块内析构
  • static:生命周期是整个程序。分配内存的时机是程序开始前,析构是在程序结束之后(和static 关键词没有太大关系)
  • thread: 生命周期是线程开始和线程结束
  • dynamic:动态生命周期,一般是new、malloc一类

Linkage

linkage 描述的是变量可见性,分为三种:

  • no linkage: 当前代码块可见
  • internal linkage:当前编译单元内可见
  • external linkage:其他编译单元可见

Storage-class specifiers

Storage-class specifiers 分为四类:

  • auto
  • register
  • static
  • extern

这四个Storage-class specifiers 会映射Storage durationLinkage

相关阅读

c字节对齐

· One min read
_attribute_((packed))

你会在redis的sds.h看到这个gcc的扩展属性,这个属性是拿来干嘛呢?其实是拿来压缩字段长度的

This attribute, attached to an enum, struct, or union type definition, specified that the minimum required memory be used to represent the type.
Specifying this attribute for struct and union types is equivalent to specifying the packed attribute on each of the structure or union members. Specifying the -fshort-enums flag on the line is equivalent to specifying the packed attribute on all enum definitions.

You may only specify this attribute after a closing curly brace on an enum definition, not in a typedef declaration, unless that declaration also contains the definition of the enum.

相关阅读

Tags:

c99柔性数组

· One min read

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. With two exceptions, the flexible array member is ignored. First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.106) Second, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

Tags: