Thread: size of sturcture

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    size of sturcture

    Code:
    struct knowsize
    {
    	int j;
    	char i;
    	char k;
    	int j1;
    }size;
    
    printf("\nsizeof structure is%d",sizeof(size.j));
    printf("\nsizeof structure is%d",sizeof(size.i));
    printf("\nsizeof structure is%d",sizeof(size.k));
    printf("\nsizeof structure is%d",sizeof(size.j1));
    
    
    printf("\nsizeof structure is%d",sizeof(size));
    output:

    4
    1
    1
    4

    12

    how this size of structure comes 12? I am not able to understand please clear this doubt.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    j sits at 0-3
    i at 4
    k at 5
    j1 cannot sit at 6 because it needs to be 32-bit aligned (multiple of 4), so it sits at 8-11

    That's 12 bytes.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Thanks for clearing my doubt!!! buddy

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    size related operation

    Code:
    char buff[30];
    
    printf("\nthe length of buffer is %d",strlen(buff));
    
    return 0;
    o/p

    the length of buffer is 43


    Can you tell me the reason is it because of not initializing buffer with any value.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    27
    The strlen function counts until it finds an '\0' terminating character. Since your buffer is not initialized the function starts counting ans stops when it finds an '\0', which in this case is 43 bytes after buff[0].

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It's not a DOUBT, it's a question!

    I think someone else explained the difference on some other thread.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > the length of buffer is 43
    or zero,
    or -1
    or why has my machine rebooted.

    And yes, it is because your array is uninitialised.
    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    stlren does not find the size of a buffer; it finds the length of a string stored inside a buffer. And since you haven't stored any string inside the buffer, you invoke undefined behavior.
    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.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Because you didn't initialize the buffer doesn't mean it contains all zeros. It contains garbage. Unless it's global.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nonoob View Post
    Because you didn't initialize the buffer doesn't mean it contains all zeros. It contains garbage. Unless it's global.
    Or static.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cyberfish View Post
    j sits at 0-3
    i at 4
    k at 5
    j1 cannot sit at 6 because it needs to be 32-bit aligned (multiple of 4), so it sits at 8-11

    That's 12 bytes.
    This may help:
    Code:
    printf("\noffset of structure is%d", &size.j - &size);
    printf("\noffset of structure is%d", &size.i - &size);
    printf("\noffset of structure is%d", &size.k - &size);
    printf("\noffset of structure is%d", &size.j1 - &size);
    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"

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use offsetof?
    Code:
    printf("\noffset of structure is%d",  offsetof( size.j ) );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Pointer Size Relative To Integers?
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 04-18-2006, 06:58 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM