Aligned allocation and VC++.NET

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

Discussion Overview

The discussion focuses on issues related to memory alignment when using the new operator for 16-byte aligned data types in Visual Studio .NET 2003, particularly in the context of SSE (Streaming SIMD Extensions) programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Experimental/applied

Main Points Raised

  • One participant reports that the new operator does not provide a 16-byte aligned pointer for a class designed for SSE, despite using the "__declspec(align(16))" directive.
  • Another participant suggests that the issue may be specific to Microsoft and recommends posting on a Microsoft newsgroup or forum, noting that GCC uses "__attribute__ (aligned(16))" instead.
  • A different participant proposes overloading the new and delete operators to use aligned malloc and free functions as a potential solution.
  • One participant mentions resources from the Intel C++ compiler help file and CodeProject for further information on SSE and alignment issues.

Areas of Agreement / Disagreement

Participants express differing views on the nature of the problem and potential solutions, indicating that there is no consensus on the best approach to resolve the alignment issue.

Contextual Notes

Some participants reference specific compiler behaviors and functions that may not be universally applicable, highlighting potential limitations in the discussion regarding compiler-specific features and alignment requirements.

Dissident Dan
Messages
244
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
8K
  • · Replies 14 ·
Replies
14
Views
8K
  • · 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