Skip to content
Search

Passive data structure

lastmod 2025-02-11created 2019-02-197 edits

In computer science and object-oriented programming, a passive data structure (PDS, also termed a plain old data structure, or plain old data, POD), is a term for a record, to contrast with objects. It is a data structure that is represented only as passive collections of field values (instance variables), without using object-oriented features.

Plain Old Data structure

Plain Old Data structure (POD) refers to a record, as opposed to an object in object-oriented programming. It is also written PODS, or called a passive data structure.

A record is a data structure made up of fields of variables. Structurally it resembles an object in object-oriented programming, but it lacks the essential features that object-oriented programming requires. For example, a record can be used as a collection of variables and can even call functions through pointers, but it has none of the features of an object, such as class inheritance or specifying variable visibility with public, private, and so on.

C/C++

Means an object that has the same memory layout as a C struct or a built-in-type. It has a memory layout that can be copied by simple memory copying with memset or memcpy. The conditions for a POD are as follows.

  • built-in type.
  • An object of a class with no virtual functions and no user-defined allocator or destructor.
  • An object of a class that has no non-POD as a non-static member.

POD(plain old data) vs standard layout type vs trivial type

POD (Plain old data)
A contiguous sequence of bytes in memory. Sometimes an object needs to be treated as a POD, for example when advanced semantics such as run-time polymorphism or user-defined copy semantics are not needed. This is because treating it as a POD lets the hardware move or copy the object more efficiently. In other words, a POD is an object that can be treated as "just data", without considering the complexity of class layout or user-defined construction, copy, and move semantics.
A POD object must meet the following conditions.
  • It must be a standard layout type.
  • It must be a trivial type.
Standard layout type
A type that clearly has the same layout as in C, and that common C++ ABIs(Apllication Binary Interfaces) can handle.
In detail, it has the following characteristics.
  • Has no virtual functions or virtual base classes.
  • Non-static data members have the same access control.
  • All non-static data members are standard layout.
  • All base classes are standard layout.
  • Does not have data of the same type as a base class as its first non-static member.
  • Has no reference members.
  • Does not have non-static data in more than one base class, or in both the derived and base classes.

For a simpler check, use std::is_standard_layout from <type_traits>.

Trivial type
A type that has a trivial default constructor and destructor, and
  • trivial copy and move operations.
In other words, bitwise operations must be possible.
Copy, move, and destructor are not trivial in the following cases.
  • They are user-defined.
  • The class has virtual functions.
  • The class has virtual base classes.
  • The class has non-trivial members or base classes.
As above, this can be checked easily with std::is_trivial from <type_traits>.
For reference, objects of built-in types have standard layout and are trivial types. Of course, the same goes for arrays of these types.
POD again
Has no complex layout. (virtual functions, etc.)
  • Has no user-defined copy semantics.
  • Has a trivial default constructor.
Also, since C++11, adding or removing non-default constructors does not affect layout or performance. Consider this example:

:

struct S0 { }; // POD
struct S1 { int i; }; // POD
struct S2 { int i; S2(int ii) : i(ii) { } }; // 기본 생성자가 없으므로 POD가 아니다
struct S3 { int i; S3(int ii) : i(ii) { } S3() {} }; // POD
struct S4 { int i; S4(int ii) : i(ii) { } S4() = default; }; // POD
struct S5 { virtual void f(); }; // 가상 함수가 있으므로 POD가 아니다

struct S6 : S1 { }; // POD
struct S7 : S0 { int b; } // POD
struct S8 : S1 { int b; } // 기저 클래스에 비정적 멤버가 있으므로 POD가 아니다.
struct S9 : S0, S1 { }; // POD
Likewise, this can be checked easily with std::is_pod from <type_traits>.

Favorite site

author Zer0 sitemap