Network (data type)

from Wikipedia, the free encyclopedia

A composite ( English object composition is) a data type , which was composed of one or more data types. The components of a network can in turn be groups, which means that complex data structures can also be defined.

The components / elements of a compound are normally arranged one after the other in the memory (for example as structin the programming language C or as recordin Pascal ). Unions are an exception . In other programming languages (see below) this data type is sometimes referred to differently.

Use in different programming languages

Structures in the programming languages ​​C ++ and C

In the programming languages C and C ++ , composite types are referred to as structure and are declared with the keyword struct(short for structure , structure '). The individual components of a structure, the so-called members 'members', may be any simple data type, fields of constant size or a pointer to the same structure.

In contrast to the union , the memory areas of the individual structure members do not overlap.

Example in C

#include <stdio.h>

struct Person
{
    int PersonalNummer;
    int Alter;
    char Name[20];
} * pPerson;

void main ()
{
    // Aufsuchen des Personaldatensatzes mithilfe des Schlüssels PersonalNummer:
    pPerson = ...; // Setzen des Zeigers pPerson auf eine der Strukturen

    printf ("PersonalNummer: %i, Alter: %i\n",
            pPerson->PersonalNummer, 
            pPerson->Alter);
}

If you have a pointer to the structure, then, as shown, the easiest way to access individual members is to use the arrow ->, for example pPerson->Alter. The arrow (engl. Arrow ) is a shorthand notation for (*pPerson).Alterthe rating *as dereferencing (engl. Dereference operator ) and the point .(engl. As a selector object selector ).

Size in memory

A structure can be larger than the sum of the individual data type sizes, since the compiler can align the individual attributes in memory with specific addresses . The arrangement of the structure in the memory is determined after the translation and can no longer be changed.

Differentiation between C ++ and C structure

In C, structures can only contain variables , pointers , arrays and other structures, while structures in C ++ have the additional ability to contain subroutines - so-called methods - which also include constructors and destructors . This can only be partially implemented in C using function pointers, which can also be part of structures.

Differentiation between C ++ structure and C ++ class

In C ++, the keywords publicand are privateused to regulate the access rights to attributes and methods in structures and classes. The only difference here is that, without explicit specification, the attributes of structures are by default public(access from outside permitted) those of a class private(access only from within the class or through friend functions ).

Implementation in Pascal

A compound ( recorddenoted with in Pascal ) of the data type Personfor the two instances Mustermann1and Mustermann2could be defined and used in Component Pascal, for example, as follows, and only instances of the same data type are assignment-compatible in this programming language :

 MODULE Personen;

 IMPORT Dates;

 TYPE
    Person = RECORD
       Vorname, Name, Wohnort: ARRAY 256 OF CHAR;
       Geburtstag: Dates.Date;
    END;

 VAR Mustermann1, Mustermann2: Person;

 BEGIN

    Mustermann1.Vorname          := "Hans";
    Mustermann1.Name             := "Mustermann";
    Mustermann1.Wohnort          := "Musterstadt";
    Mustermann1.Geburtstag.day   :=    1;
    Mustermann1.Geburtstag.month :=    1;
    Mustermann1.Geburtstag.year  := 1900;

    Mustermann2 := Mustermann1; (* Zwei Variablen vom selben Datentyp sind zuweisungskompatibel *)

 END Personen.

The imported data type defined in the module is in turn a compound with the integer elements (day), (month) and (year). DatesDates.Datedaymonthyear

Data groups in Cobol

In Cobol the data type group is called 'data group' (also group variable or group item). A data group is declared with its identifier and is the umbrella term or the summary for the hierarchically subordinate data types - which can themselves be data groups again. It has no format specification of its own (PIC clause). Using the OCCURSclause, a data group can also be declared as a table (= array ), if necessary also multi-level.

In the commands addressing the data group, the entirety of the subordinate fields is treated as one (1) data field in PIC-X character format and in the total length of all individual fields . With a MOVEcommand, for example, there is no individual format-specific processing of the individual fields, nor is there any format conversion.

Using the REDEFINESclause, a data group can 'redefine' another data group, so that both data groups use the same storage space. This is used, for example, for processing different input data, alternatively in one or the other data structure. This corresponds to the UNION construct in other programming languages.

Unions

In so-called unions , all components start at the same memory address, i. That is, their memory areas completely or at least partially overlap. A union uses at least as much memory as its largest component.

Unions are implemented in various programming languages ​​either as tagged unions or untagged unions.

A tagged union assigns a tag to each component. When a component is written, the tag of this component is stored in the union variable. In the case of read access to the component of a union, the tag of the component to be read is compared with the tag of the last component written. If the tags differ, a type error has been identified. Tagged unions are therefore type-safe .

Untagged unions do not use tags and are therefore type-unsafe. This means that it is the responsibility of the programmer to determine whether the last write access to a union changed the same component that the subsequent read access also reads out. In addition to unintentional type errors when using untagged unions, there are also use cases for the side effects of untagged unions. For example, a union of an IEEE floating point number and one structwhose components allow access to the sign, mantissa and exponent.

Example in C

#include <stdio.h>
#include <math.h>
#include <inttypes.h>

union Float64Components   
{
    double       Float64;
    struct 
    {
        uint64_t Mantissa52 : 52;
        uint64_t Exponent11 : 11;
        uint64_t Sign1      :  1;
    };
};

void main ()
{
    union Float64Components  pi;
    pi.Float64 = 3.1415926535897932384626433832795;
    printf ("%20.16f   %13I64X %03I64X %01I64X   (%20.16f)\n", 
            pi.Float64, 
            pi.Mantissa52, pi.Exponent11, pi.Sign1, 
            (pi.Sign1 ? -1 : +1) * (1.0 + pi.Mantissa52/4503599627370496.0) * pow (2, pi.Exponent11-1023));
}

output

 3.1415926535897931   921FB54442D18 400 0   (  3.1415926535897931)

Example in Haskell

Example of the declaration of a union in Haskell :

data Tree a = Br (Tree a) (Tree a) |
              Leaf a               |
              Nil

Tree is an algebraic data type. Br, Leaf, and Nil are the constructors.

See also

Individual evidence

  1. Peter Becker, Andreas Bruchmann, Dirk F. Raetzel: Differences to the data type struct . (No longer available online.) In: Chapter 12: Object-Oriented Programming in C ++. In: C ++ - An Introduction. Manfred Sommer, Philipps University Marburg, June 5, 2001, archived from the original on January 10, 2014 ; Retrieved January 10, 2014 (hosted at http://ip-klaeden.dyndns.org/ ). Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / ip-klaeden.dyndns.org
  2. Classes (I) on cplusplus.com