Memory footprint of a structure of structures

  • Thread starter Jean-Michel Hautbois
  • Start date
I

Ian Collins

"nm" gives you some help although not what OP asked for.

"gprof" gives you some help although not what OP asked for.

Which OP?
You can try something like:

#define malloc(SZ) ((SZ)< 512 ? malloc_some_small_thing(SZ) :
(SZ)< 8192 ? malloc_some_medium_thing(SZ) :
malloc_some_big_thing(SZ))

Which will fail as soon as you include <stdlib.h>
 
A

Ark

Note the use of the word "not".

Note the use of the word "cannot".
Well, sizeof, unless applied to a variable-length array, yields an
integer constant. Which AFAIK, can be used e.g. as array bound. That's
how I understood the OP's "would like, at compile time ideally, [�], to
know the footprint in memory" requirement.

I agree that, when VLAs are not involved, the compiler has sufficient
information to produce the result that he wants; I'm just letting him
know the C standard doesn't mandate support for any feature that would

Note the use of the word "doesn't".
...
Taking both of your remarks into account, I am understanding that
#error with sizeof can produce the output I want at compile time.

I curious how you reached that conclusion, because what I said was
almost precisely the opposite. Well, not exactly - the standard doesn't
mandate support for any features that could be counted on to produce the
result that you want at compile time, but neither does it prohibit them.
I of course goofed up in my previous post and James (thanks!) corrected
me alright.
What I meant (and what works) is to manually create a bunch of const
size_t objects initialized with sizes of your structures, using sizeof,
offsetof and other goodies as necessary, as long as they yield integer
constant expression (6.6p6). You can compile them in conditionally
(#ifdef MYDEBUG) and print them out or inspect in the debugger at runtime.
If I am guessing it right that you want to ensure that some size is
equal to another: that "some size" can be coded as sizeof(another).
 
J

Jorgen Grahn

You write a one-line program, or one-line extension, to simply print the
static size, of the top-most struct.

I'm often cross-compiling and/or doing experimental changes to the
data structures which break other parts of the code ... so that
solution isn't the first I think of.
But OP wants the sizes to be displayed in a tree-format, without the effort
of writing, and having to maintain in parallel, a long sequence of
printf/sizeof lines.

Of course he does. I want that too.

/Jorgen
 
J

Joe keane

Which will fail as soon as you include <stdlib.h>

No, i found it useful, maybe someone else will find it useful as well.

Of course it is extra-legal; so is using "gprof" since there is no such
thing in the standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top