473,241 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

A way to get aligned allocated memory?


Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without excessive
memory allocation.
Jul 23 '05 #1
7 2326
serikas wrote:
Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without excessive
memory allocation.


The answer is "no" because there are no mechanisms in the C++ _language_
that control alignment. There may be mechanisms provided by your compiler
or your OS, but they are not part of the language.

V
Jul 23 '05 #2
serikas wrote:
Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.) For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment? Of course I know that it is possible by allocating twice the requested size or more, but I would like to know if it is possible without excessive memory allocation.


The answer is "yes" but there are no mechanisms in the C++ _language_
that control alignment. There are mechanisms provided by your compiler
or your OS and there are some very portable general solutions. The C++
Standard: "The alignment of a complete object type is an
implementation-defined integer value representing a number of bytes; an
object is allocated at an address that meets the alignment requirements
of its object type."

A.

Jul 23 '05 #3
"serikas" <se*****@hotmail.com> wrote

Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without excessive
memory allocation.


If all items are same size then I would solve it this way:
Initially (in ctor of your allocator class) allocate a twice large
block and find the right aligned offset position (adress) for
the first. And, from then on use realloc on the memory block
and keep a counter or the total lenght.
But that means: only the allocator can release the memory block,
and you cannot physically delete released gaps, so use a flag
in the data hdr to indicate whether its free or not, and reuse them.
Jul 23 '05 #4

<ab*********@spambob.com> skrev i en meddelelse
news:11**********************@f14g2000cwb.googlegr oups.com...
serikas wrote:
Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the

requested
size or more, but I would like to know if it is possible without

excessive
memory allocation.


The answer is "yes" but there are no mechanisms in the C++ _language_
that control alignment. There are mechanisms provided by your compiler
or your OS and there are some very portable general solutions. The C++
Standard: "The alignment of a complete object type is an
implementation-defined integer value representing a number of bytes; an
object is allocated at an address that meets the alignment requirements
of its object type."

A.


Yes?? How? Of course you can allocate aligned depending on the requirement
of some type, but I doubt you can have alignment guaranteed to be some power
of 2.

/Peter
Jul 23 '05 #5

"Uenal Mutlu" <52***************@t-online.de> skrev i en meddelelse
news:d4*************@news.t-online.com...
"serikas" <se*****@hotmail.com> wrote

Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without
excessive
memory allocation.


If all items are same size then I would solve it this way:
Initially (in ctor of your allocator class) allocate a twice large
block and find the right aligned offset position (adress) for
the first. And, from then on use realloc on the memory block
and keep a counter or the total lenght.
But that means: only the allocator can release the memory block,
and you cannot physically delete released gaps, so use a flag
in the data hdr to indicate whether its free or not, and reuse them.

That will not work. realloc will typically move the block.

/Peter
Jul 23 '05 #6
"Peter Koch Larsen" wrote
"Uenal Mutlu"
"serikas" wrote

Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)

For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the requested
size or more, but I would like to know if it is possible without
excessive
memory allocation.


If all items are same size then I would solve it this way:
Initially (in ctor of your allocator class) allocate a twice large
block and find the right aligned offset position (adress) for
the first. And, from then on use realloc on the memory block
and keep a counter or the total lenght.
But that means: only the allocator can release the memory block,
and you cannot physically delete released gaps, so use a flag
in the data hdr to indicate whether its free or not, and reuse them.

That will not work. realloc will typically move the block.


You are right, a bad<tm> idea of mine.
The idea would only function for a fixed nbr of items preallocated
with just one alloc request, and of course no realloc'ing afterwards.
Jul 23 '05 #7
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:Qm*****************@news000.worldonline.dk...

<ab*********@spambob.com> skrev i en meddelelse
news:11**********************@f14g2000cwb.googlegr oups.com...
serikas wrote:
Is there a way to get aligned dynamically allocated memory?
(provided that the requested memory size is a power of 2.)
For example, if I request 128 bytes of memory, can I implement
an allocator that allocates 128 bytes with 128-byte alignment?

Of course I know that it is possible by allocating twice the

requested
size or more, but I would like to know if it is possible without

excessive
memory allocation.


The answer is "yes" but there are no mechanisms in the C++ _language_
that control alignment. There are mechanisms provided by your compiler
or your OS and there are some very portable general solutions. The C++
Standard: "The alignment of a complete object type is an
implementation-defined integer value representing a number of bytes; an
object is allocated at an address that meets the alignment requirements
of its object type."

A.


Yes?? How? Of course you can allocate aligned depending on the requirement
of some type, but I doubt you can have alignment guaranteed to be some

power of 2.

/Peter


This post may be beneath you, but I always add whatever memory is necessary
to make my structure fill out the word. So, if my structure begins with a
short, I add a padding of two bytes before or after the first element.

Hope this helps.

- dan
Jul 23 '05 #8

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

Similar topics

6
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always...
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
11
by: Divick | last post by:
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...
9
by: Spoon | last post by:
Hello everyone, As far as I understand, if I request a uint8_t buffer, it could be allocated anywhere. uint8_t *buf = new uint8_t By anywhere, I mean e.g. it could start at an odd address....
29
by: K. Jennings | last post by:
I would like to get the result of malloc() such that it is aligned on a given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will u64 *p = malloc(N) ; do the trick in general? ...
15
by: shaanxxx | last post by:
why malloc (allocator) guarantees that address return by them will be aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?
6
by: Praetorian | last post by:
This is actually 2 questions: The first one: I have a function (FuncA) calling another (FuncB) with a set of parameters which also includes an int * initialized to NULL before the call. Now...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.