can you please find out the amount of space consumed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohitbasu77
    New Member
    • Feb 2008
    • 89

    can you please find out the amount of space consumed

    How much space is consumed in two structure:

    struct xx{
    int a;
    char b;
    }

    struct yy{
    char c;
    int d;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It's platform dependent

    Comment

    • jabbah
      New Member
      • Nov 2007
      • 63

      #3
      not sure, whether I got your question right, but is this an answer?
      [CODE=cpp]
      cout << "sizeof(cha r)=" << sizeof(char) << ", "
      << "sizeof(int )=" << sizeof(int) << ", "
      << "sizeof(xx) =" << sizeof(xx) << ", "
      << "sizeof(yy) =" << sizeof(yy) << "\n";
      [/CODE]

      Comment

      • rohitbasu77
        New Member
        • Feb 2008
        • 89

        #4
        Originally posted by jabbah
        not sure, whether I got your question right, but is this an answer?
        [CODE=cpp]
        cout << "sizeof(cha r)=" << sizeof(char) << ", "
        << "sizeof(int )=" << sizeof(int) << ", "
        << "sizeof(xx) =" << sizeof(xx) << ", "
        << "sizeof(yy) =" << sizeof(yy) << "\n";
        [/CODE]
        i am asking in total how much it will consume. If an object of a class is produce.

        Comment

        • jabbah
          New Member
          • Nov 2007
          • 63

          #5
          Originally posted by rohitbasu77
          some padding concept is there can any one explain it.
          ok, I do not know these details. but I *think* that these are highly compiler/platform/optimizationset ting/whateverelse - dependent aspects.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by Banfa
            It's platform dependent
            Paying attention helps, especially when the question has been answered.

            Comment

            • rohitbasu77
              New Member
              • Feb 2008
              • 89

              #7
              Originally posted by jabbah
              ok, I do not know these details. but I *think* that these are highly compiler/platform/optimizationset ting/whateverelse - dependent aspects.
              The issue is of padding architecture, between a character and a integer.
              how much byte it takes to do so.... is actually my question.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by rohitbasu77
                The issue is of padding architecture, between a character and a integer.
                how much byte it takes to do so.... is actually my question.
                Please read my reply and that of my fellow moderator.

                It is platform dependent.

                Comment

                • rohitbasu77
                  New Member
                  • Feb 2008
                  • 89

                  #9
                  Originally posted by Banfa
                  Please read my reply and that of my fellow moderator.

                  It is platform dependent.
                  say the system is of 4 byte or 32 bit word.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by rohitbasu77
                    say the system is of 4 byte or 32 bit word.
                    It doesn't matter what the word size is it matters what the specified alignment for your system is.

                    A system with a 32 bit word size could still use 1, 2, 4 or other alignments for it's data. You will find this information either in your processor data sheet or your compiler manual (or both).

                    Comment

                    • jabbah
                      New Member
                      • Nov 2007
                      • 63

                      #11
                      Originally posted by rohitbasu77
                      say the system is of 4 byte or 32 bit word.

                      Originally posted by Banfa
                      It doesn't matter what the word size is it matters what the specified alignment for your system is.
                      why do you care about that rohitbasu77?

                      Comment

                      • rohitbasu77
                        New Member
                        • Feb 2008
                        • 89

                        #12
                        Originally posted by jabbah
                        why do you care about that rohitbasu77?

                        Reducing memory usage or conforming to a particular memory layout for an interface to another machine or program is often a requirement for C/C++ programs. ..... this can be done by removing the padding between structure fields...

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          Originally posted by rohitbasu77
                          Reducing memory usage or conforming to a particular memory layout for an interface to another machine or program is often a requirement for C/C++ programs. ..... this can be done by removing the padding between structure fields...
                          Well why didn't you say this in the first place, regardless of the structure alignment that your platform uses you normally get the best use of memory in structures by ordering the elements in the structure by size, (largest first or smallest first I don't think it matters).

                          Another way to save space if the structure contains a lot of bools is to use bit fields instead, this also works if you have a lot of ints that only take a few values (you can hold values in the range 0-3 in 2 bits).

                          You should not try to map structures to the interface of another computer or memory mapped piece of hardware, this is asking for trouble. You should read/write the individual chars, shorts and longs from there location in memory and write/read them into the correct firld of your structure.

                          Comment

                          • rohitbasu77
                            New Member
                            • Feb 2008
                            • 89

                            #14
                            Originally posted by Banfa
                            Well why didn't you say this in the first place, regardless of the structure alignment that your platform uses you normally get the best use of memory in structures by ordering the elements in the structure by size, (largest first or smallest first I don't think it matters).

                            Another way to save space if the structure contains a lot of bools is to use bit fields instead, this also works if you have a lot of ints that only take a few values (you can hold values in the range 0-3 in 2 bits).

                            You should not try to map structures to the interface of another computer or memory mapped piece of hardware, this is asking for trouble. You should read/write the individual chars, shorts and longs from there location in memory and write/read them into the correct firld of your structure.
                            yes even you do that there is a padding between int and char. that padding is system and its compiler specific. the C lib has got a function pragmapack, to use for this purpuse only.......
                            pragma(n)...... .. have you use it............. .
                            The question is how much it realy takes when you write a driver in a small chip....
                            1 byte is a big memory there.......
                            sizeof function will give you the out put what actually an int or char has consume.....
                            Thats theory only.... practically its the difference....

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              Originally posted by rohitbasu77
                              yes even you do that there is a padding between int and char. that padding is system and its compiler specific. the C lib has got a function pragmapack, to use for this purpuse only.......
                              pragma(n)...... .. have you use it............. .
                              No it doesn't, your implementation of C may provide a #pragma pack preprocessor directive but this is NOT standard C and many compilers do not provide this or any way to change structure alignment, you have to live with what ever it does. If your compiler has a #pragma pack and you use it then you will implicitly introduce an portability issue if the hardware of your project ever changes.

                              Originally posted by rohitbasu77
                              The question is how much it realy takes when you write a driver in a small chip....
                              1 byte is a big memory there.......
                              sizeof function will give you the out put what actually an int or char has consume.....
                              Thats theory only.... practically its the difference....
                              I am fully aware of the memory constraints that occur on smaller platforms (having programmed on a PIC with <4k RAM for the past year or so) the sizeof operator will give you the sizeof the type, if that type is a structure then it will include the padding (otherwise pointer arithmetic would not work properly).

                              Take this code for instance

                              [code=c]
                              struct MyStruct {
                              int i;
                              char c;
                              };

                              struct MyStruct mystruct;
                              int sms = sizeof mystruct;
                              int si = sizeof mystruct.i;
                              int sc = sizeof mystruct.c;
                              [/code]Assuming that the system has 4 byte (32 bit ints) and that it uses 4 byte alignment then once this runs

                              sms = 8;
                              si = 4;
                              sc = 1;

                              You can always find out how much wasted space there is in a structure on a given compiler/platform by comparing the sizeof the struct with the size of it's components, in this case 3 bytes (8 - (4+1)).

                              However it you use the rules I gave previously then wasted space will be minimised (typically 0-3 bytes per structure) on most platforms.

                              If you can not afford that wasted space don't use structures, although this will make the code more complex so you need to give the problem some careful thought before going down this route.

                              Comment

                              Working...