Structs are just abstractions (proof)

Discussion in 'Programming General' started by Govind, Dec 25, 2010.

Structs are just abstractions (proof)
  1. Unread #1 - Dec 25, 2010 at 9:59 AM
  2. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Structs are just abstractions (proof)

    Proof that structs are just abstractions of contiguous memory:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    typedef struct __s1
    {
    	int a;
    	int b;
    } s1;
    
    int main()
    {
    	long f = 0;
    	s1 st;
    	st.a = 7;
    	st.b = 4;
    	memcpy(&f, &st, 4);
    	printf("%d\n", f);
    	return 0;
    }
    f = 7.
     
  3. Unread #2 - Dec 28, 2010 at 1:16 AM
  4. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    465
    Sythe Gold:
    5,271
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Structs are just abstractions (proof)

    Careful, the padding in a struct is based on the architecture's data alignment.
     
  5. Unread #3 - Dec 28, 2010 at 8:48 AM
  6. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Structs are just abstractions (proof)

    Would there be padding before the first variable? Besides, no type other than int is used.
     
  7. Unread #4 - Dec 28, 2010 at 5:27 PM
  8. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    465
    Sythe Gold:
    5,271
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Structs are just abstractions (proof)

  9. Unread #5 - Jul 7, 2011 at 7:52 PM
  10. Blondi
    Joined:
    Jan 21, 2007
    Posts:
    1,458
    Referrals:
    0
    Sythe Gold:
    0

    Blondi Guru
    Banned

    Structs are just abstractions (proof)

    Yikes SMR, I wouldn't do that except for experimental/demonstration purposes: as was correctly noted above, there is usually no padding before the first variable in a struct, but what if you were on an architecture where an int is 2 bytes, not 4? The result would be (7 << 2 | 4), whatever that works out to.
     
< (help) Open A Program | [HELP] Proxy with HttpWebRequests? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site