472,993 Members | 3,191 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Regarding malloc memory alignment

Hi Group,

I have one question.

If I am allocating memory of 38 bytes to an integer pointer, what will
be the memory actually allocated?

Will there be any memory alignment concept in malloc?

Please let me know the reasons if it is yes / no.

Regards
Ram.
Dec 6 '07 #1
8 7217
ra********@gmail.com writes:
I have one question.
It looks like two questions to me.
If I am allocating memory of 38 bytes to an integer pointer, what will
be the memory actually allocated?
If you call malloc(38), you will get at least 38 bytes of memory
and possibly more (or no bytes of memory, if the request cannot
be satisfied). How much more depends on your implementation and
possibly on the preexisting state of the system.
Will there be any memory alignment concept in malloc?
Memory allocated with malloc is suitably aligned for any standard
C purpose. The C standard says this explicitly:

The pointer returned if the allocation succeeds is suitably
aligned so that it may be assigned to a pointer to any type
of object and then used to access such an object or an array
of such objects in the space allocated (until the space is
explicitly deallocated).
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
Dec 6 '07 #2
<ra********@gmail.comschrieb im Newsbeitrag
news:30**********************************@e25g2000 prg.googlegroups.com...
Hi Group,

I have one question.

If I am allocating memory of 38 bytes to an integer pointer, what will
be the memory actually allocated?
malloc will give you at least as many bytes as you asked it for or none at
all.
Will there be any memory alignment concept in malloc?
The memory you got will be properly aligned for whatever purpose you may
want to use it for.
Please let me know the reasons if it is yes / no.
Because the standard say so.
Regards
Ram.
Bye, Jojo
Dec 6 '07 #3
ra********@gmail.com wrote:
If I am allocating memory of 38 bytes to an integer pointer, what will
be the memory actually allocated?
You mean if you write

int *malloced = malloc( 38 );

in a function (with all suitable #includes and whatever)?

You will get a pointer to 38 bytes of (uninitialised) memory [1]. What this
means is that those 38 bytes can be written to and (then) read from,
but that trying to access bytes after (or before) those 38 gets you
undefined behaviour, as does computing the address of any bytes outside
the 38 (+1 for the Special Exception) [2].

I note that if sizeof(int) doesn't exactly divide 38, and you really wanted
to use the memory for ints, you'll have some useless bytes hanging around
at the end.
Will there be any memory alignment concept in malloc?
The Standard requires that the memory returned by malloc is suitably aligned
for any C data-type. How the implementation achieves this is left to it
(but each implementation of course knows what the alignment requirements
of its data-types are, so it's not hard to do, just not portable).

[1] Or null.

[2] The implementation of undefined behaviour need not /do/ anthing; it
could quite cheerfully read from random other variables (Bad) or
write to them (Very Extremely Horribly RunAway! Bad).

--
Chris "running is for the jetpackless" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Dec 7 '07 #4
On Dec 7, 3:34 pm, Chris Dollin <chris.dol...@hp.comwrote:
ramsati...@gmail.com wrote:
If I am allocating memory of 38 bytes to an integer pointer, what will
be the memory actually allocated?

You mean if you write

int *malloced = malloc( 38 );

in a function (with all suitable #includes and whatever)?

You will get a pointer to 38 bytes of (uninitialised) memory [1]. What this
means is that those 38 bytes can be written to and (then) read from,
but that trying to access bytes after (or before) those 38 gets you
undefined behaviour, as does computing the address of any bytes outside
the 38 (+1 for the Special Exception) [2].

I note that if sizeof(int) doesn't exactly divide 38, and you really wanted
to use the memory for ints, you'll have some useless bytes hanging around
at the end.
Will there be any memory alignment concept in malloc?

The Standard requires that the memory returned by malloc is suitably aligned
for any C data-type. How the implementation achieves this is left to it
(but each implementation of course knows what the alignment requirements
of its data-types are, so it's not hard to do, just not portable).

[1] Or null.

[2] The implementation of undefined behaviour need not /do/ anthing; it
could quite cheerfully read from random other variables (Bad) or
write to them (Very Extremely Horribly RunAway! Bad).

--
Chris "running is for the jetpackless" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
Hi All,

Thank you very much for your replies. But still I am confused how it
will align if I allocate 38 bytes because malloc never allocates more
memory for alignment.

Is this point right? - PLease correct me if I am wrong.

According to standard, if alignment is done, how can we make sure
that ?

Can you please show me with an example?

Thanks in advance,

Regards
Ram.

Dec 7 '07 #5
ra********@gmail.com said:

<snip>
But still I am confused how it
will align if I allocate 38 bytes because malloc never allocates more
memory for alignment.
The Standard offers no such guarantee.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dec 7 '07 #6
>Thank you very much for your replies. But still I am confused how it
>will align if I allocate 38 bytes because malloc never allocates more
memory for alignment.
malloc() never allocates *LESS* than the memory you requested (unless
it returns NULL, in which case it allocates nothing). Beyond that,
it is Not Your Problem (tm) and You Have No Stanard Way Of Checking
That So Don't Try(tm).
Is this point right? - PLease correct me if I am wrong.
No.

malloc() probably also allocates something (memory, disk space,
cash) to keep track of your allocation, even if there is no alignment
issue. It may allocate that as disk space in a SQL server in a
Microsoft Dungeon in Redmond. Or maybe in RAM near the memory it
gave you.
>According to standard, if alignment is done, how can we make sure
that ?
According to the standard, you are NOT ALLOWED TO TRY TO CHECK THIS.
>Can you please show me with an example?
I don't see the point in showing an example of NOT attempting to
check alignment of a pointer returned from malloc().

Dec 8 '07 #7
ra********@gmail.com wrote:
Thank you very much for your replies. But still I am confused how it
will align if I allocate 38 bytes because malloc never allocates more
memory for alignment.
You don't know that -- and it isn't true.
According to standard, if alignment is done, how can we make sure
that ?
We can't, not portably, which is why malloc is required to do it.

What actual problem are you trying to solve?

--
Concrete Is Better Than Tracts Hedgehog
"I just wonder when we're going to have to sit down and re-evaluate
our decision-making paradigm." /Sahara/

Dec 8 '07 #8
ra********@gmail.com wrote:
Chris Dollin <chris.dol...@hp.comwrote:
>ramsati...@gmail.com wrote:
>>If I am allocating memory of 38 bytes to an integer pointer,
what will be the memory actually allocated?

You mean if you write

int *malloced = malloc( 38 );

in a function (with all suitable #includes and whatever)?

You will get a pointer to 38 bytes of (uninitialised) memory [1].
What this means is that those 38 bytes can be written to and
(then) read from, but that trying to access bytes after (or
before) those 38 gets you undefined behaviour, as does computing
the address of any bytes outside the 38 (+1 for the Special
Exception) [2].

I note that if sizeof(int) doesn't exactly divide 38, and you
really wanted to use the memory for ints, you'll have some
useless bytes hanging around at the end.
>> Will there be any memory alignment concept in malloc?

The Standard requires that the memory returned by malloc is
suitably aligned for any C data-type. How the implementation
achieves this is left to it (but each implementation of course
knows what the alignment requirements of its data-types are, so
it's not hard to do, just not portable).

[1] Or null.

[2] The implementation of undefined behaviour need not /do/
anthing; it could quite cheerfully read from random other
variables (Bad) or write to them (Very Extremely Horribly
RunAway! Bad).

Thank you very much for your replies. But still I am confused
how it will align if I allocate 38 bytes because malloc never
allocates more memory for alignment.
Chris has summarized what the standard guarantees. Why and how is
not discussable, because malloc is necessarily a system dependant
function. It cannot be written in portable C, although you can get
fairly close. So your remaining questions can only be dealt with
for a specific implementation, and are thus off topic here on
c.l.c. What can be discussed here is how best to use it.

You can examine one implementation (if you like) at:

<http://cbfalconer.home.att.net/download/nmalloc.zip>

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 8 '07 #9

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

Similar topics

1
by: Joy | last post by:
CREATE TABLE my table (coll_a INT NOT NULL, coll_b SMALLINT, coll_c SMALLINT, INDEX b_index (coll_b) ); Above is the table I will created. I declare coll_b and coll_c, which together will...
13
by: sachin_mzn | last post by:
Hi, What is the concept of memory alignment? Is memory alignment differs, If a data type is local to a function or if it is a member of structure or union? How 32 to 64 bit processor afftects the...
1
by: rahul8143 | last post by:
hello, What is mean by memory alignment in context of structure padding? also why structure padding is useful? is it implicitly done by latest c,c++ compilers or i have to specify it explicitly?...
4
by: Dharma | last post by:
hi, struct a { byte d; byte buf; }text; text.buf = 'A' text.buf = '\0'
3
by: iduniq | last post by:
Hi, Good day! Anyone knows how to extract shorts, long, chars from a char array considering the memory alignment? It's like this, there's a char buffer, which is cast into a struct, then cast...
10
by: Francine.Neary | last post by:
I've been trying to understand the design decision to have one (well, three of course, but essentially one) allocation function that returns a pointer guaranteed to be aligned for any type. It...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.