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