Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Memory footprint of a structure of structures

Reply
Thread Tools

Memory footprint of a structure of structures

 
 
Jean-Michel Hautbois
Guest
Posts: n/a
 
      11-23-2011
Hi,

I have a (big) structure, which contains other structures with
(sometimes) big buffers.

Something like :

#define BUF_SIZE 65536
#define BUF_SIZE2 32768

typedef struct {
unsigned char buffer1[BUF_SIZE];
unsigned char buffer2[BUF_SIZE];
} t_buffer_head;

typedef struct {
unsigned char buffer1[BUF_SIZE2];
unsigned char buffer2[BUF_SIZE2];
} t_buffer_subhead;

typedef struct {
t_buffer_head header;
t_buffer_subhead subheader;
} t_buffer_s;

In my use case, I have >100 structures of t_buffer_something and one
big structure of these structures.
I would like, at compile time ideally, or by a static analysis script,
to know the footprint in memory of the big structure (here,
t_buffer_s).

Something like :

t_buffer_s (196608 bytes) :
|__t_buffer_head (131072)
| |__buffer1 (65536)
| |__buffer2 (65536)
|__t_buffer_subhead (65536)
|__buffer1 (3276
|__buffer2 (3276

Of course, in case the structure contains pointers, it would likely
indicate it whith a (???) because not knowing the malloc size
associated.

This would be the ideal format, but any approaching solution is
interesting.

Thanks in advance for your advices !
Regards,
JM
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      11-23-2011
On 11/23/2011 06:00 AM, Jean-Michel Hautbois wrote:
> Hi,
>
> I have a (big) structure, which contains other structures with
> (sometimes) big buffers.
>
> Something like :
>
> #define BUF_SIZE 65536
> #define BUF_SIZE2 32768
>
> typedef struct {
> unsigned char buffer1[BUF_SIZE];
> unsigned char buffer2[BUF_SIZE];
> } t_buffer_head;
>
> typedef struct {
> unsigned char buffer1[BUF_SIZE2];
> unsigned char buffer2[BUF_SIZE2];
> } t_buffer_subhead;
>
> typedef struct {
> t_buffer_head header;
> t_buffer_subhead subheader;
> } t_buffer_s;
>
> In my use case, I have >100 structures of t_buffer_something and one
> big structure of these structures.
> I would like, at compile time ideally, or by a static analysis script,
> to know the footprint in memory of the big structure (here,
> t_buffer_s).


Any capability to calculate such things at compile time would have to be
a feature of the particular compiler you're using; the C standard
mandates no such feature. Offhand, I don't know of any such feature
provided by any compiler I use, but then I don't use a lot of different
compilers. Without specifying which compiler you want to use, there's no
way anyone can answer that question for you.

buffer1 and buffer2 have sizes that can be determined just by static
analysis of the code, However, the size of anything other than character
variables and character arrays can vary from one implementation of C to
another. Therefore, any such static analysis tool will have to be
associated with a particular compiler. Again, the question cannot be
answered without specifying the compiler you want to use.

If you do have a particular compiler in mind, for best results you
should post your question in a forum devoted to that particular compiler.

> Something like :
>
> t_buffer_s (196608 bytes) :
> |__t_buffer_head (131072)
> | |__buffer1 (65536)
> | |__buffer2 (65536)
> |__t_buffer_subhead (65536)
> |__buffer1 (3276
> |__buffer2 (3276


Something that would work perfectly fine, and on all compilers, would be
to write a program that contains a declaration for your struct types,
and prints out the sizes using sizeof(), in whatever format you want.
That seems like such a simple solution that I presume you've already
considered it and have decided it's not convenient? You'll need to write
new code to perform the printout each time you create a new struct or
change the members of a struct, and you'll need to recompile it each
time the type or dimensions of a member changes; I suppose that could be
inconvenient.
--
James Kuyper
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-23-2011
On Wed, 2011-11-23, Jean-Michel Hautbois wrote:
> Hi,
>
> I have a (big) structure, which contains other structures with
> (sometimes) big buffers.
>
> Something like :

....
> typedef struct {
> t_buffer_head header;
> t_buffer_subhead subheader;
> } t_buffer_s;
>
> In my use case, I have >100 structures of t_buffer_something and one
> big structure of these structures.
> I would like, at compile time ideally, or by a static analysis script,
> to know the footprint in memory of the big structure (here,
> t_buffer_s).
>
> Something like :
>
> t_buffer_s (196608 bytes) :
> |__t_buffer_head (131072)
> | |__buffer1 (65536)
> | |__buffer2 (65536)
> |__t_buffer_subhead (65536)
> |__buffer1 (3276
> |__buffer2 (3276
>
> Of course, in case the structure contains pointers, it would likely
> indicate it whith a (???) because not knowing the malloc size
> associated.


Or if it's malloced at all, or if a million t_buffer_s structs point
to the same tiny malloced thing ...

> This would be the ideal format, but any approaching solution is
> interesting.


In the past, I have used gcc and (I think) its option to generate
STABS debug information. This format is quite readable, although it
gives you everything in bits, not bytes.

There used to be a utility which came with Perl and wrapped that quite
nicely. I can't remember its name though, but I think it was written by
Tom Christiansen. *googles* Ok, it's called "pstruct" or "c2ph".

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Philipp Klaus Krause
Guest
Posts: n/a
 
      11-23-2011
Am 23.11.2011 12:33, schrieb James Kuyper:
> On 11/23/2011 06:00 AM, Jean-Michel Hautbois wrote:
>> Hi,
>>
>> I have a (big) structure, which contains other structures with
>> (sometimes) big buffers.
>>
>> Something like :
>>
>> #define BUF_SIZE 65536
>> #define BUF_SIZE2 32768
>>
>> typedef struct {
>> unsigned char buffer1[BUF_SIZE];
>> unsigned char buffer2[BUF_SIZE];
>> } t_buffer_head;
>>
>> typedef struct {
>> unsigned char buffer1[BUF_SIZE2];
>> unsigned char buffer2[BUF_SIZE2];
>> } t_buffer_subhead;
>>
>> typedef struct {
>> t_buffer_head header;
>> t_buffer_subhead subheader;
>> } t_buffer_s;
>>
>> In my use case, I have >100 structures of t_buffer_something and one
>> big structure of these structures.
>> I would like, at compile time ideally, or by a static analysis script,
>> to know the footprint in memory of the big structure (here,
>> t_buffer_s).

>
> Any capability to calculate such things at compile time would have to be
> a feature of the particular compiler you're using; the C standard
> mandates no such feature.


How about sizeof?

Philipp

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-23-2011
On 11/23/2011 01:58 PM, Philipp Klaus Krause wrote:
> Am 23.11.2011 12:33, schrieb James Kuyper:
>> On 11/23/2011 06:00 AM, Jean-Michel Hautbois wrote:

....
>>> In my use case, I have >100 structures of t_buffer_something and one
>>> big structure of these structures.
>>> I would like, at compile time ideally, or by a static analysis script,
>>> to know the footprint in memory of the big structure (here,
>>> t_buffer_s).

>>
>> Any capability to calculate such things at compile time would have to be
>> a feature of the particular compiler you're using; the C standard
>> mandates no such feature.

>
> How about sizeof?


The C standard does mandate the sizeof operator, and it can be used to
create a program which, at run-time, will generate output like that
which he's asking for; I said as much in a later part of my message
which you snipped.

The C standard does not mandate the existence of any feature that would
produce such output at compile time. The only output that normally
appears at compile time and is mandated by the C standard is diagnostic
messages, the text of which is not specified by the standard, nor under
the control of the developer - with the exception of the #error
directive. However, the #error directive is executed during phase 4;
sizeof expressions cannot be evaluated until phase 7, so the value of
such expressions cannot appear in #error output.
 
Reply With Quote
 
Ark
Guest
Posts: n/a
 
      11-24-2011


On 11/23/2011 3:41 PM, James Kuyper wrote:
> On 11/23/2011 01:58 PM, Philipp Klaus Krause wrote:
>> Am 23.11.2011 12:33, schrieb James Kuyper:
>>> On 11/23/2011 06:00 AM, Jean-Michel Hautbois wrote:

> ...
>>>> In my use case, I have>100 structures of t_buffer_something and one
>>>> big structure of these structures.
>>>> I would like, at compile time ideally, or by a static analysis script,
>>>> to know the footprint in memory of the big structure (here,
>>>> t_buffer_s).
>>>
>>> Any capability to calculate such things at compile time would have to be
>>> a feature of the particular compiler you're using; the C standard
>>> mandates no such feature.

>>
>> How about sizeof?

>
> The C standard does mandate the sizeof operator, and it can be used to
> create a program which, at run-time, will generate output like that
> which he's asking for; I said as much in a later part of my message
> which you snipped.
>
> The C standard does not mandate the existence of any feature that would
> produce such output at compile time. The only output that normally
> appears at compile time and is mandated by the C standard is diagnostic
> messages, the text of which is not specified by the standard, nor under
> the control of the developer - with the exception of the #error
> directive. However, the #error directive is executed during phase 4;
> sizeof expressions cannot be evaluated until phase 7, so the value of
> such expressions cannot appear in #error output.

Are you sure you are not mixing up sizeof type and sizeof object?
--
Ark
 
Reply With Quote
 
Ark
Guest
Posts: n/a
 
      11-24-2011


On 11/23/2011 4:55 PM, Vincenzo Mercuri wrote:
> Vincenzo Mercuri ha scritto:
> [...]
>> I wonder how this could be useful if not in some "compile-time assert-
>> like macros". There is some feature like this in the "gnulib" library,
>> something like the "_Static_assert" specified by the C1X draft.
>> Take a look: http://goo.gl/4lMfK
>>

>
> Oh, it looks like static assertions are supported in gcc 4.6 and later
> http://gcc.gnu.org/gcc-4.6/changes.html#c
>

It's been elaborated in this NG some year ago:
#define static_assert(expr) extern char dummy_array[(expr)?:1:-1]
--
Ark
 
Reply With Quote
 
Philip Lantz
Guest
Posts: n/a
 
      11-24-2011
On Wed, 23 Nov 2011 20:59:36 -0800, Ark <>
wrote:
> On 11/23/2011 3:41 PM, James Kuyper wrote:
>> However, the #error directive is executed during phase 4;
>> sizeof expressions cannot be evaluated until phase 7, so the value of
>> such expressions cannot appear in #error output.

> Are you sure you are not mixing up sizeof type and sizeof object?


In what way do you think he might be mixing them up? Do you think that
sizeof type is evaluated in a different phase from sizeof object?
 
Reply With Quote
 
Jean-Michel Hautbois
Guest
Posts: n/a
 
      11-24-2011
On Nov 23, 10:46*pm, Vincenzo Mercuri <vincenzo.merc...@yahoo.it>
wrote:
> Jean-Michel Hautbois ha scritto:
> [...]> In my use case, I have>100 structures of t_buffer_something and one
> > big structure of these structures.
> > I would like, at compile time ideally, or by a static analysis script,
> > to know the footprint in memory of the big structure (here,
> > t_buffer_s).

>
> [...]
>
> I was not sure of what you meant by "footprint in memory". Then I
> searched on wikipedia and found: "Memory footprint refers to the amount
> of main memory that a program uses or references while running".
>
> How can you have at compile time the amount of memory that a program
> uses or references while running?
>
> I'm confused, but the fact that you are not interested in memory
> dynamically allocated (i.e. "malloced") makes me think that you only
> need to know the total size of the structures.


Yes, I didn't know how to call it . It is the static size of the
structure that interests me.

> I wonder how this could be useful if not in some "compile-time assert-
> like macros". There is some feature like this in the "gnulib" library,
> something like the "_Static_assert" specified by the C1X draft.
> Take a look:http://goo.gl/4lMfK
>


Thanks, I didn't thought about that stuff !
I will think about a way to use it...
JM
 
Reply With Quote
 
Philipp Klaus Krause
Guest
Posts: n/a
 
      11-24-2011
Am 23.11.2011 21:41, schrieb James Kuyper:
> On 11/23/2011 01:58 PM, Philipp Klaus Krause wrote:
>> Am 23.11.2011 12:33, schrieb James Kuyper:
>>> On 11/23/2011 06:00 AM, Jean-Michel Hautbois wrote:

> ...
>>>> In my use case, I have >100 structures of t_buffer_something and one
>>>> big structure of these structures.
>>>> I would like, at compile time ideally, or by a static analysis script,
>>>> to know the footprint in memory of the big structure (here,
>>>> t_buffer_s).
>>>
>>> Any capability to calculate such things at compile time would have to be
>>> a feature of the particular compiler you're using; the C standard
>>> mandates no such feature.

>>
>> How about sizeof?

>
> The C standard does mandate the sizeof operator, and it can be used to
> create a program which, at run-time, will generate output like that
> which he's asking for; I said as much in a later part of my message
> which you snipped.
>
> The C standard does not mandate the existence of any feature that would
> produce such output at compile time. The only output that normally
> appears at compile time and is mandated by the C standard is diagnostic
> messages, the text of which is not specified by the standard, nor under
> the control of the developer - with the exception of the #error
> directive. However, the #error directive is executed during phase 4;
> sizeof expressions cannot be evaluated until phase 7, so the value of
> such expressions cannot appear in #error output.


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.

Philipp

If, on the other hand, the OP just wants to know personally about the
size of objects in memory (as opposed to using the result in the program
as above): Many linkers have a command-line option to give the desired
information.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using virtual memory and/or disk to save reduce memory footprint nick C++ 58 03-16-2009 01:08 PM
Memory footprint of a subclass Adam Warner Java 3 02-27-2006 09:36 AM
Thumbnail creation with small memory footprint. gbrun Java 1 02-19-2006 11:35 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
JMS memory footprint size Beatrice Rutger Java 0 06-05-2005 09:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57