Variable memory allocation in Sysmac

Updated December 18, 2023

Introduction

To ensure proper memory alignment between variables in different devices, it's important to understand how memory allocation works in Sysmac, especially in the context of Ethernet/IP. When using certain types of communication, such as EtherNet/IP tag data links or CIP messages, or when exchanging data with devices other than CPU Units, adjustments may need to be made to the memory allocation of structure or union variables. This will help ensure that the memory locations of the members of these variables are properly aligned with the variables in the other devices. 

Each data type has a specific data size, which represents the minimum amount of memory needed to store the value or values of that data type. However, when it comes to memory allocation for variables, the Controller automatically structures the memory for the most efficient access. As a result, the total amount of memory needed for variables may not necessarily be the sum of the data sizes of those variables. For instance, if WORD and DWORD variables are declared, their data sizes add up to six bytes, but the Controller allocates eight bytes in memory, as illustrated in the figure below.

The alignments and the amounts of memory that are allocated for the basic data types and enumerations are given below.

Delete

Info

Unless otherwise specified, NJ allocation is used.


Basic Data Types

Example #1: 2 Byte Alignment - BOOL

For individual BOOL variables or structure members, two bytes of memory are allocated.

For a BOOL array, each element is assigned one bit of memory instead of two bytes, resulting in a smaller memory usage. However, the entire array still occupies a multiple of two bytes of memory, even if some bits are unused.

Delete

Example #2: 1 Byte Alignment - BYTE

One byte of memory is allocated for the one-byte alignment.
Example: Two consecutive BYTE variables


Delete

Example #3: 4 Byte Alignment - DWORD

Four bytes of memory are allocated for the four-byte alignment.
The location of the first byte of data in memory is an integer multiple of four bytes. Therefore, if a variable with a two-byte alignment, such as WORD data, is inserted, two bytes of unused memory will remain.
Example: Consecutive variables in the following order: DWORD, WORD, and DWORD

Delete

Example #4: 8 Byte Alignment - LWORD

Eight bytes of memory are allocated for the eight-byte alignment.
The location of the first byte of data in memory is an integer multiple of eight bytes. Therefore, if a variable with a two-byte alignment, such as WORD data, is inserted, six bytes of unused memory will remain. If a variable with a four-byte alignment, such as DWORD data, is inserted, four bytes of
unused memory will remain.
Example: Consecutive variables in the following order: LWORD, WORD, and LWORD

Delete
Delete

Arrays

An array is a group of data elements that are stored in a single section of memory, based on the data size of the array variable. The alignment of the array depends on the data type of its elements. While most arrays are fairly intuitive, please take note of the Boolean Array example which is an exception.

BOOL Array

Example: Continuous variables in the following order: two BOOL variable, one BOOL array with five
elements, one BOOL array with 19 elements, and one BOOL array with four elements

Delete

BYTE Array

Example: BYTE array with four elements for each dimension with two-dimensional array

Delete

WORD Array

Example: WORD array with three elements for each dimension with two-dimensional array

Delete

INT Array

Example: INT array with five elements

Delete
Delete

Structures

When you declare a structure variable, its members are located in memory in the same order that they are declared.

Data is aligned according to its type. This means that data will be allocated to an address starting at an Integer multiple of the alignment. For example a DWORD with an alignment of 4 will automatically be allocated at set intervals such as n+0, n+4, n+8, n+12 etc. This means that there may be unused memory between members or at the end of the last member.
The alignment of a structure is determined by the largest alignment of any of its members. The amount of memory allocated for a structure variable is an integer multiple of its alignment, which is larger than the amount of memory needed when the members are arranged in order at integer multiples of their data type alignments. See example 3 for more information on this feature.

Example 1

Example: The alignments and the amounts of memory that are allocated for the four variable declarations given in the following figure are given in the following table.

Delete

Example 2

Example: The alignments and the amounts of memory that are allocated for the four variable declarations given in the following figure.

Delete

Example 3

Example: The alignment of a structure is determined by the member with the largest alignment, which is calculated as an integer multiple.


Delete
Delete

Unions

In a union variable, the members share the same memory locations. The alignment of the union is based on the largest alignment of any of its members. The amount of memory allocated for a union is based on the largest memory requirement of any of its members.

Example: The alignments and the amounts of memory that are allocated for the four variable declarations given in the following figure are given in the following table

Delete

Memory Realignment

When you exchange structure variable data between an NJ/NX-series CPU Unit and a remote device, you must align the memory configuration of the structure variable members with those of the remote device. There are two methods that you can use to align the memory configuration with a remote device.
This section describes how to align the memory configuration for these Units.

Offset Type - CJ

When using the CJ offset type, the data will be arranged as compactly as possible, while still adhering to the minimum word size allocation. As a result, any unused words between data members are typically removed.

Example: The following example shows how the memory configuration of the structure variable in the NJ/NX-series CPU Unit is changed to match the memory configuration of the structure variable in the CJ-series CPU Unit.

Delete

Offset Type - User

By selecting the User offset type, you have the flexibility to define your own offsets, which can be beneficial when the tag set of the target includes memory that is reserved or unused. This option also provides you with greater control and enables you to allocate a 1 BYTE instead of 2 BYTES for booleans by assigning the next variable to use the allocated but unused memory.

Example: The following shows how a user offset can be used to avoid wasting memory when allocating boolean variables. Extra unused space may also be added to account for reserved areas on the target device. It is worth noting that the allocation of Bool1 to bit 6 of byte 0 is arbitrary and it could have been allocated to bit 5 or 7 as well.


Delete


Start a Conversation
This conversation is closed
Uploading...