Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Memory footprint of a structure of structures (http://www.velocityreviews.com/forums/t806210-memory-footprint-of-a-structure-of-structures.html)

Jean-Michel Hautbois 11-23-2011 11:00 AM

Memory footprint of a structure of structures
 
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 (32768)
|__buffer2 (32768)

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

James Kuyper 11-23-2011 11:33 AM

Re: Memory footprint of a structure of structures
 
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 (32768)
> |__buffer2 (32768)


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

Jorgen Grahn 11-23-2011 05:37 PM

Re: Memory footprint of a structure of structures
 
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 (32768)
> |__buffer2 (32768)
>
> 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 .

Philipp Klaus Krause 11-23-2011 06:58 PM

Re: Memory footprint of a structure of structures
 
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


James Kuyper 11-23-2011 08:41 PM

Re: Memory footprint of a structure of structures
 
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.

Ark 11-24-2011 04:59 AM

Re: Memory footprint of a structure of structures
 


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

Ark 11-24-2011 05:04 AM

Re: Memory footprint of a structure of structures
 


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

Philip Lantz 11-24-2011 06:01 AM

Re: Memory footprint of a structure of structures
 
On Wed, 23 Nov 2011 20:59:36 -0800, Ark <akhasin@macroexpressions.com>
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?

Jean-Michel Hautbois 11-24-2011 08:39 AM

Re: Memory footprint of a structure of structures
 
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

Philipp Klaus Krause 11-24-2011 09:06 AM

Re: Memory footprint of a structure of structures
 
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.


All times are GMT. The time now is 01:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.