Extension for Visual Studio Code for text alignment inspired by Atom Aligment. Uses node-alignment. Supports text alignment and multi-cursor alignment. Code alignment is the practice of formatting your code vertically to improve readability. Based on principles borrowed from mathematics and other disciplines, code alignment gives extra meaning to your code by lining up similar data in columns. Align Vertically Extension Aligns each line of the selected text by a chosen keyword. Highlight some text, right click and select 'Align Vertically' from the menu.
-->In Visual Studio 2015 and later, use the C++11 standard alignas
specifier to control alignment. For more information, see Alignment.
Microsoft Specific
About Press Copyright Contact us Creators Advertise Developers Terms Privacy Policy & Safety How YouTube works Test new features Press Copyright Contact us Creators. What: Lets you wrap and align chains of method calls. When: You have a long chain consisting of several method calls in one statement. Why: Reading a long list is easier when they're wrapped or indented according to user preference. Place your cursor in any of the call chains.
Use __declspec(align(#))
to precisely control the alignment of user-defined data (for example, static allocations or automatic data in a function).
Syntax
__declspec( align(#) )declarator
Remarks
Writing applications that use the latest processor instructions introduces some new constraints and issues. Many new instructions require data that's aligned to 16-byte boundaries. Additionally, by aligning frequently used data to the processor's cache line size, you improve cache performance. For example, if you define a structure whose size is less than 32 bytes, you may want 32 byte alignment to make sure that objects of that structure type are efficiently cached.
# is the alignment value. Valid entries are integer powers of two from 1 to 8192 (bytes), such as 2, 4, 8, 16, 32, or 64. declarator
is the data that you're declaring as aligned.
For information about how to return a value of type size_t
that is the alignment requirement of the type, see alignof
. For information about how to declare unaligned pointers when targeting 64-bit processors, see __unaligned
.
You can use __declspec(align(#))
when you define a struct
, union
, or class
, or when you declare a variable.
The compiler doesn't guarantee or attempt to preserve the alignment attribute of data during a copy or data transform operation. For example, memcpy
can copy a struct declared with __declspec(align(#))
to any location. Ordinary allocators (for example, malloc
, C++ operator new
, and the Win32 allocators) typically return memory that isn't sufficiently aligned for __declspec(align(#))
structures or arrays of structures. To guarantee that the destination of a copy or data transformation operation is correctly aligned, use _aligned_malloc
. Or, write your own allocator.
You can't specify alignment for function parameters. When you pass data that has an alignment attribute by value on the stack, its alignment is controlled by the calling convention. If data alignment is important in the called function, copy the parameter into correctly aligned memory before use.
Without __declspec(align(#))
, the compiler generally aligns data on natural boundaries based on the target processor and the size of the data, up to 4-byte boundaries on 32-bit processors, and 8-byte boundaries on 64-bit processors. Data in classes or structures is aligned in the class or structure at the minimum of its natural alignment and the current packing setting (from #pragma pack
or the /Zp
compiler option).
This example demonstrates the use of __declspec(align(#))
:
This type now has a 32-byte alignment attribute. It means that all static and automatic instances start on a 32-byte boundary. Additional structure types declared with this type as a member preserve this type's alignment attribute, that is, any structure with Str1
as an element has an alignment attribute of at least 32.
Here, sizeof(struct Str1)
is equal to 32. It implies that if an array of Str1
objects is created, and the base of the array is 32-byte aligned, each member of the array is also 32-byte aligned. To create an array whose base is correctly aligned in dynamic memory, use _aligned_malloc
. Or, write your own allocator.
The sizeof
value for any structure is the offset of the final member, plus that member's size, rounded up to the nearest multiple of the largest member alignment value or the whole structure alignment value, whichever is larger.
The compiler uses these rules for structure alignment:
Unless overridden with
__declspec(align(#))
, the alignment of a scalar structure member is the minimum of its size and the current packing.Unless overridden with
__declspec(align(#))
, the alignment of a structure is the maximum of the individual alignments of its member(s).A structure member is placed at an offset from the start of its parent structure that's the smallest multiple of its alignment greater than or equal to the offset of the end of the previous member.
The size of a structure is the smallest multiple of its alignment greater than or equal to the offset of the end of its last member.
__declspec(align(#))
can only increase alignment restrictions.
For more information, see:
Examples of Structure Alignment (x64 specific)
align Examples
The following examples show how __declspec(align(#))
affects the size and alignment of data structures. The examples assume the following definitions:
Visual Studio Format Selection
In this example, the S1
structure is defined by using __declspec(align(32))
. All uses of S1
for a variable definition or in other type declarations are 32-byte aligned. sizeof(struct S1)
returns 32, and S1
has 16 padding bytes following the 16 bytes required to hold the four integers. Each int
member requires 4-byte alignment, but the alignment of the structure itself is declared to be 32. Then the overall alignment is 32.
In this example, sizeof(struct S2)
returns 16, which is exactly the sum of the member sizes, because that is a multiple of the largest alignment requirement (a multiple of 8).
In the following example, sizeof(struct S3)
returns 64.
In this example, notice that a
has the alignment of its natural type, in this case, 4 bytes. However, S1
must be 32-byte aligned. 28 bytes of padding follow a
, so that s1
starts at offset 32. S4
then inherits the alignment requirement of S1
, because it's the largest alignment requirement in the structure. sizeof(struct S4)
returns 64.
The following three variable declarations also use __declspec(align(#))
. In each case, the variable must be 32-byte aligned. In the array, the base address of the array, not each array member, is 32-byte aligned. The sizeof
value for each array member is unaffected when you use __declspec(align(#))
.
To align each member of an array, code such as this should be used:
In this example, notice that aligning the structure itself and aligning the first element have the same effect:
S6
and S7
have identical alignment, allocation, and size characteristics.
In this example, the alignment of the starting addresses of a, b, c, and d are 4, 1, 4, and 1, respectively.
The alignment when memory is allocated on the heap depends on which allocation function is called. For example, if you use malloc
, the result depends on the operand size. If arg >= 8, the memory returned is 8 byte aligned. If arg < 8, the alignment of the memory returned is the first power of 2 less than arg. For example, if you use malloc(7)
, the alignment is 4 bytes.
Defining new types with __declspec(align(#))
You can define a type with an alignment characteristic.

For example, you can define a struct
with an alignment value this way:
Now, aType
and bType
are the same size (8 bytes) but variables of type bType
are 32-byte aligned.
Aligning data in thread local storage

Static thread-local storage (TLS) created with the __declspec(thread)
attribute and put in the TLS section in the image works for alignment exactly like normal static data. To create TLS data, the operating system allocates memory the size of the TLS section and respects the TLS section alignment attribute.
This example shows various ways to place aligned data into thread local storage.
How align
works with data packing
The /Zp
compiler option and the pack
pragma have the effect of packing data for structure and union members. This example shows how /Zp
and __declspec(align(#))
work together:
The following table lists the offset of each member under different /Zp
(or #pragma pack
) values, showing how the two interact.
Variable | /Zp1 | /Zp2 | /Zp4 | /Zp8 |
---|---|---|---|---|
a | 0 | 0 | 0 | 0 |
b | 1 | 2 | 2 | 2 |
c | 3 | 4 | 4 | 8 |
d | 32 | 32 | 32 | 32 |
e | 40 | 40 | 40 | 40 |
f | 41 | 42 | 44 | 48 |
sizeof(S) | 64 | 64 | 64 | 64 |
For more information, see /Zp
(Struct Member Alignment).
The offset of an object is based on the offset of the previous object and the current packing setting, unless the object has a __declspec(align(#))
attribute, in which case the alignment is based on the offset of the previous object and the __declspec(align(#))
value for the object.
END Microsoft Specific
See also
__declspec
Overview of ARM ABI Conventions
x64 software conventions

Applies to: SQL Server (all supported versions) - Linux
This article shows how to use the mssql extension for Visual Studio Code to develop SQL Server databases. Because Visual Studio Code is cross-platform, you can use mssql extension on Linux, macOS, and Windows.
Install and start Visual Studio Code
Visual Studio Code is a cross-platform, graphical code editor that supports extensions.
Download and install Visual Studio Code on your machine.
Start Visual Studio Code.
Note
If Visual Studio Code does not start when you are connected through an xrdp remote desktop session, see VS Code not working on Ubuntu when connected using XRDP.
Install the mssql extension
The mssql extension for Visual Studio Code lets you connect to a SQL Server, query with Transact-SQL (T-SQL), and view the results.
In Visual Studio Code, select View > Command Palette, or press Ctrl+Shift+P, or press F1 to open the Command Palette.
In the Command Palette, select Extensions: Install Extensions from the dropdown.
In the Extensions pane, type mssql.
Select the SQL Server (mssql) extension, and then select Install.
After the installation completes, select Reload to enable the extension.
Create or open a SQL file
The mssql extension enables mssql commands and T-SQL IntelliSense in the code editor when the language mode is set to SQL.
Select File > New File or press Ctrl+N. Visual Studio Code opens a new Plain Text file by default.
Select Plain Text on the lower status bar, or press Ctrl+K > M, and select SQL from the languages dropdown.
Note
If this is the first time you have used the extension, the extension installs supporting SQL Server tools.
If you open an existing file that has a .sql file extension, the language mode is automatically set to SQL.
Align Code In Visual Studio Shortcut
Connect to SQL Server
Follow these steps to create a connection profile and connect to a SQL Server.
Press Ctrl+Shift+P or F1 to open the Command Palette.
Type sql to display the mssql commands, or type sqlcon, and then select MS SQL: Connect from the dropdown.
Note
A SQL file, such as the empty SQL file you created, must have focus in the code editor before you can execute the mssql commands.
Select the MS SQL: Manage Connection Profiles command.
Then select Create to create a new connection profile for your SQL Server.
Follow the prompts to specify the properties for the new connection profile. After specifying each value, press Enter to continue.
Connection property Description Server name or ADO connection string Specify the SQL Server instance name. Use localhost to connect to a SQL Server instance on your local machine. To connect to a remote SQL Server, enter the name of the target SQL Server, or its IP address. To connect to a SQL Server container, specify the IP address of the container's host machine. If you need to specify a port, use a comma to separate it from the name. For example, for a server listening on port 1401, enter <servername or IP>,1401
.
As an alternative, you can enter the ADO connection string for your database here.Database name (optional) The database that you want to use. To connect to the default database, don't specify a database name here. Authentication Type Choose either Integrated or SQL Login. User name If you selected SQL Login, enter the name of a user with access to a database on the server. Password Enter the password for the specified user. Save Password Press Enter to select Yes and save the password. Select No to be prompted for the password each time the connection profile is used. Profile Name (optional) Type a name for the connection profile, such as localhost profile. After you enter all values and select Enter, Visual Studio Code creates the connection profile and connects to the SQL Server.
Tip
If the connection fails, try to diagnose the problem from the error message in the Output panel in Visual Studio Code. To open the Output panel, select View > Output. Also review the connection troubleshooting recommendations.
Verify your connection in the lower status bar.
As an alternative to the previous steps, you can also create and edit connection profiles in the User Settings file (settings.json). To open the settings file, select File > Preferences > Settings. For more information, see Manage connection profiles.
Create a SQL database
In the new SQL file that you started earlier, type sql to display a list of editable code snippets.
Select sqlCreateDatabase.
In the snippet, type
TutorialDB
to replace 'DatabaseName':Press Ctrl+Shift+E to execute the Transact-SQL commands. View the results in the query window.
Tip
You can customize the shortcut keys for the mssql commands. See Customize shortcuts.
Create a table
Delete the contents of the code editor window.
Press Ctrl+Shift+P or F1 to open the Command Palette.
Type sql to display the mssql commands, or type sqluse, and then select the MS SQL: Use Database command.
Select the new TutorialDB database.
In the code editor, type sql to display the snippets, select sqlCreateTable, and then press Enter.
In the snippet, type
Employees
for the table name.Press Tab to get to the next field, and then type
dbo
for the schema name.Replace the column definitions with the following columns:
Press Ctrl+Shift+E to create the table.
Insert and query
Add the following statements to insert four rows into the Employees table.
While you type, T-SQL IntelliSense helps you to complete the statements:
Tip
The mssql extension also has commands to help create INSERT and SELECT statements. These were not used in the previous example.
Press Ctrl+Shift+E to execute the commands. The two result sets display in the Results window.
View and save the result
Select View > Editor Layout > Flip Layout to switch to a vertical or horizontal split layout.
Select the Results and Messages panel headers to collapse and expand the panels.
Tip
You can customize the default behavior of the mssql extension. See Customize extension options.
Select the maximize grid icon on the second result grid to zoom in to those results.
Note
The maximize icon displays when your T-SQL script produces two or more result grids.
Open the grid context menu by right-clicking on the grid.
Select Select All.
Open the grid context menu again and select Save as JSON to save the result to a .json file.
Specify a file name for the JSON file.
Verify that the JSON file saves and opens in Visual Studio Code.
If you need to save and run SQL scripts later, for administration or a larger development project, save the scripts with a .sql extension.
Next steps
If you're new to T-SQL, see Tutorial: Write Transact-SQL statements and the Transact-SQL Reference (Database Engine).
For more information on using or contributing to the mssql extension, see the mssql extension project wiki.
For more information on using Visual Studio Code, see the Visual Studio Code documentation.
