Thread: How does the memory line up in the structure?

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    28

    How does the memory line up in the structure?

    I am trying to interface with an external program using a shared object C file. To do this, I need to understand how the structure, serv_addr, in my shared library is composed in memory i.e. its data types and number/ordering of bytes.

    I've included the relevant sections from the header files and my shared library function below.

    Could someone tell me how the computer would see/store serv_addr when my function is called?

    Would it be:

    int32 SIN6_SA_FAMILY; //10
    int32 SIN6_PORT; // 3344
    uint32 SIN6_FLOWINFO; // 0
    uint8 ADDR8[0]; // Where 0 would be represented as 48
    uint8 ADDR8[1];
    uint8 ADDR8[2];
    ...
    uint8 ADDR8[16];
    uint32 SIN6_SCOPE_ID; // 0


    Or would it be in a different order/representation?


    Relevant Sections from Header Files:
    Code:
    // Relevant Sections fromHeader Files
    
    /* POSIX.1g specifies this typename for the `sa_family' member.  */
    typedef unsigned short int sa_family_t;
    
    /* This macro is used todeclare the initial common members
       of the data types used for socket addresses,`struct sockaddr',
       `struct sockaddr_in', `struct sockaddr_un',etc.  */
    
    #define     __SOCKADDR_COMMON(sa_prefix) \
      sa_family_t sa_prefix##family
    
    
    
    /* IPv6 address */
    struct in6_addr
     {
    union
          {
    uint8_t__u6_addr8[16];
    #if defined__USE_MISC || defined __USE_GNU
    uint16_t__u6_addr16[8];
    uint32_t__u6_addr32[4];
    #endif
          } __in6_u;
    #define s6_addr               __in6_u.__u6_addr8
    #if defined__USE_MISC || defined __USE_GNU
    # define s6_addr16            __in6_u.__u6_addr16
    # define s6_addr32            __in6_u.__u6_addr32
    #endif
     };
    
    extern const struct in6_addr in6addr_any;        /* :: */
    extern const struct in6_addr in6addr_loopback;   /* ::1 */
    #define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
    #define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
    
    
    
    struct sockaddr_in6
     {
        __SOCKADDR_COMMON (sin6_);
    in_port_t sin6_port;      /* Transport layer port # */
    uint32_t sin6_flowinfo;   /* IPv6 flow information */
    struct in6_addr sin6_addr;      /* IPv6 address */
    uint32_t sin6_scope_id;   /* IPv6 scope-id */
      };
    My Shared Object Library Function:
    Code:
    // My Shared Object Library Function
    
    void getServerAddrJ(int sockfd, int portNum, struct sockaddr_in6 *serv_addr) {
    // Parse Address
    bzero((char *)& serv_addr, sizeof(serv_addr));
          serv_addr->sin6_flowinfo = 0;
          serv_addr->sin6_family = AF_INET6; // AF_INET6 = 10;
    
          serv_addr->sin6_addr = in6addr_any;
          serv_addr->sin6_port = htons(portNum); // Convert the port number to network byte order
    }

    Thanks!
    Last edited by Salem; 08-01-2017 at 12:33 AM. Reason: fixed train-wreck font/size abuse - next time, use "paste as text"

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If both things use the same header file, then you don't have a problem.

    If you don't have a common header file, then you basically have to do this where the header file is accessible.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    
    struct foo {
      int a;
      char c;
      double d;
      long int l;
    };
    
    int main()
    {
      struct foo foo;
      printf("a: Offset=%zd, size=%zd\n", offsetof(struct foo,a), sizeof(foo.a) );
      printf("c: Offset=%zd, size=%zd\n", offsetof(struct foo,c), sizeof(foo.c) );
      printf("d: Offset=%zd, size=%zd\n", offsetof(struct foo,d), sizeof(foo.d) );
      printf("l: Offset=%zd, size=%zd\n", offsetof(struct foo,l), sizeof(foo.l) );
    }
    
    $ gcc foo.c
    $ ./a.out 
    a: Offset=0, size=4
    c: Offset=4, size=1
    d: Offset=8, size=8
    l: Offset=16, size=8
    Then import that data to where the header file is not accessible.

    Further, you have to do this every time you change the source code / OS / compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    Thanks Salem! How do I mark your answer as the solution or give you Kudos/Thanks etc.?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You just did
    There's no actual solved / appreciate / rep system here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-19-2015, 05:51 PM
  2. why does this structure require a malloc() line?
    By bilsch02 in forum C Programming
    Replies: 2
    Last Post: 02-14-2015, 02:31 PM
  3. Replies: 2
    Last Post: 07-15-2013, 09:15 AM
  4. I need your Help, structure in memory
    By Serj in forum C Programming
    Replies: 4
    Last Post: 12-09-2011, 12:15 PM
  5. Replies: 4
    Last Post: 04-25-2010, 10:57 AM

Tags for this Thread