C/C++ Aligned allocation and VC++.NET

  • Thread starter Thread starter Dissident Dan
  • Start date Start date
AI Thread Summary
The discussion revolves around issues with the new operator in Visual Studio .NET 2003, specifically regarding 16-byte alignment for data types when using SSE (Streaming SIMD Extensions). A user reports that dynamically allocating an array of a class designed for 16-byte alignment results in an 8-byte aligned pointer, which causes crashes during SSE operations. Attempts to enforce alignment using the "__declspec(align(16))" directive on both the class and the pointer have been unsuccessful. The user has resorted to using the C functions _aligned_malloc() and _aligned_free() for proper alignment. Suggestions include the possibility of needing to overload the new and delete operators to utilize aligned memory allocation functions. There are references to using __attribute__ (aligned(16)) in GCC and mentions of resources for further information on alignment and SSE programming.
Dissident Dan
Messages
236
Reaction score
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
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
8
Views
5K
Replies
10
Views
7K
Replies
30
Views
6K
Replies
14
Views
7K
Replies
4
Views
2K
Replies
7
Views
3K
Replies
5
Views
3K
Replies
16
Views
4K
Back
Top