Aligned allocation and VC++.NET

  • C/C++
  • Thread starter Dissident Dan
  • Start date
In summary, aligning data types to 16 bytes using "__declspec(align(16))" in Visual Studio .NET 2003 may not work correctly when using the new operator. This can be resolved by overloading the new and delete operators with aligned malloc and free functions. Additional information and examples can be found in the help file supplied by the Intel C++ compiler and on websites such as CodeProject.
  • #1
Dissident Dan
238
2
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
  • #2
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:
  • #3
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:
  • #4
huh?
 
  • #5
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.
 

Related to Aligned allocation and VC++.NET

1. What is aligned allocation in VC++.NET?

Aligned allocation is a memory allocation technique in VC++.NET that ensures that the allocated memory address is aligned to a specific byte boundary. This helps improve the performance of the program by reducing the number of unaligned data accesses.

2. How is aligned allocation different from traditional memory allocation?

Traditional memory allocation in VC++.NET does not guarantee that the allocated memory address will be aligned to a specific boundary. Aligned allocation, on the other hand, allows developers to specify the desired alignment boundary, resulting in improved performance of the program.

3. How can aligned allocation be implemented in VC++.NET?

Aligned allocation can be implemented in VC++.NET by using the __declspec(align(#)) keyword before the type or declaration of the variable. This specifies the desired alignment boundary for that particular variable.

4. What are the benefits of using aligned allocation in VC++.NET?

Aligned allocation can improve the performance of a program by reducing the number of unaligned data accesses, which can result in faster execution times. It also helps avoid potential errors that may arise from accessing unaligned data.

5. Are there any drawbacks of using aligned allocation in VC++.NET?

One potential drawback of using aligned allocation in VC++.NET is that it may result in larger memory usage due to padding. This can be a concern for applications that have limited memory resources. Additionally, using aligned allocation may also complicate the code and make it less portable across different platforms.

Similar threads

  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
10
Views
6K
  • Programming and Computer Science
Replies
21
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
14
Views
7K
  • Programming and Computer Science
Replies
5
Views
3K
  • General Engineering
Replies
1
Views
4K
  • Programming and Computer Science
Replies
16
Views
4K
Back
Top