473,387 Members | 1,611 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,387 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 2332
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.