Thread: Explain why I am get respective o/p's??

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20

    Explain why I am get respective o/p's??

    //Difference b/w addresses is not matching and difference is also not matching //with declaration size. why??
    Code:
    1.
    #include<stdio.h>
    int main()
    {
        char a[0];
        int c;
        printf("%u %u %u",sizeof(a),a,&c);
    }
    o/p:
    0 3213783376 3213783372
    
    2.
    #include<stdio.h>
    int main()
    {
        char a[1];
        int c;
        printf("%u %u %u",sizeof(a),a,&c);
    }
    o/p:
    1 3217296447 3217296440
    
    3.
    #include<stdio.h>
    int main()
    {
        char a[2];
        int c;
        printf("%u %u %u",sizeof(a),a,&c);
    }
    o/p:
    2 3219026494 3219026488
    
    4.
    #include<stdio.h>
    int main()
    {
        char a[3];
        int c;
        printf("%u %u %u",sizeof(a),a,&c);
    }
    o/p:
    3 3213484781 3213484776
    
    5.
    #include<stdio.h>
    int main()
    {
        char a[4];
        int c;
        printf("%u %u %u",sizeof(a),a,&c);
    }
    o/p:
    4 3213052588 3213052584

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Addresses aren't supposed to match from run to run. Also, an int is generally going to be required to be at a multiple-of-four address, so there will be empty space as needed between the variables to make that happen.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20
    thx but In the second question for int memory is allocated at 3217296440 following ur concept it should have allocated at 3217296444

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    3217296440 is an address divisible by four. (There's no particular reason it has to be the next address divisible by four.)

  5. #5
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20
    As in most of the cases they will be allocated contiguously because of this I got doubt.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would have suggested multiple-of-eight alignment except that the first example misses. (And if sizeof(int) == 8 rather than 4, then it's even more likely.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Could someone help explain this a little bit
    By rocketman50 in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2011, 07:29 PM
  2. Replies: 3
    Last Post: 02-03-2008, 02:12 PM
  3. Could someone explain this?
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 02-14-2006, 08:44 AM
  4. Someone explain it please
    By yakabod in forum C++ Programming
    Replies: 3
    Last Post: 08-13-2003, 05:30 PM
  5. The respective ATN function in C?
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2003, 11:13 AM