473,320 Members | 1,939 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,320 software developers and data experts.

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 help.

PS - I already have asked this in gcc group. Please refrain from
directing me towards other groups.
Jun 27 '08 #1
8 2679
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
Jun 27 '08 #2
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));

Jun 27 '08 #3
Antoninus Twink <no****@nospam.invalidwrites:
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.help;
they are not appropriate in comp.lang.c.

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

--
Keith Thompson (The_Other_Keith) ks***@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"
Jun 27 '08 #4
On Tue, 27 May 2008 13:02:33 -0700, Keith Thompson wrote:
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.
It's not silly if older systems required a particular alignment, newer
systems don't, but the compiler still inserts padding to maintain binary
compatibility with those older systems. It's good that implementors are
given a lot of freedom in choosing what works best for their systems.
Jun 27 '08 #5
Keith Thompson wrote:
Eric Sosman <Er*********@sun.comwrites:
>Keith Thompson wrote:
>>[...] It's also legal (but silly) for the compiler
to insert padding whether it's required for alignment or not.
The ambiguity lies in "requirement." On one popular
platform, for example, a `double' can be accessed at any
address divisible by four, but can be accessed more quickly
if the address is also divisible by eight. Should the
alignment "requirement" be taken as four or as eight?

Could be either. My point is that the compiler is allowed to insert,
say, 16 bytes of padding, even if it doesn't help performance at all.
... and my point was about the word "silly:" Until you
can nail down "requirement" you can't assess the silliness
of the padding scheme.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #6
Eric Sosman <es*****@ieee-dot-org.invalidwrites:
Keith Thompson wrote:
>Eric Sosman <Er*********@sun.comwrites:
>>Keith Thompson wrote:
[...] It's also legal (but silly) for the compiler
to insert padding whether it's required for alignment or not.
The ambiguity lies in "requirement." On one popular
platform, for example, a `double' can be accessed at any
address divisible by four, but can be accessed more quickly
if the address is also divisible by eight. Should the
alignment "requirement" be taken as four or as eight?
Could be either. My point is that the compiler is allowed to insert,
say, 16 bytes of padding, even if it doesn't help performance at all.

... and my point was about the word "silly:" Until you
can nail down "requirement" you can't assess the silliness
of the padding scheme.
Ok, good point.

To re-state my point more precisely, a compiler is allowed to insert
padding between struct members even if doing so has no benefit
whatsoever. (I don't know of any compilers that actually do so.)

--
Keith Thompson (The_Other_Keith) ks***@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"
Jun 27 '08 #7
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 help.

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 **
Jun 27 '08 #8
CBFalconer <cb********@yahoo.comwrites:
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 help.

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.
But the C standard imposes a number of requirements on how memory is
allocated for structures (as we already discussed in this thread).

--
Keith Thompson (The_Other_Keith) ks***@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"
Jun 27 '08 #9

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

Similar topics

9
by: Tzu-Chien Chiu | last post by:
Hi, "What methods do you ever use to optimize the programs?" We're developing a graphics chip emulator in C++, but it's very slow for big scenes. Even though this is a cross-platform software,...
4
by: J. Campbell | last post by:
From reading this forum, it is my understanding that C++ doesn't require the compiler to keep code that does not manifest itself in any way to the user. For example, in the following: { for(int...
21
by: simon | last post by:
From my previous post... If I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2; int iSomeNum1; int iSomeNum2;
11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
2
by: Jack | last post by:
I have a chunk of code that loads a few dozen function pointers into global variables. I'm concerned with unused memory consumption. What if the client only needs to use one or two functions? Then...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
9
by: uday | last post by:
Hi All, I need one clarification regarding memory allocation for structure. The details are as given below : let us consider one structure struct { uit32 len0; uint8 *pointer0; uit32 len1;
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.