Adblock breaks this site

Structs are just abstractions (proof)

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

  1. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    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
    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.
     
  2. Sythe

    Sythe Join our discord

    test

    Administrator Village Drunk

    Joined:
    Apr 21, 2005
    Posts:
    8,075
    Referrals:
    468
    Sythe Gold:
    5,290
    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
    Structs are just abstractions (proof)

    Careful, the padding in a struct is based on the architecture's data alignment.
     
  3. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    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
    Structs are just abstractions (proof)

    Would there be padding before the first variable? Besides, no type other than int is used.
     
  4. Sythe

    Sythe Join our discord

    test

    Administrator Village Drunk

    Joined:
    Apr 21, 2005
    Posts:
    8,075
    Referrals:
    468
    Sythe Gold:
    5,290
    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
    Structs are just abstractions (proof)

  5. Blondi

    Blondi Guru
    Banned

    Joined:
    Jan 21, 2007
    Posts:
    1,458
    Referrals:
    0
    Sythe Gold:
    0
    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? >


 
 
Adblock breaks this site