Results 1 to 8 of 8

Thread: struct

  1. #1
    Join Date
    Jul 2013
    Beans
    39

    struct

    Why do struct's take up a lot more memory than the sum of the actual sizes of its members, a lot lot more for larger structs ? If the compiler is using some padding and or aligning features, I would like to not have or disable that, since it is unnecessarily wasteful of memory. I have encountered a similar problem when using floating point variables.

  2. #2
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: struct

    Quote Originally Posted by sundaresh View Post
    Why do struct's take up a lot more memory than the sum of the actual sizes of its members, a lot lot more for larger structs ? If the compiler is using some padding and or aligning features, I would like to not have or disable that, since it is unnecessarily wasteful of memory. I have encountered a similar problem when using floating point variables.
    Uh ... which language and which compiler? We can't read minds. There are over 1500 different languages with 50+ being popular.

  3. #3
    Join Date
    Jul 2013
    Beans
    39

    Re: struct

    My apologies, I was not aware struct was a keyword in programming languages other than C/C++ , and I presumed gcc to be the fairly standard C compiler on all Linux distributions including on Ubuntu as well. But this problem resolved, although facing another domino one. Apparently my installation generates code for my platform, which is 64-bit, making pointer variables 64-bit and I only want to generate 32-bit code and the -m32 option reports a missing header file <bits/libc-header-start.h>. If I need to, I will post this as a separate thread,

  4. #4
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: struct

    If you want people who know the language to respond, always say the language and the standards version in the title.
    There are at least 4 other languages that use the 'struct' keyword, BTW.

  5. #5
    Join Date
    Nov 2008
    Location
    Woodstock, IL
    Beans
    30
    Distro
    Ubuntu 18.04 Bionic Beaver

    Re: struct

    The alignment of items within structures, classes, and unions is not defined, except that members are laid out in order of declaration. This is a direct quote from The Practice of Programming by Kernighan and Pike. The reason is that different machine architectures require different data types (char, int, float, ...) to be aligned on certain memory boundaries.
    Comfortably numb...

  6. #6
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: struct

    Quote Originally Posted by edadasiewicz View Post
    The alignment of items within structures, classes, and unions is not defined, except that members are laid out in order of declaration. This is a direct quote from The Practice of Programming by Kernighan and Pike. The reason is that different machine architectures require different data types (char, int, float, ...) to be aligned on certain memory boundaries.
    The default alignment of structure members can be changed. This may save space but the members are then less efficient to access.

    Code:
    #include <stdio.h>
    
    struct s_t {
      int a;
      char b;
      int c;
    };
    
    #pragma pack(1)
    struct t_t {
      int a;
      char b;
      int c;
    };
    
    int main()
    {
      printf("%d\n", sizeof(struct s_t));
      printf("%d\n", sizeof(struct t_t));
      return 0;
    }
    Prints 12 and 9 for me.

  7. #7
    Join Date
    Jul 2006
    Beans
    173
    Distro
    Xubuntu

    Re: struct

    Quote Originally Posted by sundaresh View Post
    My apologies, I was not aware struct was a keyword in programming languages other than C/C++ , and I presumed gcc to be the fairly standard C compiler on all Linux distributions including on Ubuntu as well. But this problem resolved, although facing another domino one. Apparently my installation generates code for my platform, which is 64-bit, making pointer variables 64-bit and I only want to generate 32-bit code and the -m32 option reports a missing header file <bits/libc-header-start.h>. If I need to, I will post this as a separate thread,
    Sounds like you have no i386 libs.

    Which gcc package did you install from apt? Try gcc-multilib. If you installed regular gcc or the build-essentials package I'm not sure it installs multi-architecture library support.

  8. #8
    Join Date
    Jul 2013
    Beans
    39

    Re: struct

    Thanks to you all. Installed multi-verse just now, if #pragma is the fairly standard one could I avoid the gcc specific __attribute__ which I am using now. On an unrelated topic, desperately looking for volunteers to test index module for a DBMS I am coding. Perfect Ordered Hash based(coded) and Appointed Tree based(yet to be coded) index's are supported. The hash and the index modules can be used independent of the DBMS. POSIX and pthreads. Intended to handle lots of pthreads. Appreciate any pointers to where and how I can get this help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •