473,785 Members | 2,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to allocate memory which is 2^k aligned

Hi,
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?

The condition is such that there should not be any memory leak and it
should not also waste memory.

Thanks,
Divick

Sep 5 '06
11 3191
dasjotre wrote:
<snip>

sorry, I was beeing lazy ...

template <std::size_t Size, std::size_t Align>
class aligned_str
{
boost::aligned_ storage<Size, Alignaligned_;

public:

void * operator new(std::size_t )
{
typedef aligned_str<Siz e, AllignT;
return malloc(sizeof(T ));
}

void operator delete(void *p)
{
free(p);
}

void * address(){ return aligned_.addres s(); }
};

typedef aligned_str<Siz e, Alignmy_aligned ;
::std::auto_ptr <my_alignedp (new my_aligned);
// and use
p->address();

Sep 7 '06 #11
dasjotre wrote:
Divick wrote:
>>Hi,
can somebody help me figure out how can I make write a function
which inturn uses malloc routine to allocate memory which is 2^k
aligned?

The condition is such that there should not be any memory leak and it
should not also waste memory.


you could try something like

template <std::size_t Size, std::size_t Align>
struct aligned_str
{
boost::aligned_ storage<Size, Alignaligned_;
};

typedef aligned_str<Siz e, Alignmy_aligned ;
my_aligned * p = static_cast<my_ aligned*>(mallo c(sizeof(my_ali gned));
// and use
p->aligned_.addre ss();
I don't think that will work. aligned_storage is basically a union of a
char array of the appropriate size and some other type with suitable
alignment. For TR1 we made it clear that, essentially, the value of
Align must be a value that is the alignment of some type. It doesn't
handle arbitrary alignment values.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
For more information about this book, see www.petebecker.com/tr1book.
Sep 7 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
4682
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the values in (as would be required if I used the hideous, evil, much-abused #define :) ----------- const int constantA = 10; static const int constantB = 20;
4
1763
by: alan | last post by:
Dear all, I am a newbie in C++. If I want to allocate memory to objects, I will use double *d = new double instead of ANSI-C malloc. But if I want to resize, say enlarge num of objects to 20, how can I do it? In ANSI-C, I can use realloc to do this.
4
2591
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on Solaries OS? This means that I use new to allocate memory in one Thread and doesn't use delete to release them.
4
5004
by: marora | last post by:
I have created class definition which contains a charater pointer as one of it's data memeber. The objective is to read some data from a file, and assign it to a data member; Size of data is not known in the begining. We can assume that it will not exceed 256; class definition:
13
9727
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
14
2642
by: raghu | last post by:
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately
5
1789
by: raghu | last post by:
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately
0
1951
by: Varav | last post by:
We have a simple web service for DB2 to consume. I have tested the SOAPHTTPV and it is pretty slick. It is great that a database can do this. I first tested single requests over and over by hand and that works fine. However, we could easily have multiple requests being made right after each other or at the same time. To test this scenerio, I called the SOAPHTTPV method by doing: select db2xml.soaphttpv...
1
2184
by: darkdai | last post by:
hey everyone im trying to write a code that will have different functions which will allocate memory, de-allocate, reallocate etc. this is my code #include<stdio.h> #include<stdlib.h> void main() { void allocate(int *ptr); void reallocate(int *ptr); void ffr(int *ptr); int ans=0; int *ptr;
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.