Variable memory allocation in Sysmac
Learn how to effectively use and manage memory when working with Sysmac data.
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.
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.
Example #2: 1 Byte Alignment - BYTE
One byte of memory is allocated for the one-byte alignment.
Example: Two consecutive BYTE variables
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
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
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
BYTE Array
Example: BYTE array with four elements for each dimension with two-dimensional array
DeleteWORD Array
Example: WORD array with three elements for each dimension with two-dimensional array
DeleteStructures
When you declare a structure variable, its members are located in memory in the same order that they are declared.
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.
Example 2
Example: The alignments and the amounts of memory that are allocated for the four variable declarations given in the following figure.
Example 3
Example: The alignment of a structure is determined by the member with the largest alignment, which is calculated as an integer multiple.
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
DeleteMemory 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.
DeleteOffset 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.