Structures. Part 2. Memory allocation for the structure. Nested structures. Arrays of native-structures

Structures. Part 2. Memory allocation for the structure. Nested structures. Arrays of native-structures

This topic is based on the topics:


Content
  1. How is memory allocated for a structured variable? Example of using the sizeof operation
  2. What are the features of the use of nested structures in the programs?
  3. Example of declaring and using a nested native-structure
  4. Example of declaration and use of a nested ref-structure
  5. Example of declaration and the use of nested value-structure
  6. How do I declare an array of structures (native)? Example of declaring and use
  7. An example of declaring and using an array of native-structures, which is a separate structure field (nested within a structure)
  8. Example of declaring and using a two-dimensional array of native-structures

1. How is memory allocated for a structured variable? Example of using the sizeof operation

The declaration of the structural variable is carried out in two stages:

  • declaration of the structure template as a new data type. At this stage, memory is not allocated. Only information about the contents of the structure is generated;
  • declaration of the variable itself. At this stage, memory is allocated for any field (variable) that is described in the structure template.

Example. Let the native-structure template that describes the book in the library be specified.

struct Book
{
    char title[70];
    char author[50];
    int year;
    float price;
};

After such a description, memory is not allocated.

If you describe a variable of type Book, then for such a variable, memory will be allocated.

Book B; // for variable B 128 bytes are allocated

The size of the memory allocated for variable B will be: 70 + 50 + 4 + 4 = 128 bytes. For the ‘title’ field, 70 bytes are allocated (char type takes 1 byte). For the author field, 50 bytes are allocated. For the ‘year’ field (in Win32), 4 bytes (int type) are allocated. For the ‘price’ field, 4 bytes (float type) are allocated.

Depending on the size of the operating system and the hardware configuration, this figure may be different.

Example. Determining the size of the memory that is allocated to the structural variable.

// How to determine the size of memory allocated for a structured variable
Book B;
int size;
size = sizeof(B); // size = 128

 

2. What are the features of the use of nested structures in the programs?

The template of any structure may include other structures. If another structure-variable is described in the structure, then for this variable memory is allocated according to the memory allocation rules for the structural variable (see item 1).

 

3. Example of declaring and using a nested native-structure

Let two templates of structures are declared, which are declared in a separate file “MyStruct.h”:

– a Point pattern describing a point on the coordinate plane:

// a template for a native-structure that describes a point on the plane
struct Point
{
    int x;
    int y;
};

– a Triangle pattern describing a triangle in the plane:

// the template of the native-structure that describes the triangle
struct Triangle
{
    Point p1; // nested structure
    Point p2; // nested structure
    Point p3; // nested structure
    char comment[50]; // Comment to the triangle
};

The Triangle template describes three nested Point-type structures.

Demonstration of work with the structure of Triangle.

// nested native-structure
Triangle T; // the variable T is allocated memory

// filling the values of the variable T
T.p1.x = 25;
T.p1.y = 36;
T.p2.x = 100;
T.p2.y = 55;
T.p3.x = 60;
T.p3.y = 56;
strcpy(T.comment, "Triangle #1");

To use the strcpy() method and connect the structure file, at the beginning of the program module, enter:

#include <cstring>
#include "MyStruct.h"

In the native-structure

  • you can declare another native-structure;;
  • you can declare a value-structure;
  • you can not declare a ref-structure, because it is a reference type.

 

4. Example of declaration and use of a nested ref-structure

Let the template of the ref-structure, which describes the point on the coordinate plane, is given

// The ref-structure template
ref struct Point_ref
{
    int x;
    int y;
};

However, this structure can be nested in another ref-structure. Example

// A ref-structure template, that describes a rectangle
ref struct Rectangle_ref
{
    Point_ref p1; // p1 – nested ref-structure
    Point_ref p2; // p2 – nested ref-structure
    char * comment; // Text in a rectangle
};

...

// demonstration of using a nested ref-structure
Rectangle_ref R1;
R1.p1.x = 23;
R1.p1.y = 34;
R1.p2.x = 55;
R1.p2.y = 68;
R1.comment = new char[50];
strcpy(R1.comment, "This is rectangle");

In the ref-structure:

  • you can declare another ref-structure;
  • you can declare a value-structure;
  • you can not declare a native structure. The system displays a message: “Mixed types are not supported”.

 

5. Example of declaration and the use of nested value-structure

Let there be two templates of value-structures that describe a point (Point_value) and a triangle (Triangle_value)

// A value-structure template that describes the point
value struct Point_value
{
    int x;
    int y;
};

// A value-structure template that describes a triangle
value struct Triangle_value
{
    // p1, p2, p3 – nested value-structures
    Point_value p1;
    Point_value p2;
    Point_value p3;
    char * comment;
};

...

// nested value-structure
Triangle_value T1;

T1.p1.x = 100;
T1.p1.y = 120;
T1.p2.x = 115;
T1.p2.y = 145;
T1.p3.x = 150;
T1.p3.y = 109;
T1.comment = new char[50];
strcpy(T1.comment, "This is a triangle");

In the value-structure template:

  • you can declare another nested value-structure;
  • you can not declare a nested ref-structure
  • you can not declare a nested native-structure.

 

6. How do I declare an array of structures (native)? Example of declaring and use

For native-structures, the declaration of the array occurs in a classical way for the C/C++.

Let the template of the structure Point_native, which describes a point on the plane, be given.

// A template for a native-structure that describes a point on the plane
struct Point_native
{
    int x;
    int y;
};

Example 1. Declaring and using an array of structures as values.

// Array of native-structures
// Declaration of an array named M
Point_native M[10]; // The 10 Point_native structures are declared for which memory is allocated

// Filling structures with values
for (int i=0; i<10; i++)
{
    M[i].x = i*(i-2);
    M[i].y = i*3;
}

int d;
d = M[5].x; // d = 15
d = M[0].y; // d = 0

Example 2. Declaring and using an array of pointers to native-structures.

// Array of pointers to 10 native-structures
Point_native * PM[10];

// Allocating memory for each structure
for (int i=0; i<10; i++)
{
    PM[i] = new Point_native;
}

// Filling structures with random values
for (int i=0; i<10; i++)
{
    PM[i]->x = i*5+2;
    PM[i]->y = i*i*i;
}

// test
int d;
d = PM[4]->x; // d = 22
d = PM[4]->y; // d = 64

 

7. An example of declaring and using an array of native-structures, which is a separate structure field (nested within a structure)

A structure can contain a nested array of structures.

Let the template of the native-structure Point_native be given, which describes the point on the plane

// A template for a native structure that describes a point on the plane
struct Point_native
{
    int x;
    int y;
};

An array of n points (n = 1..10) can be represented as such a template:

// Array of native structures inside the native structure
PointsArray pt;

// Filling with arbitrary values
pt.n = 6;
for (int i=0; i<pt.n; i++)
{
    pt.pA[i].x = i*5;
    pt.pA[i].y = i*i;
}

// test
int d;
d = pt.pA[3].x; // d = 15
d = pt.pA[2].y; // d = 4

 

8. Example of declaring and using a two-dimensional array of native-structures

Let there be given a native-structure that describes a point on the plane.

// native-structure
struct MyPoint
{
    int x;
    int y;
    int color;
};

Then, working with a two-dimensional array of such structures of size 5 * 10 will be approximately the following

// Declaring an array with a name A of size 5 * 10
MyPoint A[5][10];

// Filling with arbitrary values
for (int i=0; i<5; i++)
    for (int j=0; j<10; j++)
    {
        A[i][j].x = i*2 + j*j;
        A[i][j].y = i*i + j;
        A[i][j].color = i+j;
    }

// test
int d;
d = A[3][4].x; // d = 22
d = A[2][5].y; // d = 9

 


Related topics