PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Optimizing structure memory allocation
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Optimizing structure memory allocation

Réponse
 
LinkBack Outils de la discussion
Vieux 27/05/2008, 13h25   #1
rahul
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Optimizing structure memory allocation

How is the memory allocated for structures? I need to optimize the
memory usage and bit fields are not doing the trick.

Any details about the memory allocation for the structures would be a
great .

PS - I already have asked this in gcc group. Please refrain from
directing me towards other groups.
  Réponse avec citation
Vieux 27/05/2008, 13h44   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Optimizing structure memory allocation

rahul said:

> How is the memory allocated for structures?


"A structure type describes a sequentially allocated set of member
objects", says the Standard.

Later, it adds: "If the objects pointed to are members of the same
aggregate object, pointers to structure members declared later compare
higher than pointers to members declared earlier in the structure".

Finally, "There may also be unnamed padding at the end of a structure or
union, as necessary to achieve the appropriate alignment were the
structure or union to be a member of an array."

<snip>

> PS - I already have asked this in gcc group. Please refrain from
> directing me towards other groups.


Gladly, if you are happy to accept that the answers you get here will be
related to what the language guarantees, rather than which particular
choice an implementation might make where the language offers such a
choice.

--
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
  Réponse avec citation
Vieux 27/05/2008, 13h53   #3
Richard Tobin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Optimizing structure memory allocation

In article <859569b1-e7e0-4711-969e-8960bc5df728@i36g2000prf.googlegroups.com>,
rahul <rahulsinner@gmail.com> wrote:

>How is the memory allocated for structures? I need to optimize the
>memory usage and bit fields are not doing the trick.
>
>Any details about the memory allocation for the structures would be a
>great .


You'll probably get better if you ask a more specific question.
Show us what you want to put in the structure.

The members of a struct are stored in the order you specify. Some
members may have types that require a particular alignment, typically
equal to their size. For example, while chars can go anywhere you may
find that if ints are 4 bytes long then they are always placed on
4-byte boundaries. This is common even if the processor allows
arbitrary alignment, because it's usually faster to access
suitably-aligned data.

Suppose shorts are 2 bytes and ints are 4, with corresponding
alignment requirements, and you want to store 2 chars, a short, and an
int in your struct. These could fit in 8 bytes, and will if you order
them correctly, for example

struct foo {
int a;
short b;
char c, d;
};

but if you do

struct foo2 {
char c;
int a;
char d;
short b;
};

it will take 12 bytes, because the 3 bytes after c and the byte after d
are wasted.

If you use bitfields, then you have to consider similar issues at the
bit level. Adjacent bitfields that fit within a "unit" (probably an
int) will be packed together, so don't spread them out amongst other
members of the struct, and try to order them so that they don't go
over too many int boundaries.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
  Réponse avec citation
Vieux 27/05/2008, 19h41   #4
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Optimizing structure memory allocation

rahul wrote:
>
> How is the memory allocated for structures? I need to optimize
> the memory usage and bit fields are not doing the trick.
>
> Any details about the memory allocation for the structures would
> be a great .
>
> PS - I already have asked this in gcc group. Please refrain from
> directing me towards other groups.


If you are using gcc, then a gcc group is appropriate. This one is
not. A compiler system can use any method of memory allocation it
pleases, so long as it works.

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

** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 27/05/2008, 20h18   #5
Antoninus Twink
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Optimizing structure memory allocation

On 27 May 2008 at 12:25, rahul wrote:
> How is the memory allocated for structures? I need to optimize the
> memory usage and bit fields are not doing the trick.
>
> PS - I already have asked this in gcc group. Please refrain from
> directing me towards other groups.


You might want to look at gcc's packed attribute, which "specifies that
a variable or structure field should have the smallest possible
alignment - one byte for a variable, and one bit for a field, unless you
specify a larger value with the aligned attribute."

You can either pack specific fields, e.g.

struct foo {
char a;
int b __attribute__((packed));
};

or whole structs at once, e.g.

struct bar {
int a;
char b;
int c;
char d;
int e;
} __attribute__((packed));

  Réponse avec citation
Vieux 27/05/2008, 20h33   #6
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Optimizing structure memory allocation

Antoninus Twink <nospam@nospam.invalid> writes:
> On 27 May 2008 at 12:25, rahul wrote:
>> How is the memory allocated for structures? I need to optimize the
>> memory usage and bit fields are not doing the trick.
>>
>> PS - I already have asked this in gcc group. Please refrain from
>> directing me towards other groups.

>
> You might want to look at gcc's packed attribute,

[...]

which is, of course, off-topic in comp.lang.c.

rahul, you asked us not to direct you to other groups. Why?
Questions about gcc-specific features are appropriate in gnu.gcc.;
they are not appropriate in comp.lang.c.

Or you can consult the extensive documentation that comes with gcc.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 27/05/2008, 20h44   #7
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Optimizing structure memory allocation

In article <g1h08r$1c9f$1@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <richard@cogsci.ed.ac.uk> wrote:
>In article <859569b1-e7e0-4711-969e-8960bc5df728@i36g2000prf.googlegroups.com>,
>rahul <rahulsinner@gmail.com> wrote:


>>How is the memory allocated for structures? I need to optimize the
>>memory usage and bit fields are not doing the trick.


>The members of a struct are stored in the order you specify.


Expanding slightly on Richard's answer for emphasis:

C *requires* that structure members be placed in the order given,
and in increasing address order in memory. Storing the members in
the order coded is not just a matter of convention: it is part
of the language definition. As is the fact that alignment restrictions
exist and are adhered to by the compiler by adding unnamed padding
between structure members if needed (or advised) by the target machine.
--
"All is vanity." -- Ecclesiastes
  Réponse avec citation
Vieux 27/05/2008, 21h02   #8
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Optimizing structure memory allocation

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
> In article <g1h08r$1c9f$1@pc-news.cogsci.ed.ac.uk>,
> Richard Tobin <richard@cogsci.ed.ac.uk> wrote:
>>In article <859569b1-e7e0-4711-969e-8960bc5df728@i36g2000prf.googlegroups.com>,
>>rahul <rahulsinner@gmail.com> wrote:

>
>>>How is the memory allocated for structures? I need to optimize the
>>>memory usage and bit fields are not doing the trick.

>
>>The members of a struct are stored in the order you specify.

>
> Expanding slightly on Richard's answer for emphasis:
>
> C *requires* that structure members be placed in the order given,
> and in increasing address order in memory. Storing the members in
> the order coded is not just a matter of convention: it is part
> of the language definition. As is the fact that alignment restrictions
> exist and are adhered to by the compiler by adding unnamed padding
> between structure members if needed (or advised) by the target machine.


Right, but it's perfectly legal for the alignment restriction to be
"any alignment is ok". It's also legal (but silly) for the compiler
to insert padding whether it's required for alignment or not.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h11.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13436 seconds with 16 queries