Thread: What would be the result...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    54

    What would be the result...

    Hi friends at present i am not having any compiler so please can any one conform the result of this program
    what would be the size of this structure
    Code:
    struct str 
    { 
    int data; 
    struct str *pstr; 
    char ch; 
    int data1; 
    };
    what would be the size of this structure...
    Last edited by sreeramu; 03-25-2008 at 03:03 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Assuming no padding, and chap = char

    sizeof(int) + sizeof(struct str *) + sizeof(char) + sizeof(int)

    And on most 32bit systems sizeof(int) = 4, and the size of a pointer is also 4. The standard also sets sizeof(char) must always equal _. Fill in the blanks, I aint doing your homework. It really depends on what this is compiled, the results will differ from 16bit, 32bit and 64bit compilers at least.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or easier, sizeof(str)
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also consider that there are alignment issues with this struct, as the pointer pstr and integer data1 should be correctly aligned to the appropriate pointer and integer alignment respectively.
    The alignment requirements varies depending on the architecture of the system as well as settings for the compiler [e.g. #pragma pack() and such like], so to fully answer the question you need to know:
    1. The size of a int and pointer.
    2. The alignments suitable for int and pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    According to my calculation the size would be 16 bytes ....am i correct ..??

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    On a 32-bit system, I would imagine it is. But why not try for yourself? sizeof(str).
    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.

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by Elysia View Post
    On a 32-bit system, I would imagine it is. But why not try for yourself? sizeof(str).
    he said he does not have any compiler! why would he ask us poor fellow if he had??

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sreeramu View Post
    According to my calculation the size would be 16 bytes ....am i correct ..??
    It is one possible result, yes. In a 64-bit system, the size would probably be 4 + (4) + 8 + 1 + (3) + 4 = 24 bytes instead. Numbers in parenthesis are the alignment additions. This is just ONE example of the possible answers - without knowing more of what the system configuration is , we can't really say.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Even without a compiler, testing the hypothesis that the size of the struct could be 16 with Comeau online

    Code:
    struct str 
    { 
    int data; 
    struct str *pstr; 
    char ch; 
    int data1; 
    };
    
    int main(void)
    {
       int arr[10 / (sizeof(struct str) - 16)];
    }
    Which produces the following error message, confirming that (with these options) the size is indeed 16:
    Code:
    "ComeauTest.c", line 11: error: division by zero
         int arr[10 / (sizeof(struct str) - 16)];
                    ^
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 01:44 AM
  3. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  4. Can someone help with my seg fault?
    By John_L in forum C++ Programming
    Replies: 23
    Last Post: 03-01-2008, 04:04 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM