Aligned allocation and VC++.NET

  • Context: C/C++ 
  • Thread starter Thread starter Dissident Dan
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on the issue of 16-byte alignment when using the new operator in Visual Studio .NET 2003 for classes that utilize SSE intrinsics. The user reports that despite using the "__declspec(align(16))" directive, the dynamically allocated pointer remains 8-byte aligned, leading to crashes. The user has found a workaround by employing the C functions _aligned_malloc() and _aligned_free() and suggests that overloading the new and delete operators may be necessary for proper alignment. References to alternative methods in GCC and additional resources are provided for further exploration.

PREREQUISITES
  • Understanding of C++ memory management and dynamic allocation
  • Familiarity with SSE (Streaming SIMD Extensions) and its requirements
  • Knowledge of Visual Studio .NET 2003 and its specific alignment directives
  • Experience with C-style memory allocation functions like _aligned_malloc()
NEXT STEPS
  • Research how to overload new and delete operators in C++ for custom memory alignment
  • Explore the use of __attribute__((aligned(16))) in GCC for memory alignment
  • Investigate the implications of memory alignment on performance in SSE applications
  • Review the Intel C++ Compiler documentation for alignment examples and best practices
USEFUL FOR

Developers working with C++ in Visual Studio .NET, particularly those implementing SSE intrinsics and requiring precise memory alignment for performance optimization.

Dissident Dan
Messages
236
Reaction score
1
Has anyone else experience the new operator not working correctly for 16-byte aligned data types? I am using Visual Studio .NET 2003.

I have a class that has its first component 16 byte-aligned via the "__declspec(align(16))" directive because I am using SSE.

Code:
class Vector {
public:
// data members
union {
__declspec(align(16)) float comps[4];
struct {
float x,y,z,w;
};
struct {
float r, g, b, a;
};
struct {
float yaw, pitch, roll;
};
};

When dynamically allocating and array of this class using the new operator, I get an 8 byte-aligned pointer. It needs to be 16 byte-aligned to not crash when using SSE intrinsics.

I have even tried using "__declspec(align(16))" on the pointer and between the "new" and the data type. Example:

Code:
__declspec(align(16)) Vector *tan1 = new __declspec(align(16)) Vector[vertexCount * 2];

I have resorted to using the old C functions _aligned_malloc() and _aligned_free().

This must be a bug with Visual Studio, right? Anyone know of any fixes?
 
Last edited:
Technology news on Phys.org
You're probably going to need to post this on a microsoft newsgroup or forum. I'm not familiar with MS specific code. In gcc __attribute__ (aligned(16)) is used.

[edit] This is just a guess, but when using __attribute__ I believe it goes after the allocation statement. Something like this:

float comps[4] __attribute__ (aligned(16));

This might be the same case with __delcspec
 
Last edited:
It turns out that you need to overload the new and delete operators using aligned malloc and free functions.

Check out this PDF for more info:
http://homepage.te.hik.se/personal/tkama/audio_procME/papers/17664_alignment.pdf"
 
Last edited by a moderator:
huh?
 
The help file( CHM ) supplied by intel c++ compiler hase some example on this topic. Also, www.codeproject.com hase some articles to introduce SSE.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
7K
  • · Replies 30 ·
2
Replies
30
Views
6K
  • · Replies 14 ·
Replies
14
Views
7K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 16 ·
Replies
16
Views
5K