Finding the sizes of all structures in a header file without instantiating

This is a discussion on Finding the sizes of all structures in a header file without instantiating within the C++ Programming forums, part of the General Programming Boards category; Hi to all, I am new to this forum and will like to introduce myself. I am a software engineer ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Finding the sizes of all structures in a header file without instantiating

    Hi to all,

    I am new to this forum and will like to introduce myself. I am a software engineer but new to programming.

    I have a programming problem to solve.

    I have a project that contains a header file in which there are around 1000 structures. I need to calculate the sizes of all the structures in a single go and without instantiating any structure. I know 'sizeof' operator is used to find the size of a data type but I need to find the sizes of all the structures programatically using 'sizeof' of course. I need to save this data in a new file.

    Thanks a lot.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,454
    You need to create a list, by hand, of all types in the header file. No, there is not a single C++ statement, preprocessor directive, nor is there a universally applicable compilation option to loop over all types in a header file.

    Simply do a search of each header file (and all header files it #include's, recursively) for the keywords struct and class, and capture the results of a search. You might be able to simplify the search by writing a single source file that #include's your header, and searching the output from the preprocessor. This assumes your compiler has an option to capture preprocessor output. To find that out, read the documentation of your compiler.

    Bear in mind that there is no guarantee a declaration of the form "class X" does not have a newline or several after the class keyword.

    The next step is, for each Name (or a struct or class type) found, simply write code that computes sizeof(Name) for each Name found, and outputs the value to a data file as needed. Remove any duplicates as needed (to allow for template definitions and forward declarations). There is no need to instantiate any of the structs. (Note: if you want to do this in C, compute sizeof(struct Name).

    Bear in mind that the results of sizeof() are implementation defined ..... they vary between compilers. They can also change with different configuration options of a single compiler.

    You may well need to handle template classes separately, if you need the sizes of those. A template class, in itself, does not have a size .... it is necessary to instantiate the template. SomeTemplateClass<x> and SomeTemplateClass<y> may well have different sizes, if x and y are different types, and you need to know what types the template has to be instantiated for.

    Personally, I don't see the point of the exercise though. A header file with 1000 type definitions is also disgusting code organisation. And there is generally very little point in collating the sizes of thousands of user-defined types.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Code:
    size_t answer = sizeof(structure1) + sizeof(structure2) + sizeof(structure3) ...
    where ... means you fill in the other 997 or so.

    That's absolutely insane to have a header file containing 1000 structures.
    Why on earth are you being asked to do this ridiculous thing?

    What is supposed to be done if there are structures inside structures? Surely adding the size of the inner structure individually only makes sense when there is no instance of that struct contained within the outer struct. This exercise reeks of stupidity.
    Last edited by iMalc; 01-21-2012 at 08:23 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Quote Originally Posted by grumpy View Post
    You need to create a list, by hand, of all types in the header file. No, there is not a single C++ statement, preprocessor directive, nor is there a universally applicable compilation option to loop over all types in a header file.

    Simply do a search of each header file (and all header files it #include's, recursively) for the keywords struct and class, and capture the results of a search. You might be able to simplify the search by writing a single source file that #include's your header, and searching the output from the preprocessor. This assumes your compiler has an option to capture preprocessor output. To find that out, read the documentation of your compiler.

    Bear in mind that there is no guarantee a declaration of the form "class X" does not have a newline or several after the class keyword.

    The next step is, for each Name (or a struct or class type) found, simply write code that computes sizeof(Name) for each Name found, and outputs the value to a data file as needed. Remove any duplicates as needed (to allow for template definitions and forward declarations). There is no need to instantiate any of the structs. (Note: if you want to do this in C, compute sizeof(struct Name).

    Bear in mind that the results of sizeof() are implementation defined ..... they vary between compilers. They can also change with different configuration options of a single compiler.

    You may well need to handle template classes separately, if you need the sizes of those. A template class, in itself, does not have a size .... it is necessary to instantiate the template. SomeTemplateClass<x> and SomeTemplateClass<y> may well have different sizes, if x and y are different types, and you need to know what types the template has to be instantiated for.

    Personally, I don't see the point of the exercise though. A header file with 1000 type definitions is also disgusting code organisation. And there is generally very little point in collating the sizes of thousands of user-defined types.
    Thanks a lot for your reply,

    I have found out the name of all the structures in the header file using file handling. Now the problem is that this name is in a string variable or a character array. If I will find the size of this string, it will give the size of the string and not the structure. Can I convert the value in a string to a predefined structure name and then do sizeof. This way I will not have to use sizeof individually and I can find the size in a one go.

    Thanks a lot.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by hiteshverma1984 View Post
    This way I will not have to use sizeof individually and I can find the size in a one go.
    You'd need to dynamically generate code and then compile that and run it. You'd also ned to declare a struct containing an instance of every other struct.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    1
    @hitesh i have the same problem as well . i have structure name is char string how can i get size of that structure .please do help me out on this one

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    Read the responses so far.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding header file's contents
    By Else in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2009, 06:22 AM
  2. File sizes
    By bradszy in forum C++ Programming
    Replies: 18
    Last Post: 04-28-2008, 05:24 AM
  3. dev-c++ file sizes are huge
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2005, 04:08 PM
  4. Getting file sizes
    By mart_man00 in forum C Programming
    Replies: 22
    Last Post: 04-01-2003, 08:17 AM
  5. finding data sizes
    By joegio13 in forum C++ Programming
    Replies: 8
    Last Post: 11-16-2002, 12:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21